Skip to content

Commit

Permalink
move to separate file to avoid interfering with hole punching test
Browse files Browse the repository at this point in the history
  • Loading branch information
etan-status committed Mar 1, 2024
1 parent 30dc39e commit 31f5d14
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
27 changes: 27 additions & 0 deletions tests/errorhelpers.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import chronos

proc allFuturesThrowing*[F: FutureBase](args: varargs[F]): Future[void] =
# This proc is only meant for use in tests / not suitable for general use.
# - Swallowing errors arbitrarily instead of aggregating them is bad design
# - It raises `CatchableError` instead of the union of the `futs` errors,
# inflating the caller's `raises` list unnecessarily. `macro` could fix it
var futs: seq[F]
for fut in args:
futs &= fut
proc call() {.async.} =
var first: ref CatchableError = nil
futs = await allFinished(futs)
for fut in futs:
if fut.failed:
let err = fut.readError()
if err of Defect:
raise err
else:
if err of CancelledError:
raise err
if isNil(first):
first = err
if not isNil(first):
raise first

return call()
30 changes: 2 additions & 28 deletions tests/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,8 @@ import ../libp2p/protocols/secure/secure
import ../libp2p/switch
import ../libp2p/nameresolving/[nameresolver, mockresolver]

import ./asyncunit
export asyncunit, mockresolver

proc allFuturesThrowing*[F: FutureBase](args: varargs[F]): Future[void] =
# This proc is only meant for use in tests / not suitable for general use.
# - Swallowing errors arbitrarily instead of aggregating them is bad design
# - It raises `CatchableError` instead of the union of the `futs` errors,
# inflating the caller's `raises` list unnecessarily. `macro` could fix it
var futs: seq[F]
for fut in args:
futs &= fut
proc call() {.async.} =
var first: ref CatchableError = nil
futs = await allFinished(futs)
for fut in futs:
if fut.failed:
let err = fut.readError()
if err of Defect:
raise err
else:
if err of CancelledError:
raise err
if isNil(first):
first = err
if not isNil(first):
raise first

return call()
import "."/[asyncunit, errorhelpers]
export asyncunit, errorhelpers, mockresolver

const
StreamTransportTrackerName = "stream.transport"
Expand Down
4 changes: 2 additions & 2 deletions tests/hole-punching-interop/hole_punching.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ../../libp2p/[builders,
protocols/connectivity/autonat/service,
protocols/ping]
import ../stubs/autonatclientstub
import ../helpers
import ../errorhelpers

proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch =
let rng = newRng()
Expand Down Expand Up @@ -48,7 +48,7 @@ proc main() {.async.} =
isListener = getEnv("MODE") == "listen"
switch = createSwitch(relayClient, hpservice)
auxSwitch = createSwitch()
redisClient = open("redis", 6379.Port)
redisClient = open("localhost", 6379.Port)

debug "Connected to redis"

Expand Down

0 comments on commit 31f5d14

Please sign in to comment.