Use random ports (#429)

This commit is contained in:
diegomrsantos 2023-08-02 21:04:30 +02:00 committed by GitHub
parent 6b4f5a1d23
commit c546a4329c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 23 deletions

View File

@ -1259,46 +1259,44 @@ suite "Stream Transport test suite":
return buffer == message
proc testConnectBindLocalAddress() {.async.} =
let dst1 = initTAddress("127.0.0.1:33335")
let dst2 = initTAddress("127.0.0.1:33336")
let dst3 = initTAddress("127.0.0.1:33337")
proc client(server: StreamServer, transp: StreamTransport) {.async.} =
await transp.closeWait()
# We use ReuseAddr here only to be able to reuse the same IP/Port when there's a TIME_WAIT socket. It's useful when
# running the test multiple times or if a test ran previously used the same port.
let servers =
[createStreamServer(dst1, client, {ReuseAddr}),
createStreamServer(dst2, client, {ReuseAddr}),
createStreamServer(dst3, client, {ReusePort})]
let server1 = createStreamServer(initTAddress("127.0.0.1:0"), client)
let server2 = createStreamServer(initTAddress("127.0.0.1:0"), client)
let server3 = createStreamServer(initTAddress("127.0.0.1:0"), client, {ReusePort})
for server in servers:
server.start()
let ta = initTAddress("0.0.0.0:35000")
server1.start()
server2.start()
server3.start()
# It works cause there's no active listening socket bound to ta and we are using ReuseAddr
var transp1 = await connect(dst1, localAddress = ta, flags={SocketFlags.ReuseAddr})
var transp2 = await connect(dst2, localAddress = ta, flags={SocketFlags.ReuseAddr})
var transp1 = await connect(server1.local, flags={SocketFlags.ReuseAddr})
let ta = transp1.localAddress
var transp2 = await connect(server2.local, localAddress = ta, flags={SocketFlags.ReuseAddr})
# It works cause even thought there's an active listening socket bound to dst3, we are using ReusePort
var transp3 = await connect(dst2, localAddress = dst3, flags={SocketFlags.ReusePort})
# It works cause even though there's an active listening socket bound to dst3, we are using ReusePort
var transp3 = await connect(server2.local, localAddress = server3.local, flags={SocketFlags.ReusePort})
expect(TransportOsError):
var transp2 {.used.} = await connect(dst3, localAddress = ta)
var transp2 {.used.} = await connect(server3.local, localAddress = ta)
expect(TransportOsError):
var transp3 {.used.} =
await connect(dst3, localAddress = initTAddress(":::35000"))
var transp3 {.used.} = await connect(server3.local, localAddress = initTAddress("::", transp1.localAddress.port))
await transp1.closeWait()
await transp2.closeWait()
await transp3.closeWait()
for server in servers:
server.stop()
await server.closeWait()
server1.stop()
await server1.closeWait()
server2.stop()
await server2.closeWait()
server3.stop()
await server3.closeWait()
markFD = getCurrentFD()