refactor proc
This commit is contained in:
parent
e21b0e4d99
commit
888385ee7a
2
.pinned
2
.pinned
|
@ -1,6 +1,6 @@
|
|||
bearssl;https://github.com/status-im/nim-bearssl@#667b40440a53a58e9f922e29e20818720c62d9ac
|
||||
chronicles;https://github.com/status-im/nim-chronicles@#32ac8679680ea699f7dbc046e8e0131cac97d41a
|
||||
chronos;https://github.com/status-im/nim-chronos@#dc3847e4d6733dfc3811454c2a9c384b87343e26
|
||||
chronos;https://github.com/status-im/nim-chronos@#ef93a15bf9eb22b3fcc15aab3edaf3f8ae9618a7
|
||||
dnsclient;https://github.com/ba0f3/dnsclient.nim@#23214235d4784d24aceed99bbfe153379ea557c8
|
||||
faststreams;https://github.com/status-im/nim-faststreams@#720fc5e5c8e428d9d0af618e1e27c44b42350309
|
||||
httputils;https://github.com/status-im/nim-http-utils@#3b491a40c60aad9e8d3407443f46f62511e63b18
|
||||
|
|
|
@ -33,9 +33,17 @@ proc anyCompleted*[T](futs: seq[Future[T]]): Future[Future[T]] {.async.} =
|
|||
let index = requests.find(raceFut)
|
||||
requests.del(index)
|
||||
|
||||
proc raceCancel*(
|
||||
futs: seq[Future | InternalRaisesFuture]
|
||||
proc raceAndCancelPending*(
|
||||
futs: seq[SomeFuture]
|
||||
): Future[void] {.async: (raises: [ValueError, CancelledError]).} =
|
||||
## Executes a race between the provided sequence of futures.
|
||||
## Cancels any remaining futures that have not yet completed.
|
||||
##
|
||||
## - `futs`: A sequence of futures to race.
|
||||
##
|
||||
## Raises:
|
||||
## - `ValueError` if the sequence of futures is empty.
|
||||
## - `CancelledError` if the operation is canceled.
|
||||
try:
|
||||
discard await race(futs)
|
||||
finally:
|
||||
|
|
|
@ -25,7 +25,7 @@ suite "Utils Future":
|
|||
futureB = longRunningTaskB()
|
||||
|
||||
# Both futures should be canceled when raceCancel is called
|
||||
await raceCancel(@[futureA, futureB]).cancelAndWait()
|
||||
await raceAndCancelPending(@[futureA, futureB]).cancelAndWait()
|
||||
check futureA.cancelled
|
||||
check futureB.cancelled
|
||||
|
||||
|
@ -42,16 +42,16 @@ suite "Utils Future":
|
|||
futureSlow = slowTask()
|
||||
|
||||
# The quick task finishes, so the slow task should be canceled
|
||||
await raceCancel(@[futureQuick, futureSlow])
|
||||
await raceAndCancelPending(@[futureQuick, futureSlow])
|
||||
check futureQuick.finished
|
||||
check futureSlow.cancelled
|
||||
|
||||
asyncTest "raceCancel with AsyncEvent":
|
||||
asyncTest "raceAndCancelPending with AsyncEvent":
|
||||
let asyncEvent = newAsyncEvent()
|
||||
let fut1 = asyncEvent.wait()
|
||||
let fut2 = newAsyncEvent().wait()
|
||||
asyncEvent.fire()
|
||||
await raceCancel(@[fut1, fut2])
|
||||
await raceAndCancelPending(@[fut1, fut2])
|
||||
|
||||
check fut1.finished
|
||||
check fut2.cancelled
|
||||
|
|
Loading…
Reference in New Issue