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

Commit

Permalink
fix(ping): tests were failing and there it was missing to catch when …
Browse files Browse the repository at this point in the history
…count and n are used at the same time
  • Loading branch information
daviddias committed Mar 27, 2018
1 parent 427322d commit 2181568
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 210 deletions.
6 changes: 6 additions & 0 deletions src/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ module.exports = (arg) => {
callback = opts
opts = {}
}

if (opts.n && opts.count) {
return callback(new Error('Use either n or count, not both'))
}

// Default number of packtes to 1
if (!opts.n && !opts.count) {
opts.n = 1
}

const request = {
path: 'ping',
args: id,
Expand Down
289 changes: 79 additions & 210 deletions test/ping.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('.ping', function () {
})
},
(cb) => {
console.log('going to spawn second node')
f.spawn({ initOptions: { bits: 1024 } }, (err, node) => {
expect(err).to.not.exist()
other = node.api
Expand Down Expand Up @@ -66,236 +65,106 @@ describe('.ping', function () {
], done)
})

describe('callback API', () => {
it('ping another peer with default packet count', (done) => {
ipfs.ping(otherId, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(3)
res.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
it('.ping with default n', (done) => {
ipfs.ping(otherId, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(3)
res.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
})

it('ping another peer with a specifc packet count through parameter count', (done) => {
ipfs.ping(otherId, {count: 3}, (err, res) => {
expect(err).to.not.exist()
it('.ping with count = 2', (done) => {
ipfs.ping(otherId, { count: 2 }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(4)
res.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
})

it('.ping with n = 2', (done) => {
ipfs.ping(otherId, { n: 2 }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(4)
res.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
})

it('.ping fails with count & n', function (done) {
this.timeout(20 * 1000)

ipfs.ping(otherId, {count: 2, n: 2}, (err, res) => {
expect(err).to.exist()
done()
})
})

it('.ping with Promises', () => {
return ipfs.ping(otherId)
.then((res) => {
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(5)
expect(res).to.have.lengthOf(3)
res.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
})
})

it('ping another peer with a specifc packet count through parameter n', (done) => {
ipfs.ping(otherId, {n: 3}, (err, res) => {
it('.pingPullStream', (done) => {
pull(
ipfs.pingPullStream(otherId),
collect((err, data) => {
expect(err).to.not.exist()
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(5)
res.forEach(packet => {
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(3)
data.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
const resultMsg = data.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
})
)
})

it('sending both n and count should fail', (done) => {
ipfs.ping(otherId, {count: 10, n: 10}, (err, res) => {
expect(err).to.exist()
it('.pingReadableStream', (done) => {
let packetNum = 0
ipfs.pingReadableStream(otherId)
.on('data', data => {
packetNum++
expect(data).to.be.an('object')
expect(data).to.have.keys('Success', 'Time', 'Text')
})
.on('error', err => {
expect(err).not.to.exist()
})
.on('end', () => {
expect(packetNum).to.equal(3)
done()
})
})
})

describe('promise API', () => {
it('ping another peer with default packet count', () => {
return ipfs.ping(otherId)
.then((res) => {
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(3)
res.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
})
})

it('ping another peer with a specifc packet count through parameter count', () => {
return ipfs.ping(otherId, {count: 3})
.then((res) => {
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(5)
res.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
})
})

it('ping another peer with a specifc packet count through parameter n', () => {
return ipfs.ping(otherId, {n: 3})
.then((res) => {
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(5)
res.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
})
})

it('sending both n and count should fail', (done) => {
ipfs.ping(otherId, {n: 3, count: 3})
.catch(err => {
expect(err).to.exist()
done()
})
})
})

describe('pull stream API', () => {
it('ping another peer with the default packet count', (done) => {
pull(
ipfs.pingPullStream(otherId),
collect((err, data) => {
expect(err).to.not.exist()
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(3)
data.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = data.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
)
})

it('ping another peer with a specifc packet count through parameter count', (done) => {
pull(
ipfs.pingPullStream(otherId, {count: 3}),
collect((err, data) => {
expect(err).to.not.exist()
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(5)
data.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = data.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
)
})

it('ping another peer with a specifc packet count through parameter n', (done) => {
pull(
ipfs.pingPullStream(otherId, {n: 3}),
collect((err, data) => {
expect(err).to.not.exist()
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(5)
data.forEach(packet => {
expect(packet).to.have.keys('Success', 'Time', 'Text')
expect(packet.Time).to.be.a('number')
})
const resultMsg = data.find(packet => packet.Text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
)
})

it('sending both n and count should fail', (done) => {
pull(
ipfs.pingPullStream(otherId, {n: 3, count: 3}),
collect(err => {
expect(err).to.exist()
done()
})
)
})
})

describe('readable stream API', () => {
it('ping another peer with the default packet count', (done) => {
let packetNum = 0
ipfs.pingReadableStream(otherId)
.on('data', data => {
packetNum++
expect(data).to.be.an('object')
expect(data).to.have.keys('Success', 'Time', 'Text')
})
.on('error', err => {
expect(err).not.to.exist()
})
.on('end', () => {
expect(packetNum).to.equal(3)
done()
})
})

it('ping another peer with a specifc packet count through parameter count', (done) => {
let packetNum = 0
ipfs.pingReadableStream(otherId, {count: 3})
.on('data', data => {
packetNum++
expect(data).to.be.an('object')
expect(data).to.have.keys('Success', 'Time', 'Text')
})
.on('error', err => {
expect(err).not.to.exist()
})
.on('end', () => {
expect(packetNum).to.equal(5)
done()
})
})

it('ping another peer with a specifc packet count through parameter n', (done) => {
let packetNum = 0
ipfs.pingReadableStream(otherId, {n: 3})
.on('data', data => {
packetNum++
expect(data).to.be.an('object')
expect(data).to.have.keys('Success', 'Time', 'Text')
})
.on('error', err => {
expect(err).not.to.exist()
})
.on('end', () => {
expect(packetNum).to.equal(5)
done()
})
})

it('sending both n and count should fail', (done) => {
ipfs.pingReadableStream(otherId, {n: 3, count: 3})
.on('error', err => {
expect(err).to.exist()
done()
})
})
})
})

0 comments on commit 2181568

Please sign in to comment.