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

fix: remove antipattern from ping tests #386

Merged
merged 2 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions js/src/ping/ping-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = (createCommon, options) => {
const common = createCommon()

describe('.pingPullStream', function () {
this.timeout(60 * 1000)
this.timeout(30 * 1000)

let ipfsA
let ipfsB
Expand Down Expand Up @@ -61,37 +61,23 @@ module.exports = (createCommon, options) => {
})

it('should fail when pinging an unknown peer over pull stream', (done) => {
let messageNum = 0
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const count = 2
pull(
ipfsA.pingPullStream(unknownPeerId, { count }),
pull.drain((res) => {
expectIsPingResponse(res)
messageNum++

// First message should be "looking up" response
if (messageNum === 1) {
expect(res.text).to.include('Looking up')
}

// Second message should be a failure response
if (messageNum === 2) {
expect(res.success).to.be.false()
}
}, (err) => {
pull.collect((err, results) => {
expect(err).to.exist()
done()
})
)
})

it('should fail when pinging an invalid peer over pull stream', (done) => {
it('should fail when pinging an invalid peer id over pull stream', (done) => {
const invalidPeerId = 'not a peer ID'
const count = 2
pull(
ipfsA.pingPullStream(invalidPeerId, { count }),
pull.collect((err) => {
pull.collect((err, results) => {
expect(err).to.exist()
expect(err.message).to.include('failed to parse peer address')
done()
Expand Down
30 changes: 6 additions & 24 deletions js/src/ping/ping-readable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = (createCommon, options) => {
const common = createCommon()

describe('.pingReadableStream', function () {
this.timeout(60 * 1000)
this.timeout(30 * 1000)

let ipfsA
let ipfsB
Expand Down Expand Up @@ -68,31 +68,14 @@ module.exports = (createCommon, options) => {
)
})

it('should fail when pinging an unknown peer over readable stream', (done) => {
let messageNum = 0
it('should fail when pinging peer that is not available over readable stream', (done) => {
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const count = 2

pump(
ipfsA.pingReadableStream(unknownPeerId, { count }),
ipfsA.pingReadableStream(unknownPeerId, {}),
new Writable({
objectMode: true,
write (res, enc, cb) {
expectIsPingResponse(res)
messageNum++

// First message should be "looking up" response
if (messageNum === 1) {
expect(res.text).to.include('Looking up')
}

// Second message should be a failure response
if (messageNum === 2) {
expect(res.success).to.be.false()
}

cb()
}
write: (res, enc, cb) => cb()
}),
(err) => {
expect(err).to.exist()
Expand All @@ -101,12 +84,11 @@ module.exports = (createCommon, options) => {
)
})

it('should fail when pinging an invalid peer over readable stream', (done) => {
it('should fail when pinging an invalid peer id over readable stream', (done) => {
const invalidPeerId = 'not a peer ID'
const count = 2

pump(
ipfsA.pingReadableStream(invalidPeerId, { count }),
ipfsA.pingReadableStream(invalidPeerId, {}),
new Writable({
objectMode: true,
write: (chunk, enc, cb) => cb()
Expand Down
13 changes: 6 additions & 7 deletions js/src/ping/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = (createCommon, options) => {
const common = createCommon()

describe('.ping', function () {
this.timeout(60 * 1000)
this.timeout(30 * 1000)

let ipfsA
let ipfsB
Expand Down Expand Up @@ -51,24 +51,23 @@ module.exports = (createCommon, options) => {
})
})

it('should fail when pinging an unknown peer', (done) => {
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
it('should fail when pinging a peer that is not available', (done) => {
const notAvailablePeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const count = 2

ipfsA.ping(unknownPeerId, { count }, (err, responses) => {
ipfsA.ping(notAvailablePeerId, { count }, (err, responses) => {
expect(err).to.exist()
expect(responses[0].text).to.include('Looking up')
expect(responses[1].success).to.be.false()
done()
})
})

it('should fail when pinging an invalid peer', (done) => {
it('should fail when pinging an invalid peer Id', (done) => {
const invalidPeerId = 'not a peer ID'
const count = 2
ipfsA.ping(invalidPeerId, { count }, (err, responses) => {
expect(err).to.exist()
expect(err.message).to.include('failed to parse peer address')
expect(responses).to.not.exist()
done()
})
})
Expand Down