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

Commit 789ef7e

Browse files
test(interop): node.js basics
1 parent c1ad90f commit 789ef7e

File tree

5 files changed

+167
-2
lines changed

5 files changed

+167
-2
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"test:unit:node:http": "TEST=http npm run test:node",
2727
"test:unit:node:cli": "TEST=cli npm run test:node",
2828
"test:unit:browser": "gulp test:browser",
29-
"test:interop": "mocha test/interop",
30-
"test:interop:node": "mocha test/interop/node.js",
29+
"test:interop": "mocha -t 60000 test/interop",
30+
"test:interop:node": "mocha -t 60000 test/interop/node.js",
3131
"test:interop:browser": "mocha test/interop/browser.js",
3232
"test:benchmark": "echo \"Error: no benchmarks yet\" && exit 1",
3333
"test:benchmark:node": "echo \"Error: no benchmarks yet\" && exit 1",

test/interop/daemons/go.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const ctl = require('ipfsd-ctl')
4+
const waterfall = require('async/waterfall')
5+
6+
class GoDaemon {
7+
constructor () {
8+
this.node = null
9+
this.api = null
10+
}
11+
12+
start (callback) {
13+
console.log('starting go')
14+
waterfall([
15+
(cb) => ctl.disposable(cb),
16+
(node, cb) => {
17+
this.node = node
18+
this.node.startDaemon(cb)
19+
},
20+
(api, cb) => {
21+
this.api = api
22+
cb()
23+
}
24+
], callback)
25+
}
26+
27+
stop (callback) {
28+
this.node.stopDaemon(callback)
29+
}
30+
}
31+
32+
module.exports = GoDaemon

test/interop/daemons/js.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
const os = require('os')
4+
const IPFSAPI = require('ipfs-api')
5+
const series = require('async/series')
6+
const rimraf = require('rimraf')
7+
8+
const IPFS = require('../../../src/core')
9+
const HTTPAPI = require('../../../src/http-api')
10+
11+
class JsDaemon {
12+
constructor () {
13+
this.repoPath = os.tmpdir() + `${Math.ceil(Math.random() * 100)}`
14+
this.ipfs = new IPFS(this.repoPath)
15+
this.node = null
16+
this.api = null
17+
}
18+
19+
start (callback) {
20+
console.log('starting js', this.repoPath)
21+
series([
22+
(cb) => this.ipfs.init(cb),
23+
(cb) => {
24+
this.node = new HTTPAPI(this.ipfs._repo)
25+
this.node.start(cb)
26+
},
27+
(cb) => {
28+
this.api = new IPFSAPI(this.node.apiMultiaddr)
29+
cb()
30+
}
31+
], callback)
32+
}
33+
34+
stop (callback) {
35+
series([
36+
(cb) => this.node.stop(cb),
37+
(cb) => rimraf(this.repoPath, cb)
38+
], callback)
39+
}
40+
}
41+
42+
module.exports = JsDaemon

test/interop/index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const series = require('async/series')
6+
const parallel = require('async/parallel')
7+
const waterfall = require('async/waterfall')
8+
const bl = require('bl')
9+
const crypto = require('crypto')
10+
11+
const GoDaemon = require('./daemons/go')
12+
const JsDaemon = require('./daemons/js')
13+
14+
describe('interop', () => {
15+
let goDaemon
16+
let jsDaemon
17+
18+
before((done) => {
19+
goDaemon = new GoDaemon()
20+
jsDaemon = new JsDaemon()
21+
22+
parallel([
23+
(cb) => goDaemon.start(cb),
24+
(cb) => jsDaemon.start(cb)
25+
], done)
26+
})
27+
28+
after((done) => {
29+
parallel([
30+
(cb) => goDaemon.stop(cb),
31+
(cb) => jsDaemon.stop(cb)
32+
], done)
33+
})
34+
35+
it('connect go <-> js', (done) => {
36+
let jsId
37+
let goId
38+
39+
series([
40+
(cb) => parallel([
41+
(cb) => jsDaemon.api.id(cb),
42+
(cb) => goDaemon.api.id(cb)
43+
], (err, ids) => {
44+
expect(err).to.not.exist
45+
jsId = ids[0]
46+
goId = ids[1]
47+
cb()
48+
}),
49+
(cb) => goDaemon.api.swarm.connect(jsId.addresses[0], cb),
50+
(cb) => jsDaemon.api.swarm.connect(goId.addresses[0], cb),
51+
(cb) => parallel([
52+
(cb) => goDaemon.api.swarm.peers(cb),
53+
(cb) => jsDaemon.api.swarm.peers(cb)
54+
], (err, peers) => {
55+
expect(err).to.not.exist
56+
expect(peers[0].map((p) => p.peer.toB58String())).to.include(jsId.id)
57+
expect(peers[1].map((p) => p.peer.toB58String())).to.include(goId.id)
58+
cb()
59+
})
60+
], done)
61+
})
62+
63+
it('cat file: go -> js', (done) => {
64+
const data = crypto.randomBytes(1024 * 5)
65+
waterfall([
66+
(cb) => goDaemon.api.add(data, cb),
67+
(res, cb) => jsDaemon.api.cat(res[0].hash, cb),
68+
(stream, cb) => stream.pipe(bl(cb))
69+
], (err, file) => {
70+
expect(err).to.not.exist
71+
expect(file).to.be.eql(data)
72+
done()
73+
})
74+
})
75+
76+
it('cat file: js -> go', (done) => {
77+
const data = crypto.randomBytes(1024 * 5)
78+
waterfall([
79+
(cb) => jsDaemon.api.add(data, cb),
80+
(res, cb) => goDaemon.api.cat(res[0].hash, cb),
81+
(stream, cb) => stream.pipe(bl(cb))
82+
], (err, file) => {
83+
expect(err).to.not.exist
84+
expect(file).to.be.eql(data)
85+
done()
86+
})
87+
})
88+
})

test/interop/node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
require('./index')

0 commit comments

Comments
 (0)