Skip to content

Commit 6106915

Browse files
jacobheundaviddias
authored andcommitted
fix: start and stop connection manager with libp2p
test: add test to verify libp2p starts and stops the right things test: add test for verifying disabled modules fix: linting
1 parent d9059db commit 6106915

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ class Node extends libp2p {
165165
active: false
166166
}
167167
},
168+
dht: {
169+
kBucketSize: 20
170+
},
168171
// Enable/Disable Experimental features
169172
EXPERIMENTAL: { // Experimental features ("behind a flag")
170173
pubsub: false,

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
},
3434
"homepage": "https://github.com/libp2p/js-libp2p",
3535
"browser": {
36-
"joi": "joi-browser"
36+
"joi": "joi-browser",
37+
"./test/utils/bundle-nodejs": "./test/utils/bundle-browser"
3738
},
3839
"dependencies": {
3940
"async": "^2.6.1",
@@ -73,6 +74,7 @@
7374
"pull-serializer": "~0.3.2",
7475
"pull-stream": "^3.6.8",
7576
"sinon": "^6.1.3",
77+
"webrtcsupport": "^2.2.0",
7678
"wrtc": "~0.1.6"
7779
},
7880
"contributors": [

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ class Node extends EventEmitter {
146146
})
147147

148148
series([
149-
(cb) => this._switch.start(cb),
149+
(cb) => {
150+
this.connectionManager.start()
151+
this._switch.start(cb)
152+
},
150153
(cb) => {
151154
if (ws) {
152155
// always add dialing on websockets
@@ -256,7 +259,10 @@ class Node extends EventEmitter {
256259
}
257260
cb()
258261
},
259-
(cb) => this._switch.stop(cb),
262+
(cb) => {
263+
this.connectionManager.stop()
264+
this._switch.stop(cb)
265+
},
260266
(cb) => {
261267
this.emit('stop')
262268
cb()

test/create.spec.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
chai.use(require('dirty-chai'))
6+
const expect = chai.expect
7+
const series = require('async/series')
8+
const createNode = require('./utils/create-node')
9+
const sinon = require('sinon')
10+
11+
describe('libp2p creation', () => {
12+
it('should be able to start and stop successfully', (done) => {
13+
createNode([], {
14+
config: {
15+
EXPERIMENTAL: {
16+
dht: true,
17+
pubsub: true
18+
}
19+
}
20+
}, (err, node) => {
21+
expect(err).to.not.exist()
22+
23+
let sw = node._switch
24+
let cm = node.connectionManager
25+
let dht = node._dht
26+
let pub = node._floodSub
27+
28+
sinon.spy(sw, 'start')
29+
sinon.spy(cm, 'start')
30+
sinon.spy(dht, 'start')
31+
sinon.spy(pub, 'start')
32+
sinon.spy(sw, 'stop')
33+
sinon.spy(cm, 'stop')
34+
sinon.spy(dht, 'stop')
35+
sinon.spy(pub, 'stop')
36+
sinon.spy(node, 'emit')
37+
38+
series([
39+
(cb) => node.start(cb),
40+
(cb) => {
41+
expect(sw.start.calledOnce).to.equal(true)
42+
expect(cm.start.calledOnce).to.equal(true)
43+
expect(dht.start.calledOnce).to.equal(true)
44+
expect(pub.start.calledOnce).to.equal(true)
45+
expect(node.emit.calledWith('start')).to.equal(true)
46+
47+
cb()
48+
},
49+
(cb) => node.stop(cb)
50+
], (err) => {
51+
expect(err).to.not.exist()
52+
53+
expect(sw.stop.calledOnce).to.equal(true)
54+
expect(cm.stop.calledOnce).to.equal(true)
55+
expect(dht.stop.calledOnce).to.equal(true)
56+
expect(pub.stop.calledOnce).to.equal(true)
57+
expect(node.emit.calledWith('stop')).to.equal(true)
58+
59+
done()
60+
})
61+
})
62+
})
63+
64+
it('should not create disabled modules', (done) => {
65+
createNode([], {
66+
config: {
67+
EXPERIMENTAL: {
68+
dht: false,
69+
pubsub: false
70+
}
71+
}
72+
}, (err, node) => {
73+
expect(err).to.not.exist()
74+
expect(node._dht).to.not.exist()
75+
expect(node._floodSub).to.not.exist()
76+
done()
77+
})
78+
})
79+
})

test/utils/bundle-browser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class Node extends libp2p {
7878
active: false
7979
}
8080
},
81+
dht: {
82+
kBucketSize: 20
83+
},
8184
EXPERIMENTAL: {
8285
dht: false,
8386
pubsub: false

0 commit comments

Comments
 (0)