Skip to content

Commit

Permalink
Fix tests to probe ports.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed Mar 19, 2024
1 parent f97bfd0 commit 117ab1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
26 changes: 21 additions & 5 deletions tests/testdatagram.nim
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,23 @@ suite "Datagram Transport test suite":
except CancelledError as exc:
raiseAssert exc.msg

let sdgram = newDatagramTransport(process1, port)
let sdgram =
block:
var res: DatagramTransport
var currentPort = port
for i in 0 ..< 10:
res =
try:
newDatagramTransport(process1, currentPort,
flags = {ServerFlags.ReusePort})
except TransportOsError:
echo "Unable to create transport on port ", currentPort
currentPort = Port(uint16(currentPort) + 1'u16)
nil
if not(isNil(res)):
break
doAssert(not(isNil(res)), "Unable to create transport, giving up")
res

var
address =
Expand Down Expand Up @@ -824,22 +840,22 @@ suite "Datagram Transport test suite":
if isAvailable(AddressFamily.IPv4):
check:
(await performAutoAddressTest(Port(0), AddressFamily.IPv4)) == true
asyncTest "[IP] Auto-address constructor test (*:60436)":
asyncTest "[IP] Auto-address constructor test (*:30231)":
if isAvailable(AddressFamily.IPv6):
check:
(await performAutoAddressTest(Port(60436), AddressFamily.IPv6)) == true
(await performAutoAddressTest(Port(30231), AddressFamily.IPv6)) == true
# If IPv6 is available newAutoDatagramTransport should bind to `::` - this
# means that we should be able to connect to it via IPV4_MAPPED address,
# but only when IPv4 is also available.
if isAvailable(AddressFamily.IPv4):
check:
(await performAutoAddressTest(Port(60436), AddressFamily.IPv4)) ==
(await performAutoAddressTest(Port(30231), AddressFamily.IPv4)) ==
true
else:
# If IPv6 is not available newAutoDatagramTransport should bind to
# `0.0.0.0` - this means we should be able to connect to it via IPv4
# address.
if isAvailable(AddressFamily.IPv4):
check:
(await performAutoAddressTest(Port(60436), AddressFamily.IPv4)) ==
(await performAutoAddressTest(Port(30231), AddressFamily.IPv4)) ==
true
27 changes: 19 additions & 8 deletions tests/teststream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1490,10 +1490,21 @@ suite "Stream Transport test suite":
family: AddressFamily): Future[bool] {.
async: (raises: []).} =
let server =
try:
createStreamServer(port)
except TransportOsError as exc:
raiseAssert exc.msg
block:
var currentPort = port
var res: StreamServer
for i in 0 ..< 10:
res =
try:
createStreamServer(port)
except TransportOsError:
echo "Unable to create server on port ", currentPort
currentPort = Port(uint16(currentPort) + 1'u16)
nil
if not(isNil(res)):
break
doAssert(not(isNil(res)), "Unable to create server, giving up")
res

var
address =
Expand Down Expand Up @@ -1746,23 +1757,23 @@ suite "Stream Transport test suite":
check:
(await performAutoAddressTest(Port(0), AddressFamily.IPv4)) == true

asyncTest "[IP] Auto-address constructor test (*:60436)":
asyncTest "[IP] Auto-address constructor test (*:30232)":
if isAvailable(AddressFamily.IPv6):
check:
(await performAutoAddressTest(Port(60436), AddressFamily.IPv6)) == true
(await performAutoAddressTest(Port(30232), AddressFamily.IPv6)) == true
# If IPv6 is available createStreamServer should bind to `::` this means
# that we should be able to connect to it via IPV4_MAPPED address, but
# only when IPv4 is also available.
if isAvailable(AddressFamily.IPv4):
check:
(await performAutoAddressTest(Port(60436), AddressFamily.IPv4)) ==
(await performAutoAddressTest(Port(30232), AddressFamily.IPv4)) ==
true
else:
# If IPv6 is not available createStreamServer should bind to `0.0.0.0`
# this means we should be able to connect to it via IPV4 address.
if isAvailable(AddressFamily.IPv4):
check:
(await performAutoAddressTest(Port(60436), AddressFamily.IPv4)) ==
(await performAutoAddressTest(Port(30232), AddressFamily.IPv4)) ==
true

test "File descriptors leak test":
Expand Down

0 comments on commit 117ab1c

Please sign in to comment.