Skip to content

Commit

Permalink
fix: refactored tests to instantiate default dialer (libp2p#1581)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Feb 26, 2023
1 parent 8aead1a commit dbcece3
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const DefaultConfig: Partial<Libp2pInit> = {
connectionManagerConfig: {
maxConnections: 300,
minConnections: 50,

resolvers: {
dnsaddr: dnsaddrResolver
}
Expand Down
4 changes: 2 additions & 2 deletions src/connection-manager/dialer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface DialerInit {
* How long to wait between attempting to keep our number of concurrent connections
* above minConnections
*/
autoDialInterval: number
autoDialInterval?: number

/**
* How long a dial attempt is allowed to take
Expand All @@ -69,7 +69,7 @@ export interface DialerInit {
* When a new inbound connection is opened, the upgrade process (e.g. protect,
* encrypt, multiplex etc) must complete within this number of ms.
*/
inboundUpgradeTimeout: number
inboundUpgradeTimeout?: number

/**
* Number of max concurrent dials
Expand Down
2 changes: 1 addition & 1 deletion src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
this.components.upgrader = new DefaultUpgrader(this.components, {
connectionEncryption: (init.connectionEncryption ?? []).map(fn => this.configureComponent(fn(this.components))),
muxers: (init.streamMuxers ?? []).map(fn => this.configureComponent(fn(this.components))),
inboundUpgradeTimeout: init.dialer.inboundUpgradeTimeout
inboundUpgradeTimeout: init.dialer.inboundUpgradeTimeout ?? 10000
})

// Create the dialer
Expand Down
43 changes: 32 additions & 11 deletions test/connection-manager/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import { stubInterface } from 'sinon-ts'
import type { KeyBook, PeerStore } from '@libp2p/interface-peer-store'
import sinon from 'sinon'
import pWaitFor from 'p-wait-for'
import type { Connection } from '@libp2p/interface-connection'
import type { Connection, ConnectionGater } from '@libp2p/interface-connection'
import delay from 'delay'
import type { Libp2pNode } from '../../src/libp2p.js'
import { codes } from '../../src/errors.js'
import { start } from '@libp2p/interfaces/startable'
import type { Dialer } from '@libp2p/interface-connection-manager'
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'
import type { TransportManager } from '@libp2p/interface-transport'

describe('Connection Manager', () => {
let libp2p: Libp2p
Expand Down Expand Up @@ -49,18 +50,28 @@ describe('Connection Manager', () => {
it('should filter connections on disconnect, removing the closed one', async () => {
const upgrader = mockUpgrader()
const peerStore = stubInterface<PeerStore>()
const peerId = peerIds[0]
peerStore.keyBook = stubInterface<KeyBook>()

const dialer = new DefaultDialer({
peerId,
peerStore,
transportManager: stubInterface<TransportManager>(),
connectionGater: stubInterface<ConnectionGater>()
},
{
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})

const connectionManager = new DefaultConnectionManager({
peerId: peerIds[0],
dialer: stubInterface<Dialer>(),
peerId,
dialer,
upgrader,
peerStore
}, {
maxConnections: 1000,
minConnections: 50,
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
minConnections: 5
})

await start(connectionManager)
Expand Down Expand Up @@ -89,18 +100,28 @@ describe('Connection Manager', () => {
it('should close connections on stop', async () => {
const upgrader = mockUpgrader()
const peerStore = stubInterface<PeerStore>()
const peerId = peerIds[0]
peerStore.keyBook = stubInterface<KeyBook>()

const dialer = new DefaultDialer({
peerId,
peerStore,
transportManager: stubInterface<TransportManager>(),
connectionGater: stubInterface<ConnectionGater>()
},
{
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})

const connectionManager = new DefaultConnectionManager({
peerId: peerIds[0],
dialer: stubInterface<Dialer>(),
dialer,
upgrader,
peerStore
}, {
maxConnections: 1000,
minConnections: 50,
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
minConnections: 50
})

await start(connectionManager)
Expand Down
8 changes: 5 additions & 3 deletions test/dialing/direct.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ describe('Dialing (direct, TCP)', () => {
connectionGater: mockConnectionGater()
})
localComponents.peerStore = new PersistentPeerStore(localComponents)
localComponents.connectionManager = new DefaultConnectionManager(localComponents, {
maxConnections: 100,
minConnections: 50,
localComponents.dialer = new DefaultDialer(localComponents, {
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
localComponents.connectionManager = new DefaultConnectionManager(localComponents, {
maxConnections: 100,
minConnections: 50
})

localTM = new DefaultTransportManager(localComponents)
localTM.add(tcp()())
Expand Down
8 changes: 5 additions & 3 deletions test/dialing/direct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ describe('Dialing (direct, WebSockets)', () => {
localComponents.peerStore = new PersistentPeerStore(localComponents, {
addressFilter: localComponents.connectionGater.filterMultiaddrForPeer
})
localComponents.connectionManager = new DefaultConnectionManager(localComponents, {
maxConnections: 100,
minConnections: 50,
localComponents.dialer = new DefaultDialer(localComponents, {
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
localComponents.connectionManager = new DefaultConnectionManager(localComponents, {
maxConnections: 100,
minConnections: 50
})

localTM = new DefaultTransportManager(localComponents)
localTM.add(webSockets({ filter: filters.all })())
Expand Down
10 changes: 7 additions & 3 deletions test/fetch/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { pipe } from 'it-pipe'
import { PersistentPeerStore } from '@libp2p/peer-store'
import { MemoryDatastore } from 'datastore-core'
import { DefaultComponents } from '../../src/components.js'
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'

const defaultInit: FetchServiceInit = {
protocolPrefix: 'ipfs',
Expand All @@ -33,12 +34,15 @@ async function createComponents (index: number) {
datastore: new MemoryDatastore()
})
components.peerStore = new PersistentPeerStore(components)
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000,
components.dialer = new DefaultDialer(components, {
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000

})

return components
}
Expand Down
5 changes: 4 additions & 1 deletion test/identify/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { TimeoutController } from 'timeout-abort-controller'
import { CustomEvent } from '@libp2p/interfaces/events'
import pDefer from 'p-defer'
import { DefaultComponents } from '../../src/components.js'
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'

const listenMaddrs = [multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]

Expand Down Expand Up @@ -59,7 +60,9 @@ async function createComponents (index: number) {
components.peerStore = new PersistentPeerStore(components)
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000,
maxConnections: 1000
})
components.dialer = new DefaultDialer(components, {
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
Expand Down
5 changes: 4 additions & 1 deletion test/identify/push.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { start, stop } from '@libp2p/interfaces/startable'
import { stubInterface } from 'sinon-ts'
import type { Dialer } from '@libp2p/interface-connection-manager'
import { DefaultComponents } from '../../src/components.js'
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'

const listenMaddrs = [multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]

Expand Down Expand Up @@ -57,7 +58,9 @@ async function createComponents (index: number): Promise<DefaultComponents> {
components.peerStore = new PersistentPeerStore(components)
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000,
maxConnections: 1000
})
components.dialer = new DefaultDialer(components, {
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
Expand Down
9 changes: 6 additions & 3 deletions test/ping/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { pipe } from 'it-pipe'
import { PersistentPeerStore } from '@libp2p/peer-store'
import { MemoryDatastore } from 'datastore-core'
import { DefaultComponents } from '../../src/components.js'
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'

const defaultInit: PingServiceInit = {
protocolPrefix: 'ipfs',
Expand All @@ -33,12 +34,14 @@ async function createComponents (index: number): Promise<DefaultComponents> {
datastore: new MemoryDatastore()
})
components.peerStore = new PersistentPeerStore(components)
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000,
components.dialer = new DefaultDialer(components, {
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000
})

return components
}
Expand Down
4 changes: 1 addition & 3 deletions test/registrar/registrar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ describe('registrar', () => {
components.peerStore = new PersistentPeerStore(components)
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000,
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
maxConnections: 1000
})
registrar = new DefaultRegistrar(components)
})
Expand Down

0 comments on commit dbcece3

Please sign in to comment.