diff --git a/src/connection-manager/index.ts b/src/connection-manager/index.ts index a9d32a98d1..53615a2bb3 100644 --- a/src/connection-manager/index.ts +++ b/src/connection-manager/index.ts @@ -512,6 +512,13 @@ export class DefaultConnectionManager extends EventEmitter this.opts.maxOutgoingConnections) { + throw errCode( + new Error('Connection Manager maxOutgoing connections exceeded'), + codes.ERR_CONNECTION_DENIED + ) + } + let timeoutController: TimeoutController | undefined if (options?.signal == null) { diff --git a/test/connection-manager/index.spec.ts b/test/connection-manager/index.spec.ts index b494b518e0..72f548cd1f 100644 --- a/test/connection-manager/index.spec.ts +++ b/test/connection-manager/index.spec.ts @@ -371,8 +371,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.dial.resolves(stubInterface()) + + const connectionManager = new DefaultConnectionManager({ + peerId: libp2p.peerId, + upgrader: stubInterface(), + peerStore: stubInterface(), + 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 () => {