Skip to content

Commit

Permalink
refactor proc
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Aug 29, 2024
1 parent e21b0e4 commit 888385e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .pinned
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 10 additions & 2 deletions libp2p/utils/future.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions tests/utils/testfuture.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit 888385e

Please sign in to comment.