-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent auto-dialer from dialing self (#1104)
Co-authored-by: Robert Kiel <[email protected]> Co-authored-by: achingbrain <[email protected]>
- Loading branch information
1 parent
d8ceb0b
commit 9b22c6e
Showing
3 changed files
with
80 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict' | ||
/* eslint-env mocha */ | ||
|
||
const { expect } = require('aegir/utils/chai') | ||
const sinon = require('sinon') | ||
const AutoDialler = require('../../src/connection-manager/auto-dialler') | ||
const pWaitFor = require('p-wait-for') | ||
const PeerId = require('peer-id') | ||
const delay = require('delay') | ||
|
||
describe('Auto-dialler', () => { | ||
let autoDialler | ||
let libp2p | ||
let options | ||
|
||
beforeEach(async () => { | ||
libp2p = {} | ||
options = {} | ||
autoDialler = new AutoDialler(libp2p, options) | ||
}) | ||
|
||
afterEach(async () => { | ||
sinon.restore() | ||
}) | ||
|
||
it('should not dial self', async () => { | ||
// peers with protocols are dialled before peers without protocols | ||
const self = { | ||
id: await PeerId.create(), | ||
protocols: [ | ||
'/foo/bar' | ||
] | ||
} | ||
const other = { | ||
id: await PeerId.create(), | ||
protocols: [] | ||
} | ||
|
||
autoDialler._options.minConnections = 10 | ||
libp2p.peerId = self.id | ||
libp2p.connections = { | ||
size: 1 | ||
} | ||
libp2p.peerStore = { | ||
getPeers: sinon.stub().returns([self, other]) | ||
} | ||
libp2p.connectionManager = { | ||
get: () => {} | ||
} | ||
libp2p.dialer = { | ||
connectToPeer: sinon.stub().resolves() | ||
} | ||
|
||
await autoDialler.start() | ||
|
||
await pWaitFor(() => libp2p.dialer.connectToPeer.callCount === 1) | ||
await delay(1000) | ||
|
||
await autoDialler.stop() | ||
|
||
expect(libp2p.dialer.connectToPeer.callCount).to.equal(1) | ||
expect(libp2p.dialer.connectToPeer.calledWith(self.id)).to.be.false() | ||
}) | ||
}) |