From 888385ee7ae18f6c90699957a40cfc76a94ab125 Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 29 Aug 2024 22:27:10 +0200 Subject: [PATCH] refactor proc --- .pinned | 2 +- libp2p/utils/future.nim | 12 ++++++++++-- tests/utils/testfuture.nim | 8 ++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.pinned b/.pinned index 8a4794ec8..f7226aed6 100644 --- a/.pinned +++ b/.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 diff --git a/libp2p/utils/future.nim b/libp2p/utils/future.nim index 10cea3495..f5dad68ae 100644 --- a/libp2p/utils/future.nim +++ b/libp2p/utils/future.nim @@ -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: diff --git a/tests/utils/testfuture.nim b/tests/utils/testfuture.nim index 460567af7..c99c77c8a 100644 --- a/tests/utils/testfuture.nim +++ b/tests/utils/testfuture.nim @@ -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