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 Dec 6, 2022
1 parent 456dc62 commit d6b4fd3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,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
23 changes: 21 additions & 2 deletions test/connection-manager/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import pWaitFor from 'p-wait-for'
import sinon from 'sinon'
import { stubInterface } from 'sinon-ts'
import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
import { codes as ErrorCodes } from '../../src/errors.js'
import type { Libp2pNode } from '../../src/libp2p.js'
import { createBaseOptions } from '../utils/base-options.browser.js'
import { createNode } from '../utils/creators/peer.js'
Expand Down Expand Up @@ -116,7 +117,7 @@ describe('Connection Manager', () => {
expect(lowestSpy).to.have.property('callCount', 1)
})

it('should close connection when the maximum has been reached even without tags', async () => {
it('should close connection when the maximum incoming has been reached even without tags', async () => {
const max = 5
libp2p = await createNode({
config: createBaseOptions({
Expand Down Expand Up @@ -251,8 +252,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 d6b4fd3

Please sign in to comment.