diff --git a/codex/blockexchange/engine/advertiser.nim b/codex/blockexchange/engine/advertiser.nim index 5ff82e48..20baaf58 100644 --- a/codex/blockexchange/engine/advertiser.nim +++ b/codex/blockexchange/engine/advertiser.nim @@ -137,10 +137,12 @@ proc start*(b: Advertiser) {.async.} = b.advertiserRunning = true for i in 0.. " & $machine.state running = machine.run(machine.state) - asyncSpawn running.track(machine) + machine.trackedFutures.track(running) + asyncSpawn running except CancelledError: break # do not propagate bc it is asyncSpawned @@ -90,7 +91,9 @@ proc start*(machine: Machine, initialState: State) = machine.scheduled = newAsyncQueue[Event]() machine.started = true - asyncSpawn machine.scheduler().track(machine) + let fut = machine.scheduler() + machine.trackedFutures.track(fut) + asyncSpawn fut machine.schedule(Event.transition(machine.state, initialState)) proc stop*(machine: Machine) {.async.} = diff --git a/codex/utils/trackedfutures.nim b/codex/utils/trackedfutures.nim index a9753299..2505ffe2 100644 --- a/codex/utils/trackedfutures.nim +++ b/codex/utils/trackedfutures.nim @@ -19,9 +19,9 @@ proc removeFuture(self: TrackedFutures, future: FutureBase) = if not self.cancelling and not future.isNil: self.futures.del(future.id) -proc track*[T](self: TrackedFutures, fut: Future[T]): Future[T] = +proc track*[T](self: TrackedFutures, fut: Future[T]) = if self.cancelling: - return fut + return self.futures[fut.id] = FutureBase(fut) @@ -30,14 +30,6 @@ proc track*[T](self: TrackedFutures, fut: Future[T]): Future[T] = fut.addCallback(cb) - return fut - -proc track*[T, U](future: Future[T], self: U): Future[T] = - ## Convenience method that allows chaining future, eg: - ## `await someFut().track(sales)`, where `sales` has declared a - ## `trackedFutures` property. - self.trackedFutures.track(future) - proc cancelTracked*(self: TrackedFutures) {.async: (raises: []).} = self.cancelling = true diff --git a/tests/codex/utils/testtrackedfutures.nim b/tests/codex/utils/testtrackedfutures.nim index 9274f84f..cbabd39c 100644 --- a/tests/codex/utils/testtrackedfutures.nim +++ b/tests/codex/utils/testtrackedfutures.nim @@ -18,36 +18,36 @@ asyncchecksuite "tracked futures": test "tracks unfinished futures": let fut = newFuture[void]("test") - discard fut.track(module) + module.trackedFutures.track(fut) check module.trackedFutures.len == 1 test "does not track completed futures": let fut = newFuture[void]("test") fut.complete() - discard fut.track(module) + module.trackedFutures.track(fut) check eventually module.trackedFutures.len == 0 test "does not track failed futures": let fut = newFuture[void]("test") fut.fail((ref CatchableError)(msg: "some error")) - discard fut.track(module) + module.trackedFutures.track(fut) check eventually module.trackedFutures.len == 0 test "does not track cancelled futures": let fut = newFuture[void]("test") await fut.cancelAndWait() - discard fut.track(module) + module.trackedFutures.track(fut) check eventually module.trackedFutures.len == 0 test "removes tracked future when finished": let fut = newFuture[void]("test") - discard fut.track(module) + module.trackedFutures.track(fut) fut.complete() check eventually module.trackedFutures.len == 0 test "removes tracked future when cancelled": let fut = newFuture[void]("test") - discard fut.track(module) + module.trackedFutures.track(fut) await fut.cancelAndWait() check eventually module.trackedFutures.len == 0 @@ -55,9 +55,9 @@ asyncchecksuite "tracked futures": let fut1 = newFuture[void]("test1") let fut2 = newFuture[void]("test2") let fut3 = newFuture[void]("test3") - discard fut1.track(module) - discard fut2.track(module) - discard fut3.track(module) + module.trackedFutures.track(fut1) + module.trackedFutures.track(fut2) + module.trackedFutures.track(fut3) await module.trackedFutures.cancelTracked() check eventually fut1.cancelled check eventually fut2.cancelled diff --git a/tests/integration/nodeprocess.nim b/tests/integration/nodeprocess.nim index c762926d..88613d2e 100644 --- a/tests/integration/nodeprocess.nim +++ b/tests/integration/nodeprocess.nim @@ -157,10 +157,11 @@ proc waitUntilOutput*(node: NodeProcess, output: string) {.async.} = trace "waiting until", output let started = newFuture[void]() - let fut = node.captureOutput(output, started).track(node) + let fut = node.captureOutput(output, started) + node.trackedFutures.track(fut) asyncSpawn fut await started.wait(60.seconds) # allow enough time for proof generation - + proc waitUntilStarted*(node: NodeProcess) {.async.} = try: await node.waitUntilOutput(node.startedOutput)