Skip to content

Commit

Permalink
fix: start listener before AfterStart
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <[email protected]>
  • Loading branch information
sfroment committed Dec 25, 2024
1 parent 7caee9f commit a35d6fc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/libp2p/src/transport-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ export class DefaultTransportManager implements TransportManager, Startable {
return this.started
}

start (): void {
async start (): Promise<void> {
this.started = true
}

async afterStart (): Promise<void> {
// Listen on the provided transports for the provided addresses
const addrs = this.components.addressManager.getListenAddrs()

await this.listen(addrs)
}

async afterStart (): Promise<void> {
}

/**
* Stops all listeners
*/
Expand Down
46 changes: 45 additions & 1 deletion packages/transport-websockets/test/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import { multiaddr } from '@multiformats/multiaddr'
import { WebSockets, WebSocketsSecure } from '@multiformats/multiaddr-matcher'
import { expect } from 'aegir/chai'
import { isLoopbackAddr } from 'is-loopback-addr'
import { createLibp2p } from 'libp2p'
import { pEvent } from 'p-event'
import pWaitFor from 'p-wait-for'
import Sinon from 'sinon'
import { stubInterface } from 'sinon-ts'
import * as filters from '../src/filters.js'
import { webSockets } from '../src/index.js'
import type { Connection, Libp2pEvents, Listener, Transport, Upgrader, TLSCertificate } from '@libp2p/interface'
import type { Connection, Libp2pEvents, Listener, Transport, Upgrader, TLSCertificate, PeerDiscoveryEvents, PeerDiscovery, Startable } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'
import type { StubbedInstance } from 'sinon-ts'

describe('instantiate the transport', () => {
Expand Down Expand Up @@ -746,3 +748,45 @@ describe('auto-tls (IPv6)', () => {
expect(wsOptions.port).to.equal(wssOptions.port)
})
})

class TestPeerDiscovery extends TypedEventEmitter<PeerDiscoveryEvents> implements PeerDiscovery, Startable {
private readonly addressManager: AddressManager

constructor (components: {
addressManager: AddressManager
}) {
super()
this.addressManager = components.addressManager
}

async start (): Promise<void> {
}

async stop (): Promise<void> { }

afterStart (): void {
this.addressManager.getAddresses()
}

readonly [Symbol.toStringTag] = '@libp2p/test-peer-discovery'
}

describe('discovery-websockets', () => {
it('should discover peers over websockets', async () => {
const libp2p = await createLibp2p({
addresses: {
listen: [
'/ip4/0.0.0.0/tcp/0/ws'
]
},
peerDiscovery: [(component: {
addressManager: AddressManager
}) => new TestPeerDiscovery(component)],
transports: [
webSockets()
]
})

await libp2p.start()
})
})

0 comments on commit a35d6fc

Please sign in to comment.