Skip to content

Commit 7fa767c

Browse files
committed
test: update tests to use new start and stop
1 parent c7d1c57 commit 7fa767c

File tree

2 files changed

+105
-70
lines changed

2 files changed

+105
-70
lines changed

test/2-nodes.js

+94-64
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ const parallel = require('async/parallel')
77
const series = require('async/series')
88
const _times = require('lodash.times')
99

10-
const PSG = require('../src')
10+
const FloodSub = require('../src')
1111
const utils = require('./utils')
1212
const first = utils.first
1313
const createNode = utils.createNode
1414
const expectSet = utils.expectSet
1515

16-
describe('basics', () => {
17-
let nodeA
18-
let nodeB
19-
let psA
20-
let psB
21-
16+
describe('basics between 2 nodes', () => {
2217
describe('fresh nodes', () => {
18+
let nodeA
19+
let nodeB
20+
let fsA
21+
let fsB
22+
2323
before((done) => {
2424
series([
2525
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb),
@@ -42,124 +42,143 @@ describe('basics', () => {
4242
})
4343

4444
it('Mount the pubsub protocol', (done) => {
45-
psA = new PSG(nodeA)
46-
psB = new PSG(nodeB)
45+
fsA = new FloodSub(nodeA)
46+
fsB = new FloodSub(nodeB)
4747

4848
setTimeout(() => {
49-
expect(psA.peers.size).to.be.eql(0)
50-
expect(psA.subscriptions.size).to.eql(0)
51-
expect(psB.peers.size).to.be.eql(0)
52-
expect(psB.subscriptions.size).to.eql(0)
49+
expect(fsA.peers.size).to.be.eql(0)
50+
expect(fsA.subscriptions.size).to.eql(0)
51+
expect(fsB.peers.size).to.be.eql(0)
52+
expect(fsB.subscriptions.size).to.eql(0)
5353
done()
5454
}, 50)
5555
})
5656

57+
it('start both FloodSubs', (done) => {
58+
parallel([
59+
(cb) => fsA.start(cb),
60+
(cb) => fsB.start(cb)
61+
], done)
62+
})
63+
5764
it('Dial from nodeA to nodeB', (done) => {
5865
series([
5966
(cb) => nodeA.dialByPeerInfo(nodeB.peerInfo, cb),
6067
(cb) => setTimeout(() => {
61-
expect(psA.peers.size).to.equal(1)
62-
expect(psB.peers.size).to.equal(1)
68+
expect(fsA.peers.size).to.equal(1)
69+
expect(fsB.peers.size).to.equal(1)
6370
cb()
6471
}, 250)
6572
], done)
6673
})
6774

6875
it('Subscribe to a topic:Z in nodeA', (done) => {
69-
psA.subscribe('Z')
76+
fsA.subscribe('Z')
7077
setTimeout(() => {
71-
expectSet(psA.subscriptions, ['Z'])
72-
expect(psB.peers.size).to.equal(1)
73-
expectSet(first(psB.peers).topics, ['Z'])
78+
expectSet(fsA.subscriptions, ['Z'])
79+
expect(fsB.peers.size).to.equal(1)
80+
expectSet(first(fsB.peers).topics, ['Z'])
7481
done()
7582
}, 100)
7683
})
7784

7885
it('Publish to a topic:Z in nodeA', (done) => {
79-
psB.once('Z', shouldNotHappen)
86+
fsB.once('Z', shouldNotHappen)
8087

8188
function shouldNotHappen (msg) { expect.fail() }
8289

83-
psA.once('Z', (msg) => {
90+
fsA.once('Z', (msg) => {
8491
expect(msg.data.toString()).to.equal('hey')
85-
psB.removeListener('Z', shouldNotHappen)
92+
fsB.removeListener('Z', shouldNotHappen)
8693
done()
8794
})
8895

89-
psB.once('Z', shouldNotHappen)
96+
fsB.once('Z', shouldNotHappen)
9097

91-
psA.publish('Z', new Buffer('hey'))
98+
fsA.publish('Z', new Buffer('hey'))
9299
})
93100

94101
it('Publish to a topic:Z in nodeB', (done) => {
95-
psB.once('Z', shouldNotHappen)
102+
fsB.once('Z', shouldNotHappen)
96103

97-
psA.once('Z', (msg) => {
98-
psA.once('Z', shouldNotHappen)
104+
fsA.once('Z', (msg) => {
105+
fsA.once('Z', shouldNotHappen)
99106
expect(msg.data.toString()).to.equal('banana')
100107
setTimeout(() => {
101-
psA.removeListener('Z', shouldNotHappen)
102-
psB.removeListener('Z', shouldNotHappen)
108+
fsA.removeListener('Z', shouldNotHappen)
109+
fsB.removeListener('Z', shouldNotHappen)
103110
done()
104111
}, 100)
105112
})
106113

107-
psB.once('Z', shouldNotHappen)
114+
fsB.once('Z', shouldNotHappen)
108115

109-
psB.publish('Z', new Buffer('banana'))
116+
fsB.publish('Z', new Buffer('banana'))
110117
})
111118

112119
it('Publish 10 msg to a topic:Z in nodeB', (done) => {
113120
let counter = 0
114121

115-
psB.once('Z', shouldNotHappen)
122+
fsB.once('Z', shouldNotHappen)
116123

117-
psA.on('Z', receivedMsg)
124+
fsA.on('Z', receivedMsg)
118125

119126
function receivedMsg (msg) {
120127
expect(msg.data.toString()).to.equal('banana')
121-
expect(msg.from).to.be.eql(psB.libp2p.peerInfo.id.toB58String())
128+
expect(msg.from).to.be.eql(fsB.libp2p.peerInfo.id.toB58String())
122129
expect(Buffer.isBuffer(msg.seqno)).to.be.true
123130
expect(msg.topicCIDs).to.be.eql(['Z'])
124131

125132
if (++counter === 10) {
126-
psA.removeListener('Z', receivedMsg)
133+
fsA.removeListener('Z', receivedMsg)
127134
done()
128135
}
129136
}
130137

131138
_times(10, () => {
132-
psB.publish('Z', new Buffer('banana'))
139+
fsB.publish('Z', new Buffer('banana'))
133140
})
134141
})
135142

136143
it('Unsubscribe from topic:Z in nodeA', (done) => {
137-
psA.unsubscribe('Z')
138-
expect(psA.subscriptions.size).to.equal(0)
144+
fsA.unsubscribe('Z')
145+
expect(fsA.subscriptions.size).to.equal(0)
139146

140147
setTimeout(() => {
141-
expect(psB.peers.size).to.equal(1)
142-
expectSet(first(psB.peers).topics, [])
148+
expect(fsB.peers.size).to.equal(1)
149+
expectSet(first(fsB.peers).topics, [])
143150
done()
144151
}, 100)
145152
})
146153

147154
it('Publish to a topic:Z in nodeA nodeB', (done) => {
148-
psA.once('Z', shouldNotHappen)
149-
psB.once('Z', shouldNotHappen)
155+
fsA.once('Z', shouldNotHappen)
156+
fsB.once('Z', shouldNotHappen)
150157

151158
setTimeout(() => {
152-
psA.removeListener('Z', shouldNotHappen)
153-
psB.removeListener('Z', shouldNotHappen)
159+
fsA.removeListener('Z', shouldNotHappen)
160+
fsB.removeListener('Z', shouldNotHappen)
154161
done()
155162
}, 100)
156163

157-
psB.publish('Z', new Buffer('banana'))
158-
psA.publish('Z', new Buffer('banana'))
164+
fsB.publish('Z', new Buffer('banana'))
165+
fsA.publish('Z', new Buffer('banana'))
166+
})
167+
168+
it('stop both FloodSubs', (done) => {
169+
parallel([
170+
(cb) => fsA.stop(cb),
171+
(cb) => fsB.stop(cb)
172+
], done)
159173
})
160174
})
161175

162176
describe('long running nodes (already have state)', () => {
177+
let nodeA
178+
let nodeB
179+
let fsA
180+
let fsB
181+
163182
before((done) => {
164183
series([
165184
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb),
@@ -168,20 +187,24 @@ describe('basics', () => {
168187
nodeA = nodes[0]
169188
nodeB = nodes[1]
170189

171-
psA = new PSG(nodeA)
172-
psB = new PSG(nodeB)
190+
fsA = new FloodSub(nodeA)
191+
fsB = new FloodSub(nodeB)
173192

174-
psA.subscribe('Za')
175-
psB.subscribe('Zb')
193+
parallel([
194+
(cb) => fsA.start(cb),
195+
(cb) => fsB.start(cb)
196+
], next)
176197

177-
setTimeout(() => {
178-
expect(psA.peers.size).to.equal(0)
179-
expectSet(psA.subscriptions, ['Za'])
180-
expect(psB.peers.size).to.equal(0)
181-
expectSet(psB.subscriptions, ['Zb'])
198+
function next () {
199+
fsA.subscribe('Za')
200+
fsB.subscribe('Zb')
182201

202+
expect(fsA.peers.size).to.equal(0)
203+
expectSet(fsA.subscriptions, ['Za'])
204+
expect(fsB.peers.size).to.equal(0)
205+
expectSet(fsB.subscriptions, ['Zb'])
183206
done()
184-
}, 50)
207+
}
185208
})
186209
})
187210

@@ -196,21 +219,28 @@ describe('basics', () => {
196219
nodeA.dialByPeerInfo(nodeB.peerInfo, (err) => {
197220
expect(err).to.not.exist
198221
setTimeout(() => {
199-
expect(psA.peers.size).to.equal(1)
200-
expect(psB.peers.size).to.equal(1)
222+
expect(fsA.peers.size).to.equal(1)
223+
expect(fsB.peers.size).to.equal(1)
201224

202-
expectSet(psA.subscriptions, ['Za'])
203-
expect(psB.peers.size).to.equal(1)
204-
expectSet(first(psB.peers).topics, ['Za'])
225+
expectSet(fsA.subscriptions, ['Za'])
226+
expect(fsB.peers.size).to.equal(1)
227+
expectSet(first(fsB.peers).topics, ['Za'])
205228

206-
expectSet(psB.subscriptions, ['Zb'])
207-
expect(psA.peers.size).to.equal(1)
208-
expectSet(first(psA.peers).topics, ['Zb'])
229+
expectSet(fsB.subscriptions, ['Zb'])
230+
expect(fsA.peers.size).to.equal(1)
231+
expectSet(first(fsA.peers).topics, ['Zb'])
209232

210233
done()
211234
}, 250)
212235
})
213236
})
237+
238+
it('stop both FloodSubs', (done) => {
239+
parallel([
240+
(cb) => fsA.stop(cb),
241+
(cb) => fsB.stop(cb)
242+
], done)
243+
})
214244
})
215245
})
216246

test/multiple-nodes.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
const expect = require('chai').expect
66
const parallel = require('async/parallel')
77

8-
const PSG = require('../src')
8+
const FloodSub = require('../src')
99
const utils = require('./utils')
1010
const first = utils.first
1111
const createNode = utils.createNode
1212
const expectSet = utils.expectSet
1313

14-
describe('multiple nodes', () => {
14+
describe('multiple nodes (more than 2)', () => {
1515
describe('every peer subscribes to the topic', () => {
1616
describe('line', () => {
1717
// line
@@ -330,10 +330,15 @@ function spawnPubSubNode (callback) {
330330
if (err) {
331331
return callback(err)
332332
}
333-
334-
callback(null, {
335-
libp2p: node,
336-
ps: new PSG(node)
333+
const ps = new FloodSub(node)
334+
ps.start((err) => {
335+
if (err) {
336+
return callback(err)
337+
}
338+
callback(null, {
339+
libp2p: node,
340+
ps: ps
341+
})
337342
})
338343
})
339344
}

0 commit comments

Comments
 (0)