mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-30 14:21:04 +00:00
Co-authored-by: Alan Shaw <alan.shaw@protocol.ai> Co-authored-by: Alan Shaw <alan@tableflip.io> Co-authored-by: Arnaud <arnaud.valensi@gmail.com> Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: David Dias <mail@daviddias.me> Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: Francisco Baio Dias <xicombd@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Haad <haadcode@users.noreply.github.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Hugo Dias <hugomrdias@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Kevin Kwok <antimatter15@gmail.com> Co-authored-by: Kobi Gurkan <kobigurk@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: Michael Fakhry <fakhrimichael@live.com> Co-authored-by: Oli Evans <oli@tableflip.io> Co-authored-by: Pau Ramon Revilla <masylum@gmail.com> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Pius Nyakoojo <piusnyakoojo@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Sid Harder <sideharder@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: harrshasri <35241544+harrshasri@users.noreply.github.com> Co-authored-by: kumavis <kumavis@users.noreply.github.com> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>
91 lines
2.4 KiB
JavaScript
91 lines
2.4 KiB
JavaScript
/* eslint-env mocha */
|
|
'use strict'
|
|
|
|
const chai = require('chai')
|
|
const dirtyChai = require('dirty-chai')
|
|
const expect = chai.expect
|
|
chai.use(dirtyChai)
|
|
|
|
const parallel = require('async/parallel')
|
|
const TCP = require('libp2p-tcp')
|
|
const pull = require('pull-stream')
|
|
const PeerBook = require('peer-book')
|
|
|
|
const utils = require('./utils')
|
|
const createInfos = utils.createInfos
|
|
const tryEcho = utils.tryEcho
|
|
const Switch = require('libp2p-switch')
|
|
|
|
describe('Switch (no Stream Multiplexing)', () => {
|
|
let switchA
|
|
let switchB
|
|
|
|
before((done) => createInfos(2, (err, infos) => {
|
|
expect(err).to.not.exist()
|
|
|
|
const peerA = infos[0]
|
|
const peerB = infos[1]
|
|
|
|
peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/9001')
|
|
peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9002/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
|
|
|
|
switchA = new Switch(peerA, new PeerBook())
|
|
switchB = new Switch(peerB, new PeerBook())
|
|
|
|
switchA.transport.add('tcp', new TCP())
|
|
switchB.transport.add('tcp', new TCP())
|
|
|
|
parallel([
|
|
(cb) => switchA.transport.listen('tcp', {}, null, cb),
|
|
(cb) => switchB.transport.listen('tcp', {}, null, cb)
|
|
], done)
|
|
}))
|
|
|
|
after((done) => parallel([
|
|
(cb) => switchA.stop(cb),
|
|
(cb) => switchB.stop(cb)
|
|
], done))
|
|
|
|
it('handle a protocol', (done) => {
|
|
switchB.handle('/bananas/1.0.0', (protocol, conn) => pull(conn, conn))
|
|
expect(switchB.protocols).to.have.all.keys('/bananas/1.0.0')
|
|
done()
|
|
})
|
|
|
|
it('dial on protocol', (done) => {
|
|
switchB.handle('/pineapple/1.0.0', (protocol, conn) => pull(conn, conn))
|
|
|
|
switchA.dial(switchB._peerInfo, '/pineapple/1.0.0', (err, conn) => {
|
|
expect(err).to.not.exist()
|
|
tryEcho(conn, done)
|
|
})
|
|
})
|
|
|
|
it('dial on protocol (returned conn)', (done) => {
|
|
switchB.handle('/apples/1.0.0', (protocol, conn) => pull(conn, conn))
|
|
|
|
const conn = switchA.dial(switchB._peerInfo, '/apples/1.0.0', (err) => {
|
|
expect(err).to.not.exist()
|
|
})
|
|
|
|
tryEcho(conn, done)
|
|
})
|
|
|
|
it('dial to warm a conn', (done) => {
|
|
switchA.dial(switchB._peerInfo, done)
|
|
})
|
|
|
|
it('dial on protocol, reuse warmed conn', (done) => {
|
|
switchA.dial(switchB._peerInfo, '/bananas/1.0.0', (err, conn) => {
|
|
expect(err).to.not.exist()
|
|
tryEcho(conn, done)
|
|
})
|
|
})
|
|
|
|
it('unhandle', () => {
|
|
const proto = '/bananas/1.0.0'
|
|
switchA.unhandle(proto)
|
|
expect(switchA.protocols[proto]).to.not.exist()
|
|
})
|
|
})
|