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

Commit fb726a7

Browse files
richardschneiderdaviddias
authored andcommitted
test: pubsub interop tests
1 parent bc0e1bd commit fb726a7

File tree

4 files changed

+326
-7
lines changed

4 files changed

+326
-7
lines changed

test/interop/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ require('./exchange-files')
66
require('./circuit-relay')
77
require('./kad-dht')
88
require('./pubsub')
9+
require('./pubsub-go')

test/interop/pubsub-go.js

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
const series = require('async/series')
9+
const parallel = require('async/parallel')
10+
11+
const GODaemon = require('../utils/interop-daemon-spawner/go')
12+
13+
/*
14+
* Wait for a condition to become true. When its true, callback is called.
15+
*/
16+
function waitFor (predicate, callback) {
17+
const ttl = Date.now() + (2 * 1000)
18+
const self = setInterval(() => {
19+
if (predicate()) {
20+
clearInterval(self)
21+
return callback()
22+
}
23+
if (Date.now() > ttl) {
24+
clearInterval(self)
25+
return callback(new Error("waitFor time expired"))
26+
}
27+
}, 500)
28+
}
29+
30+
describe('pubsub GO 2 GO', function () {
31+
this.timeout(4 * 1000)
32+
33+
let go1D
34+
let go2D
35+
let go1Id
36+
let go2Id
37+
38+
before(function (done) {
39+
this.timeout(50 * 1000)
40+
41+
go1D = new GODaemon({
42+
disposable: true,
43+
init: true,
44+
flags: ['--enable-pubsub-experiment']
45+
})
46+
go2D = new GODaemon({
47+
disposable: true,
48+
init: true,
49+
flags: ['--enable-pubsub-experiment']
50+
})
51+
52+
parallel([
53+
(cb) => go1D.start(cb),
54+
(cb) => go2D.start(cb)
55+
], (done))
56+
})
57+
58+
after((done) => {
59+
parallel([
60+
(cb) => go1D.stop(cb),
61+
(cb) => go2D.stop(cb)
62+
], done)
63+
})
64+
65+
it('make connections', (done) => {
66+
series([
67+
(cb) => go1D.api.id(cb),
68+
(cb) => go2D.api.id(cb)
69+
], (err, ids) => {
70+
expect(err).to.not.exist()
71+
72+
go1Id = ids[0].id
73+
go2Id = ids[1].id
74+
75+
const go1LocalAddr = ids[0].addresses.find(a => a.includes('127.0.0.1'))
76+
const go2LocalAddr = ids[1].addresses.find(a => a.includes('127.0.0.1'))
77+
78+
parallel([
79+
(cb) => go1D.api.swarm.connect(go2LocalAddr, cb),
80+
(cb) => go2D.api.swarm.connect(go1LocalAddr, cb),
81+
(cb) => setTimeout(() => {
82+
cb()
83+
}, 1000)
84+
], done)
85+
})
86+
})
87+
88+
it('publish from Go2, subscribe on Go1', (done) => {
89+
const topic = 'pubsub-go2-go1'
90+
const data = Buffer.from('hello world')
91+
let n = 0
92+
93+
function checkMessage (msg) {
94+
++n
95+
expect(msg.data.toString()).to.equal(data.toString())
96+
expect(msg).to.have.property('seqno')
97+
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
98+
expect(msg).to.have.property('topicIDs').eql([topic])
99+
expect(msg).to.have.property('from', go2Id)
100+
}
101+
102+
series([
103+
(cb) => go1D.api.pubsub.subscribe(topic, checkMessage, cb),
104+
(cb) => setTimeout(() => { cb() }, 500),
105+
(cb) => go2D.api.pubsub.publish(topic, data, cb),
106+
(cb) => go2D.api.pubsub.publish(topic, data, cb),
107+
(cb) => go2D.api.pubsub.publish(topic, data, cb),
108+
(cb) => waitFor(() => n === 3, cb)
109+
], done)
110+
})
111+
112+
it('publish binary data', (done) => {
113+
const topic = 'pubsub-binary-go1-go2'
114+
const data = Buffer.from('00010203040506070809', 'hex')
115+
let n = 0
116+
117+
function checkMessage (msg) {
118+
++n
119+
expect(msg.data.toString('hex')).to.equal(data.toString('hex'))
120+
expect(msg).to.have.property('seqno')
121+
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
122+
expect(msg).to.have.property('topicIDs').eql([topic])
123+
expect(msg).to.have.property('from', go2Id)
124+
}
125+
126+
series([
127+
(cb) => go1D.api.pubsub.subscribe(topic, checkMessage, cb),
128+
(cb) => setTimeout(() => { cb() }, 500),
129+
(cb) => go2D.api.pubsub.publish(topic, data, cb),
130+
(cb) => waitFor(() => n === 1, cb)
131+
], done)
132+
})
133+
134+
})

test/interop/pubsub-js.js

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
const series = require('async/series')
9+
const parallel = require('async/parallel')
10+
11+
const JSDaemon = require('../utils/interop-daemon-spawner/js')
12+
13+
/*
14+
* Wait for a condition to become true. When its true, callback is called.
15+
*/
16+
function waitFor (predicate, callback) {
17+
const ttl = Date.now() + (2 * 1000)
18+
const self = setInterval(() => {
19+
if (predicate()) {
20+
clearInterval(self)
21+
return callback()
22+
}
23+
if (Date.now() > ttl) {
24+
clearInterval(self)
25+
return callback(new Error("waitFor time expired"))
26+
}
27+
}, 500)
28+
}
29+
30+
describe('pubsub JS 2 JS', function () {
31+
this.timeout(4 * 1000)
32+
33+
let js1D
34+
let js2D
35+
let js1Id
36+
let js2Id
37+
38+
before(function (done) {
39+
this.timeout(50 * 1000)
40+
41+
js1D = new JSDaemon()
42+
js2D = new JSDaemon({
43+
disposable: true,
44+
init: true,
45+
port: 2
46+
})
47+
48+
parallel([
49+
(cb) => js1D.start(cb),
50+
(cb) => js2D.start(cb)
51+
], (done))
52+
})
53+
54+
after(function (done) {
55+
this.timeout(10 * 1000)
56+
57+
parallel([
58+
(cb) => js1D.stop(cb),
59+
(cb) => js2D.stop(cb)
60+
], done)
61+
})
62+
63+
it('make connections', (done) => {
64+
series([
65+
(cb) => js1D.api.id(cb),
66+
(cb) => js2D.api.id(cb)
67+
], (err, ids) => {
68+
expect(err).to.not.exist()
69+
70+
js1Id = ids[0].id
71+
js2Id = ids[1].id
72+
73+
const js1LocalAddr = ids[0].addresses.find(a => a.includes('127.0.0.1'))
74+
const js2LocalAddr = ids[1].addresses.find(a => a.includes('127.0.0.1'))
75+
76+
parallel([
77+
(cb) => js1D.api.swarm.connect(js2LocalAddr, cb),
78+
(cb) => js2D.api.swarm.connect(js1LocalAddr, cb),
79+
(cb) => setTimeout(() => {
80+
cb()
81+
}, 1000)
82+
], done)
83+
})
84+
})
85+
86+
it('publish from JS2, subscribe on JS1', (done) => {
87+
const topic = 'pubsub-js2-js1'
88+
const data = Buffer.from('hello world')
89+
let n = 0
90+
91+
function checkMessage (msg) {
92+
++n
93+
expect(msg.data.toString()).to.equal(data.toString())
94+
expect(msg).to.have.property('seqno')
95+
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
96+
expect(msg).to.have.property('topicIDs').eql([topic])
97+
expect(msg).to.have.property('from', js2Id)
98+
}
99+
100+
series([
101+
(cb) => js1D.api.pubsub.subscribe(topic, checkMessage, cb),
102+
(cb) => setTimeout(() => { cb() }, 500),
103+
(cb) => js2D.api.pubsub.publish(topic, data, cb),
104+
(cb) => js2D.api.pubsub.publish(topic, data, cb),
105+
(cb) => js2D.api.pubsub.publish(topic, data, cb),
106+
(cb) => waitFor(() => n === 3, cb)
107+
], done)
108+
})
109+
110+
it('publish binary data', (done) => {
111+
const topic = 'pubsub-binary-js1-js2'
112+
const data = Buffer.from('00010203040506070809', 'hex')
113+
let n = 0
114+
115+
function checkMessage (msg) {
116+
++n
117+
expect(msg.data.toString('hex')).to.equal(data.toString('hex'))
118+
expect(msg).to.have.property('seqno')
119+
expect(Buffer.isBuffer(msg.seqno)).to.be.eql(true)
120+
expect(msg).to.have.property('topicIDs').eql([topic])
121+
expect(msg).to.have.property('from', js2Id)
122+
}
123+
124+
series([
125+
(cb) => js1D.api.pubsub.subscribe(topic, checkMessage, cb),
126+
(cb) => setTimeout(() => { cb() }, 500),
127+
(cb) => js2D.api.pubsub.publish(topic, data, cb),
128+
(cb) => waitFor(() => n === 1, cb)
129+
], done)
130+
})
131+
132+
})

0 commit comments

Comments
 (0)