Skip to content

fix: parameter to _onPeerDisconnected #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 22, 2020
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"lodash": "^4.17.15",
"mocha": "^6.2.1",
"p-times": "^2.1.0",
"p-wait-for": "^3.1.0",
"promisify-es6": "^1.0.3",
"sinon": "^7.5.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class BasicPubSub extends Pubsub {
* @override
* @param {string} idB58Str peer id string in base58
* @param {Connection} conn connection
* @param {PeerInfo} peer peer info
* @param {Peer} peer PubSub peer
* @returns {void}
*
*/
Expand All @@ -117,7 +117,7 @@ class BasicPubSub extends Pubsub {
}
)
} catch (err) {
this._onPeerDisconnected(peer, err)
this._onPeerDisconnected(peer.info, err)
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/pubsub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const sinon = require('sinon')
const pWaitFor = require('p-wait-for')

const { utils } = require('libp2p-pubsub')
const {
createGossipsub,
createPeerInfo,
mockRegistrar
} = require('./utils')

Expand Down Expand Up @@ -120,4 +122,34 @@ describe('Pubsub', () => {
}, 500))
})
})

describe('process', () => {
it('should disconnect peer on stream error', async () => {
sinon.spy(gossipsub, '_onPeerDisconnected')

const peerInfo = await createPeerInfo()
const mockConn = {
newStream () {
return {
stream: {
sink: async source => {
for await (const _ of source) { // eslint-disable-line no-unused-vars
// mock stream just swallows any data sent
}
},
source: (async function * () { // eslint-disable-line require-yield
// throw in a bit
await new Promise(resolve => setTimeout(resolve, 100))
throw new Error('boom')
})()
}
}
}
}

gossipsub._onPeerConnected(peerInfo, mockConn)

await pWaitFor(() => gossipsub._onPeerDisconnected.calledWith(peerInfo), { timeout: 1000 })
})
})
})