This repository has been 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.3k
/
Copy pathpubsub.js
165 lines (141 loc) · 4.38 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
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const series = require('async/series')
const parallel = require('async/parallel')
const GODaemon = require('../utils/interop-daemon-spawner/go')
const JSDaemon = require('../utils/interop-daemon-spawner/js')
/*
* Wait for a condition to become true. When its true, callback is called.
*/
function waitFor (predicate, callback) {
const ttl = Date.now() + (2 * 1000)
const self = setInterval(() => {
if (predicate()) {
clearInterval(self)
callback()
}
if (Date.now() > ttl) {
clearInterval(self)
callback(new Error("waitFor time expired"))
}
}, 500)
}
describe('pubsub', function () {
let jsD
let goD
let jsId
let goId
before(function (done) {
this.timeout(50 * 1000)
goD = new GODaemon({
disposable: true,
init: true,
flags: ['--enable-pubsub-experiment']
})
jsD = new JSDaemon()
parallel([
(cb) => goD.start(cb),
(cb) => jsD.start(cb)
], (done))
})
after((done) => {
parallel([
(cb) => goD.stop(cb),
(cb) => jsD.stop(cb)
], done)
})
it('make connections', (done) => {
series([
(cb) => jsD.api.id(cb),
(cb) => goD.api.id(cb)
], (err, ids) => {
expect(err).to.not.exist()
jsId = ids[0].id
goId = ids[1].id
const jsLocalAddr = ids[0].addresses.find(a => a.includes('127.0.0.1'))
const goLocalAddr = ids[1].addresses.find(a => a.includes('127.0.0.1'))
parallel([
(cb) => jsD.api.swarm.connect(goLocalAddr, cb),
(cb) => goD.api.swarm.connect(jsLocalAddr, cb)
], done)
})
})
it('publish from Go, subscribe on Go', (done) => {
const topic = 'pubsub-go-go'
const data = Buffer.from('hello world')
let n = 0
function checkMessage (msg) {
++n
expect(msg.data.toString()).to.equal(data.toString())
expect(msg).to.have.property('seqno')
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
// TODO: expect(msg).to.have.property('topicIDs').eql([topic])
expect(msg).to.have.property('from', goId)
}
series([
(cb) => goD.api.pubsub.subscribe(topic, checkMessage, cb),
(cb) => goD.api.pubsub.publish(topic, data, cb),
(cb) => waitFor(() => n === 1, cb)
], done)
})
it('publish from JS, subscribe on JS', (done) => {
const topic = 'pubsub-js-js'
const data = Buffer.from('hello world')
let n = 0
function checkMessage (msg) {
++n
expect(msg.data.toString()).to.equal(data.toString())
expect(msg).to.have.property('seqno')
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
expect(msg).to.have.property('topicIDs').eql([topic])
expect(msg).to.have.property('from', jsId)
}
series([
(cb) => jsD.api.pubsub.subscribe(topic, checkMessage, cb),
(cb) => jsD.api.pubsub.publish(topic, data, cb),
(cb) => waitFor(() => n === 1, cb)
], done)
})
it('publish from JS, subscribe on Go', (done) => {
const topic = 'pubsub-js-go'
const data = Buffer.from('hello world')
let n = 0
function checkMessage (msg) {
console.log('check message', msg)
++n
expect(msg.data.toString()).to.equal(data.toString())
expect(msg).to.have.property('seqno')
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
expect(msg).to.have.property('topicIDs').eql([topic])
expect(msg).to.have.property('from', jsId)
}
series([
(cb) => goD.api.pubsub.subscribe(topic, checkMessage, cb),
(cb) => jsD.api.pubsub.publish(topic, data, cb),
(cb) => waitFor(() => n === 1, cb)
], done)
})
it('publish from Go, subscribe on JS', (done) => {
const topic = 'pubsub-go-js'
const data = Buffer.from('hello world')
let n = 0
function checkMessage (msg) {
console.log('check message', msg)
++n
expect(msg.data.toString()).to.equal(data.toString())
expect(msg).to.have.property('seqno')
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
expect(msg).to.have.property('topicIDs').eql([topic])
expect(msg).to.have.property('from', goId)
}
series([
(cb) => jsD.api.pubsub.subscribe(topic, checkMessage, cb),
(cb) => goD.api.pubsub.publish(topic, data, cb),
(cb) => waitFor(() => n === 1, cb),
], done)
})
})