Skip to content

Commit 71cb905

Browse files
authored
Merge pull request #58 from alanshaw/patch-1
fix: parameter to _onPeerDisconnected
2 parents 8cc71a2 + 2754dcf commit 71cb905

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"lodash": "^4.17.15",
5959
"mocha": "^6.2.1",
6060
"p-times": "^2.1.0",
61+
"p-wait-for": "^3.1.0",
6162
"promisify-es6": "^1.0.3",
6263
"sinon": "^7.5.0"
6364
},

src/pubsub.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class BasicPubSub extends Pubsub {
9898
* @override
9999
* @param {string} idB58Str peer id string in base58
100100
* @param {Connection} conn connection
101-
* @param {PeerInfo} peer peer info
101+
* @param {Peer} peer PubSub peer
102102
* @returns {void}
103103
*
104104
*/
@@ -117,7 +117,7 @@ class BasicPubSub extends Pubsub {
117117
}
118118
)
119119
} catch (err) {
120-
this._onPeerDisconnected(peer, err)
120+
this._onPeerDisconnected(peer.info, err)
121121
}
122122
}
123123

test/pubsub.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ const chai = require('chai')
66
chai.use(require('dirty-chai'))
77
const expect = chai.expect
88
const sinon = require('sinon')
9+
const pWaitFor = require('p-wait-for')
910

1011
const { utils } = require('libp2p-pubsub')
1112
const {
1213
createGossipsub,
14+
createPeerInfo,
1315
mockRegistrar
1416
} = require('./utils')
1517

@@ -120,4 +122,34 @@ describe('Pubsub', () => {
120122
}, 500))
121123
})
122124
})
125+
126+
describe('process', () => {
127+
it('should disconnect peer on stream error', async () => {
128+
sinon.spy(gossipsub, '_onPeerDisconnected')
129+
130+
const peerInfo = await createPeerInfo()
131+
const mockConn = {
132+
newStream () {
133+
return {
134+
stream: {
135+
sink: async source => {
136+
for await (const _ of source) { // eslint-disable-line no-unused-vars
137+
// mock stream just swallows any data sent
138+
}
139+
},
140+
source: (async function * () { // eslint-disable-line require-yield
141+
// throw in a bit
142+
await new Promise(resolve => setTimeout(resolve, 100))
143+
throw new Error('boom')
144+
})()
145+
}
146+
}
147+
}
148+
}
149+
150+
gossipsub._onPeerConnected(peerInfo, mockConn)
151+
152+
await pWaitFor(() => gossipsub._onPeerDisconnected.calledWith(peerInfo), { timeout: 1000 })
153+
})
154+
})
123155
})

0 commit comments

Comments
 (0)