Skip to content

Commit 28af2ff

Browse files
authored
test: fix flaky interop tests - address already in use (#3396)
1 parent db185b1 commit 28af2ff

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

packages/integration-tests/.aegir.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ async function createGoLibp2pRelay () {
169169
const { defaultLogger } = await import('@libp2p/logger')
170170

171171
const log = defaultLogger().forComponent('go-libp2p')
172-
const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000
173-
const apiAddr = multiaddr(`/ip4/127.0.0.1/tcp/${controlPort}`)
172+
const controlAddrUndefinedPort = multiaddr('/ip4/127.0.0.1/tcp/0')
174173
const deferred = pDefer()
174+
175175
const proc = execa(p2pd(), [
176-
`-listen=${apiAddr.toString()}`,
176+
`-listen=${controlAddrUndefinedPort.toString()}`,
177177
// listen on TCP, WebSockets and WebTransport
178178
'-hostAddrs=/ip4/127.0.0.1/tcp/0,/ip4/127.0.0.1/tcp/0/ws,/ip4/127.0.0.1/udp/0/quic-v1/webtransport',
179179
'-noise=true',
@@ -185,15 +185,23 @@ async function createGoLibp2pRelay () {
185185
GOLOG_LOG_LEVEL: 'debug'
186186
}
187187
})
188+
188189
proc.catch(() => {
189190
// go-libp2p daemon throws when killed
190191
})
191192

193+
proc.once('exit', code => {
194+
deferred.reject(new Error(`go-libp2p daemon exited before startup (code: ${code ?? 'unknown'})`))
195+
})
196+
197+
let controlMultiaddr
198+
192199
proc.stdout?.on('data', (buf) => {
193200
const str = buf.toString()
194201

195-
// daemon has started
196-
if (str.includes('Control socket:')) {
202+
const match = str.match(/Control socket:\s*(.+)/)
203+
if (match != null) {
204+
controlMultiaddr = multiaddr(match[1].trim())
197205
deferred.resolve()
198206
}
199207
})
@@ -202,13 +210,18 @@ async function createGoLibp2pRelay () {
202210

203211
log(str)
204212
})
213+
205214
await deferred.promise
206215

207-
const daemonClient = createClient(apiAddr)
216+
if (controlMultiaddr == null) {
217+
throw new Error('go-libp2p daemon did not report a control socket')
218+
}
219+
220+
const daemonClient = createClient(controlMultiaddr)
208221
const id = await daemonClient.identify()
209222

210223
return {
211-
apiAddr,
224+
apiAddr: controlMultiaddr,
212225
peerId: id.peerId.toString(),
213226
multiaddrs: id.addrs.map(ma => ma.toString()).join(','),
214227
proc

packages/integration-tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"delay": "^7.0.0",
5858
"detect-browser": "^5.3.0",
5959
"execa": "^9.6.0",
60-
"go-libp2p": "^1.6.0",
60+
"go-libp2p": "^1.7.0",
6161
"libp2p": "^3.1.5",
6262
"main-event": "^1.0.1",
6363
"multiformats": "^13.4.0",

packages/integration-tests/test/interop.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type { Identify } from '@libp2p/identify'
2626
import type { ServiceMap, PrivateKey } from '@libp2p/interface'
2727
import type { SpawnOptions, Daemon, DaemonFactory } from '@libp2p/interop'
2828
import type { Ping } from '@libp2p/ping'
29+
import type { Multiaddr } from '@multiformats/multiaddr'
2930
import type { Libp2pOptions, ServiceFactoryMap } from 'libp2p'
3031

3132
/**
@@ -39,13 +40,12 @@ import type { Libp2pOptions, ServiceFactoryMap } from 'libp2p'
3940
*/
4041

4142
async function createGoPeer (options: SpawnOptions): Promise<Daemon> {
42-
const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000
43-
const apiAddr = multiaddr(`/ip4/127.0.0.1/tcp/${controlPort}`)
43+
const controlAddrUndefinedPort = multiaddr('/ip4/127.0.0.1/tcp/0')
4444

45-
const log = logger(`go-libp2p:${controlPort}`)
45+
const log = logger('go-libp2p')
4646

4747
const opts = [
48-
`-listen=${apiAddr.toString()}`
48+
`-listen=${controlAddrUndefinedPort.toString()}`
4949
]
5050

5151
if (options.noListen === true) {
@@ -92,19 +92,30 @@ async function createGoPeer (options: SpawnOptions): Promise<Daemon> {
9292
opts.push('-muxer=yamux')
9393
}
9494

95-
const deferred = pDefer()
95+
const deferred = pDefer<void>()
9696
const proc = execa(p2pd(), opts, {
9797
env: {
9898
GOLOG_LOG_LEVEL: 'debug'
9999
}
100100
})
101101

102+
proc.catch(() => {
103+
// go-libp2p daemon throws when killed
104+
})
105+
106+
proc.once('exit', code => {
107+
deferred.reject(new Error(`go-libp2p daemon exited before startup (code: ${code ?? 'unknown'})`))
108+
})
109+
110+
let controlMultiaddr: Multiaddr | undefined
111+
102112
proc.stdout?.on('data', (buf: Buffer) => {
103113
const str = buf.toString()
104114
log(str)
105115

106-
// daemon has started
107-
if (str.includes('Control socket:')) {
116+
const match = str.match(/Control socket:\s*(.+)/)
117+
if (match != null) {
118+
controlMultiaddr = multiaddr(match[1].trim())
108119
deferred.resolve()
109120
}
110121
})
@@ -115,8 +126,12 @@ async function createGoPeer (options: SpawnOptions): Promise<Daemon> {
115126

116127
await deferred.promise
117128

129+
if (controlMultiaddr == null) {
130+
throw new Error('go-libp2p daemon did not report a control socket')
131+
}
132+
118133
return {
119-
client: createClient(apiAddr),
134+
client: createClient(controlMultiaddr),
120135
stop: async () => {
121136
proc.kill()
122137
}

0 commit comments

Comments
 (0)