Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit bc0e1bd

Browse files
richardschneiderdaviddias
authored andcommitted
test: tests js/go pubsub interop
1 parent b12e688 commit bc0e1bd

File tree

4 files changed

+108
-21
lines changed

4 files changed

+108
-21
lines changed

test/interop/pubsub.js

Lines changed: 101 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@ const parallel = require('async/parallel')
1111
const GODaemon = require('../utils/interop-daemon-spawner/go')
1212
const JSDaemon = require('../utils/interop-daemon-spawner/js')
1313

14-
describe('pubsub', () => {
14+
/*
15+
* Wait for a condition to become true. When its true, callback is called.
16+
*/
17+
function waitFor (predicate, callback) {
18+
const ttl = Date.now() + (2 * 1000)
19+
const self = setInterval(() => {
20+
if (predicate()) {
21+
clearInterval(self)
22+
callback()
23+
}
24+
if (Date.now() > ttl) {
25+
clearInterval(self)
26+
callback(new Error("waitFor time expired"))
27+
}
28+
}, 500)
29+
}
30+
31+
describe('pubsub', function () {
1532
let jsD
1633
let goD
1734
let jsId
@@ -34,47 +51,115 @@ describe('pubsub', () => {
3451
})
3552

3653
after((done) => {
37-
series([
54+
parallel([
3855
(cb) => goD.stop(cb),
3956
(cb) => jsD.stop(cb)
4057
], done)
4158
})
4259

4360
it('make connections', (done) => {
44-
parallel([
61+
series([
4562
(cb) => jsD.api.id(cb),
4663
(cb) => goD.api.id(cb)
4764
], (err, ids) => {
4865
expect(err).to.not.exist()
4966

50-
jsId = ids[0].ID
51-
goId = ids[0].ID
67+
jsId = ids[0].id
68+
goId = ids[1].id
5269

53-
console.log('jsId:', jsId)
54-
console.log('goId:', goId)
70+
const jsLocalAddr = ids[0].addresses.find(a => a.includes('127.0.0.1'))
71+
const goLocalAddr = ids[1].addresses.find(a => a.includes('127.0.0.1'))
5572

5673
parallel([
57-
(cb) => jsD.api.swarm.connect(ids[1].addresses[0], cb),
58-
(cb) => goD.api.swarm.connect(ids[0].addresses[0], cb)
74+
(cb) => jsD.api.swarm.connect(goLocalAddr, cb),
75+
(cb) => goD.api.swarm.connect(jsLocalAddr, cb)
5976
], done)
6077
})
6178
})
6279

63-
it.skip('publish from JS, subscribe on Go', (done) => {
64-
// TODO write this test
80+
it('publish from Go, subscribe on Go', (done) => {
81+
const topic = 'pubsub-go-go'
82+
const data = Buffer.from('hello world')
83+
let n = 0
84+
85+
function checkMessage (msg) {
86+
++n
87+
expect(msg.data.toString()).to.equal(data.toString())
88+
expect(msg).to.have.property('seqno')
89+
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
90+
// TODO: expect(msg).to.have.property('topicIDs').eql([topic])
91+
expect(msg).to.have.property('from', goId)
92+
}
93+
94+
series([
95+
(cb) => goD.api.pubsub.subscribe(topic, checkMessage, cb),
96+
(cb) => goD.api.pubsub.publish(topic, data, cb),
97+
(cb) => waitFor(() => n === 1, cb)
98+
], done)
99+
})
100+
101+
it('publish from JS, subscribe on JS', (done) => {
102+
const topic = 'pubsub-js-js'
103+
const data = Buffer.from('hello world')
104+
let n = 0
105+
106+
function checkMessage (msg) {
107+
++n
108+
expect(msg.data.toString()).to.equal(data.toString())
109+
expect(msg).to.have.property('seqno')
110+
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
111+
expect(msg).to.have.property('topicIDs').eql([topic])
112+
expect(msg).to.have.property('from', jsId)
113+
}
114+
115+
series([
116+
(cb) => jsD.api.pubsub.subscribe(topic, checkMessage, cb),
117+
(cb) => jsD.api.pubsub.publish(topic, data, cb),
118+
(cb) => waitFor(() => n === 1, cb)
119+
], done)
120+
})
121+
122+
it('publish from JS, subscribe on Go', (done) => {
123+
const topic = 'pubsub-js-go'
124+
const data = Buffer.from('hello world')
125+
let n = 0
126+
127+
function checkMessage (msg) {
128+
console.log('check message', msg)
129+
++n
130+
expect(msg.data.toString()).to.equal(data.toString())
131+
expect(msg).to.have.property('seqno')
132+
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
133+
expect(msg).to.have.property('topicIDs').eql([topic])
134+
expect(msg).to.have.property('from', jsId)
135+
}
136+
137+
series([
138+
(cb) => goD.api.pubsub.subscribe(topic, checkMessage, cb),
139+
(cb) => jsD.api.pubsub.publish(topic, data, cb),
140+
(cb) => waitFor(() => n === 1, cb)
141+
], done)
65142
})
66143

67-
it.skip('publish from Go, subscribe on JS', (done) => {
144+
it('publish from Go, subscribe on JS', (done) => {
68145
const topic = 'pubsub-go-js'
69146
const data = Buffer.from('hello world')
147+
let n = 0
70148

71-
function checkMessage () {
72-
console.log('check message', arguments)
149+
function checkMessage (msg) {
150+
console.log('check message', msg)
151+
++n
152+
expect(msg.data.toString()).to.equal(data.toString())
153+
expect(msg).to.have.property('seqno')
154+
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
155+
expect(msg).to.have.property('topicIDs').eql([topic])
156+
expect(msg).to.have.property('from', goId)
73157
}
74158

75159
series([
76-
cb => jsD.api.pubsub.subscribe(topic, checkMessage, cb),
77-
cb => goD.api.pubsub.publish(topic, data, cb)
160+
(cb) => jsD.api.pubsub.subscribe(topic, checkMessage, cb),
161+
(cb) => goD.api.pubsub.publish(topic, data, cb),
162+
(cb) => waitFor(() => n === 1, cb),
78163
], done)
79164
})
80165
})

test/utils/interop-daemon-spawner/go.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class GoDaemon {
4040
this.node = node
4141
this.node.setConfig('Bootstrap', '[]', cb)
4242
},
43-
(res, cb) => this.node.startDaemon(cb),
44-
// (res, cb) => this.node.startDaemon(this.flags, cb),
43+
(res, cb) => this.node.startDaemon(this.flags, cb),
4544
(api, cb) => {
4645
this.api = api
4746

test/utils/interop-daemon-spawner/js.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ class JsDaemon extends EventEmitter {
3737
this.path = opts.path || tmpDir()
3838
this._started = false
3939

40+
const extras = {
41+
enablePubsubExperiment: true
42+
}
4043
if (this.init) {
4144
const p = portConfig(this.port)
4245
this.node = new HttpApi(this.path, {
4346
Bootstrap: [],
4447
Addresses: p
45-
})
48+
}, extras)
4649
} else {
47-
this.node = new HttpApi(this.path)
50+
this.node = new HttpApi(this.path, null, extras)
4851
}
4952

5053
this.node.start(this.init, (err) => {

test/utils/interop-daemon-spawner/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const path = require('path')
77
exports.tmpDir = (prefix) => {
88
return path.join(
99
os.tmpdir(),
10-
prefix || 'tmp',
10+
prefix || 'js-ipfs-interop',
1111
crypto.randomBytes(32).toString('hex')
1212
)
1313
}

0 commit comments

Comments
 (0)