Refactor utils for naming clarity

This commit is contained in:
Alejandro Cabeza Romero 2024-09-05 22:11:27 +02:00
parent 37458458de
commit 264e0dcca0
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
8 changed files with 38 additions and 37 deletions

View File

@ -14,8 +14,10 @@ import ../libp2p/protocols/secure/secure
import ../libp2p/switch
import ../libp2p/nameresolving/[nameresolver, mockresolver]
import "."/[asyncunit, errorhelpers]
export asyncunit, errorhelpers, mockresolver
import errorhelpers
import utils/async_tests
export async_tests, errorhelpers, mockresolver
const
StreamTransportTrackerName = "stream.transport"

View File

@ -34,7 +34,7 @@ import
protocols/pubsub/rpc/messages,
]
import ../../libp2p/protocols/pubsub/errors as pubsub_errors
import ../helpers, ../utils/[async, futures, async, tests]
import ../helpers, ../utils/[futures, async_tests, xtests]
from ../../libp2p/protocols/pubsub/mcache import window

View File

@ -25,8 +25,9 @@ import
protocols/pubsub/pubsubpeer,
protocols/pubsub/peertable,
protocols/pubsub/rpc/messages,
]
import ../helpers
],
../utils/[futures, async_tests],
../helpers
template tryPublish(
call: untyped, require: int, wait = 10.milliseconds, timeout = 10.seconds

View File

@ -18,7 +18,7 @@ import
discovery/discoverymngr,
discovery/rendezvousinterface,
]
import ./helpers, ./asyncunit, ./utils/[async, assertions, futures]
import ./helpers, ./utils/[futures, assertions, async_tests]
proc createSwitch(rdv: RendezVous = RendezVous.new()): Switch =
SwitchBuilder

View File

@ -1,30 +0,0 @@
import chronos/futures, stew/results, chronos
import ./futures
proc toOk(future: Future[void]): Result[void, string] =
return results.ok()
proc toOk[T](future: Future[T]): Result[T, string] =
return results.ok(future.read())
proc toResult*[T](future: Future[T]): Result[T, string] =
if future.cancelled():
return results.err("Future cancelled/timed out.")
elif future.finished():
if not future.failed():
return future.toOk()
else:
return results.err("Future finished but failed.")
else:
return results.err("Future still not finished.")
proc waitForResult*[T](
future: Future[T], timeout = DURATION_TIMEOUT
): Future[Result[T, string]] {.async.} =
discard await future.withTimeout(timeout)
return future.toResult()
proc reset*[T](future: Future[T]): void =
# Likely an incomplete reset, but good enough for testing purposes (for now)
future.internalError = nil
future.internalState = FutureState.Pending

View File

@ -1,5 +1,33 @@
import chronos
import chronos/futures, stew/results, chronos
const
DURATION_TIMEOUT* = 1.seconds
DURATION_TIMEOUT_EXTENDED* = 1500.milliseconds
proc toOk(future: Future[void]): Result[void, string] =
return results.ok()
proc toOk[T](future: Future[T]): Result[T, string] =
return results.ok(future.read())
proc toResult*[T](future: Future[T]): Result[T, string] =
if future.cancelled():
return results.err("Future cancelled/timed out.")
elif future.finished():
if not future.failed():
return future.toOk()
else:
return results.err("Future finished but failed.")
else:
return results.err("Future still not finished.")
proc waitForResult*[T](
future: Future[T], timeout = DURATION_TIMEOUT
): Future[Result[T, string]] {.async.} =
discard await future.withTimeout(timeout)
return future.toResult()
proc reset*[T](future: Future[T]): void =
# Likely an incomplete reset, but good enough for testing purposes (for now)
future.internalError = nil
future.internalState = FutureState.Pending