This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
swarm-no-muxing.node.js
90 lines (70 loc) · 2.37 KB
/
swarm-no-muxing.node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* 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('../src')
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()
})
})