Skip to content

Commit

Permalink
test: throws an error for outbound connection beyond limit and added …
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
maschad committed Jan 3, 2023
1 parent 4c6112f commit cccd34e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,13 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
return existingConnections[0]
}

if ((this.getConnections().length + 1) > this.opts.maxOutgoingConnections) {
throw errCode(
new Error('Connection Manager maxOutgoing connections exceeded'),
codes.ERR_CONNECTION_DENIED
)
}

let timeoutController: TimeoutController | undefined

if (options?.signal == null) {
Expand Down
20 changes: 19 additions & 1 deletion test/connection-manager/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,26 @@ describe('Connection Manager', () => {
.to.eventually.be.false()
})

it.skip('should deny connections when maxOutgoingConnections is exceeded', async () => {
it('should throw an error when attempting to connect and maxOutgoingConnections is exceeded', async () => {
const dialer = stubInterface<Dialer>()
dialer.dial.resolves(stubInterface<Connection>())

const connectionManager = new DefaultConnectionManager({
peerId: libp2p.peerId,
upgrader: stubInterface<Upgrader>(),
peerStore: stubInterface<PeerStore>(),
dialer
}, {
...defaultOptions,
maxOutgoingConnections: 1
})

// max out the connection limit
await connectionManager.openConnection(await createEd25519PeerId())
expect(connectionManager.getConnections()).to.have.lengthOf(1)

await expect(connectionManager.openConnection(await createEd25519PeerId())).to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_CONNECTION_DENIED)
})

it('should deny connections from peers that connect too frequently', async () => {
Expand Down

0 comments on commit cccd34e

Please sign in to comment.