This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpubsub.js
167 lines (144 loc) · 3.56 KB
/
pubsub.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const delay = require('delay')
const series = require('async/series')
const ipfsExec = require('../utils/ipfs-exec')
const IPFS = require('../../src')
const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create({ type: 'js' })
const config = {
Bootstrap: [],
Discovery: {
MDNS: {
Enabled:
false
}
}
}
describe('pubsub', function () {
this.timeout(80 * 1000)
let node
let ipfsdA
let ipfsdB
let cli
let httpApi
const topicA = 'nonscentsA'
const topicB = 'nonscentsB'
const topicC = 'nonscentsC'
before(function (done) {
this.timeout(60 * 1000)
DaemonFactory
.create({ type: 'proc' })
.spawn({
exec: IPFS,
config,
args: ['--enable-pubsub-experiment']
}, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsdA = _ipfsd
node = _ipfsd.api
done()
})
})
after((done) => ipfsdB.stop(done))
before((done) => {
df.spawn({
args: ['--enable-pubsub-experiment'],
exec: `./src/cli/bin.js`,
config
}, (err, _ipfsd) => {
expect(err).to.not.exist()
httpApi = _ipfsd.api
ipfsdB = _ipfsd
httpApi.repoPath = ipfsdB.repoPath
done()
})
})
after((done) => ipfsdA.stop(done))
after((done) => ipfsdB.stop(done))
before((done) => {
cli = ipfsExec(httpApi.repoPath)
done()
})
it('subscribe and publish', () => {
const sub = cli(`pubsub sub ${topicA}`)
sub.stdout.on('data', (c) => {
expect(c.toString().trim()).to.be.eql('world')
sub.kill()
})
return Promise.all([
sub.catch(ignoreKill),
delay(1000)
.then(() => cli(`pubsub pub ${topicA} world`))
.then((out) => {
expect(out).to.be.eql('')
})
])
})
it('ls', function () {
this.timeout(80 * 1000)
const sub = cli(`pubsub sub ${topicB}`)
sub.stdout.once('data', (data) => {
expect(data.toString().trim()).to.be.eql('world')
cli('pubsub ls')
.then((out) => {
expect(out.trim()).to.be.eql(topicB)
sub.kill()
})
})
return Promise.all([
sub.catch(ignoreKill),
delay(200)
.then(() => cli(`pubsub pub ${topicB} world`))
])
})
it('peers', (done) => {
let sub
let instancePeerId
let peerAddress
const handler = (msg) => {
expect(msg.data.toString()).to.be.eql('world')
cli(`pubsub peers ${topicC}`)
.then((out) => {
expect(out.trim()).to.be.eql(instancePeerId)
sub.kill()
node.pubsub.unsubscribe(topicC, handler)
done()
})
}
series([
(cb) => httpApi.id((err, peerInfo) => {
expect(err).to.not.exist()
peerAddress = peerInfo.addresses[0]
expect(peerAddress).to.exist()
cb()
}),
(cb) => node.id((err, peerInfo) => {
expect(err).to.not.exist()
instancePeerId = peerInfo.id.toString()
cb()
}),
(cb) => node.swarm.connect(peerAddress, cb),
(cb) => node.pubsub.subscribe(topicC, handler, cb)
],
(err) => {
expect(err).to.not.exist()
sub = cli(`pubsub sub ${topicC}`)
return Promise.all([
sub.catch(ignoreKill),
delay(1000)
.then(() => cli(`pubsub pub ${topicC} world`))
])
})
})
})
function ignoreKill (err) {
if (!err.killed) {
throw err
}
}