diff --git a/package.json b/package.json index d8db9a06a..32134ea76 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "libp2p-tcp": "^0.14.1", "multiaddr": "^7.1.0", "p-limit": "^2.2.1", + "p-wait-for": "^3.1.0", "peer-id": "^0.13.3", "sinon": "^7.5.0", "streaming-iterables": "^4.1.0" diff --git a/src/transport/tests/listen-test.js b/src/transport/tests/listen-test.js index a45519519..68dcd2a9b 100644 --- a/src/transport/tests/listen-test.js +++ b/src/transport/tests/listen-test.js @@ -8,6 +8,7 @@ const expect = chai.expect chai.use(dirtyChai) const sinon = require('sinon') +const pWaitFor = require('p-wait-for') const pipe = require('it-pipe') const { isValidTick } = require('./utils') @@ -92,6 +93,23 @@ module.exports = (common) => { expect(upgradeSpy.callCount).to.equal(2) }) + it('should not handle connection if upgradeInbound throws', async () => { + sinon.stub(upgrader, 'upgradeInbound').throws() + + const listener = transport.createListener(() => { + throw new Error('should not handle the connection if upgradeInbound throws') + }) + + // Listen + await listener.listen(addrs[0]) + + // Create a connection to the listener + const socket = await transport.dial(addrs[0]) + + await pWaitFor(() => typeof socket.timeline.close === 'number') + await listener.close() + }) + describe('events', () => { it('connection', (done) => { const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound')