diff --git a/chronos/internal/asyncfutures.nim b/chronos/internal/asyncfutures.nim index b144cea7..c4a73747 100644 --- a/chronos/internal/asyncfutures.nim +++ b/chronos/internal/asyncfutures.nim @@ -560,9 +560,13 @@ proc waitFor*[T](fut: Future[T]): T {.raises: [CatchableError].} = ## **Blocks** the current thread until the specified future finishes and ## reads it, potentially raising an exception if the future failed or was ## cancelled. - while not(fut.finished()): - poll() - + var finished = false + # Ensure that callbacks currently scheduled on the future run before returning + proc continuation(udata: pointer) {.gcsafe.} = finished = true + if not(fut.finished()): + fut.addCallback(continuation) + while not(finished): + poll() fut.read() proc asyncSpawn*(future: Future[void]) = diff --git a/tests/testfut.nim b/tests/testfut.nim index fc9d4828..1297dc45 100644 --- a/tests/testfut.nim +++ b/tests/testfut.nim @@ -54,7 +54,6 @@ suite "Future[T] behavior test suite": fut.addCallback proc(udata: pointer) = testResult &= "5" discard waitFor(fut) - poll() check: fut.finished @@ -80,7 +79,6 @@ suite "Future[T] behavior test suite": fut.addCallback cb5 fut.removeCallback cb3 discard waitFor(fut) - poll() check: fut.finished testResult == "1245" @@ -1260,12 +1258,12 @@ suite "Future[T] behavior test suite": (loc.procedure == procedure) check: - chk(loc10, "testfut.nim", 1227, "macroFuture") - chk(loc11, "testfut.nim", 1230, "") - chk(loc20, "testfut.nim", 1239, "template") - chk(loc21, "testfut.nim", 1242, "") - chk(loc30, "testfut.nim", 1236, "procedure") - chk(loc31, "testfut.nim", 1243, "") + chk(loc10, "testfut.nim", 1225, "macroFuture") + chk(loc11, "testfut.nim", 1228, "") + chk(loc20, "testfut.nim", 1237, "template") + chk(loc21, "testfut.nim", 1240, "") + chk(loc30, "testfut.nim", 1234, "procedure") + chk(loc31, "testfut.nim", 1241, "") asyncTest "withTimeout(fut) should wait cancellation test": proc futureNeverEnds(): Future[void] = diff --git a/tests/testhttpclient.nim b/tests/testhttpclient.nim index e10892eb..f08b3c5b 100644 --- a/tests/testhttpclient.nim +++ b/tests/testhttpclient.nim @@ -187,11 +187,11 @@ suite "HTTP client testing suite": let ResponseTests = [ (MethodGet, "/test/short_size_response", 65600, 1024, "SHORTSIZERESPONSE"), - (MethodGet, "/test/long_size_response", 262400, 1024, + (MethodGet, "/test/long_size_response", 131200, 1024, "LONGSIZERESPONSE"), (MethodGet, "/test/short_chunked_response", 65600, 1024, "SHORTCHUNKRESPONSE"), - (MethodGet, "/test/long_chunked_response", 262400, 1024, + (MethodGet, "/test/long_chunked_response", 131200, 1024, "LONGCHUNKRESPONSE") ] proc process(r: RequestFence): Future[HttpResponseRef] {. diff --git a/tests/testproc.bat b/tests/testproc.bat index 11b4047e..05840395 100644 --- a/tests/testproc.bat +++ b/tests/testproc.bat @@ -34,7 +34,7 @@ ping -n 10 127.0.0.1 > NUL EXIT 0 :BIGDATA -FOR /L %%G IN (1, 1, 400000) DO ECHO ALICEWASBEGINNINGTOGETVERYTIREDOFSITTINGBYHERSISTERONTHEBANKANDO +FOR /L %%G IN (1, 1, 100000) DO ECHO ALICEWASBEGINNINGTOGETVERYTIREDOFSITTINGBYHERSISTERONTHEBANKANDO EXIT 0 :ENVTEST diff --git a/tests/testproc.nim b/tests/testproc.nim index 288ec181..588e3087 100644 --- a/tests/testproc.nim +++ b/tests/testproc.nim @@ -214,9 +214,9 @@ suite "Asynchronous process management test suite": "tests/testproc.sh bigdata" let expect = when defined(windows): - 400_000 * (64 + 2) + 100_000 * (64 + 2) else: - 400_000 * (64 + 1) + 100_000 * (64 + 1) let process = await startProcess(command, options = options, stdoutHandle = AsyncProcess.Pipe, stderrHandle = AsyncProcess.Pipe) diff --git a/tests/testproc.sh b/tests/testproc.sh index c5e7e0ac..e525da59 100755 --- a/tests/testproc.sh +++ b/tests/testproc.sh @@ -12,7 +12,7 @@ elif [ "$1" == "timeout2" ]; then elif [ "$1" == "timeout10" ]; then sleep 10 elif [ "$1" == "bigdata" ]; then - for i in {1..400000} + for i in {1..100000} do echo "ALICEWASBEGINNINGTOGETVERYTIREDOFSITTINGBYHERSISTERONTHEBANKANDO" done diff --git a/tests/testutils.nim b/tests/testutils.nim index fb5de50e..49072ebe 100644 --- a/tests/testutils.nim +++ b/tests/testutils.nim @@ -75,11 +75,6 @@ suite "Asynchronous utilities test suite": pendingFuturesCount() == 2'u waitFor fut - check: - getCount() == 1'u - pendingFuturesCount() == 1'u - - poll() check: getCount() == 0'u pendingFuturesCount() == 0'u