Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 0b9a483

Browse files
committed
test: add interface-core-api ping tests and fix impl
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent d82aa47 commit 0b9a483

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

src/ping-readable-stream.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ module.exports = (arg) => {
2121
const response = new PingMessageStream()
2222

2323
send(request, (err, stream) => {
24-
if (err) { return response.destroy(err) }
25-
24+
if (err) { return response.emit('error', err) }
2625
pump(stream, response)
2726
})
2827

src/ping.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const promisify = require('promisify-es6')
44
const pump = require('pump')
5-
const concat = require('concat-stream')
5+
const Writable = require('readable-stream').Writable
66
const moduleConfig = require('./utils/module-config')
77
const PingMessageStream = require('./utils/ping-message-stream')
88

@@ -31,16 +31,22 @@ module.exports = (arg) => {
3131
}
3232

3333
// Transform the response stream to a value:
34-
// [{ Success: <boolean>, Time: <number>, Text: <string> }]
34+
// [{ success: <boolean>, time: <number>, text: <string> }]
3535
const transform = (stream, callback) => {
3636
const messageConverter = new PingMessageStream()
37+
const responses = []
38+
3739
pump(
3840
stream,
3941
messageConverter,
40-
concat({encoding: 'object'}, (data) => callback(null, data)),
41-
(err) => {
42-
if (err) callback(err)
43-
}
42+
new Writable({
43+
objectMode: true,
44+
write (chunk, enc, cb) {
45+
responses.push(chunk)
46+
cb()
47+
}
48+
}),
49+
(err) => callback(err, responses)
4450
)
4551
}
4652

src/utils/ping-message-stream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class PingMessageStream extends TransformStream {
1313
try {
1414
const msg = pingMessageConverter(obj)
1515
this.push(msg)
16+
17+
if (!msg.success) {
18+
throw new Error(msg.text)
19+
}
1620
} catch (err) {
1721
return callback(err)
1822
}

test/interface/ping.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
'use strict'
4+
5+
const test = require('interface-ipfs-core')
6+
const parallel = require('async/parallel')
7+
8+
const IPFSApi = require('../../src')
9+
const f = require('../utils/factory')
10+
11+
const nodes = []
12+
const common = {
13+
setup: function (callback) {
14+
callback(null, {
15+
spawnNode: (cb) => {
16+
f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => {
17+
if (err) {
18+
return cb(err)
19+
}
20+
21+
nodes.push(_ipfsd)
22+
cb(null, IPFSApi(_ipfsd.apiAddr))
23+
})
24+
}
25+
})
26+
},
27+
teardown: function (callback) {
28+
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
29+
}
30+
}
31+
32+
test.ping(common)

0 commit comments

Comments
 (0)