From 8d19476a4c1e59c5bf5c9b53afe678f2d4047cef Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 17 May 2022 16:40:57 +0200 Subject: [PATCH] [sales] Ensure that Sales is completely stopped --- dagger/contracts/interactions.nim | 4 ++-- dagger/sales.nim | 27 ++++++++++----------------- tests/dagger/testsales.nim | 4 ++-- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/dagger/contracts/interactions.nim b/dagger/contracts/interactions.nim index 347ce33e..fd04334b 100644 --- a/dagger/contracts/interactions.nim +++ b/dagger/contracts/interactions.nim @@ -65,9 +65,9 @@ proc new*(_: type ContractInteractions): ?ContractInteractions = ContractInteractions.new("ws://localhost:8545") proc start*(interactions: ContractInteractions) {.async.} = - interactions.sales.start() + await interactions.sales.start() await interactions.proving.start() proc stop*(interactions: ContractInteractions) {.async.} = - interactions.sales.stop() + await interactions.sales.stop() await interactions.proving.stop() diff --git a/dagger/sales.nim b/dagger/sales.nim index c2df8987..fba9a08f 100644 --- a/dagger/sales.nim +++ b/dagger/sales.nim @@ -135,28 +135,21 @@ proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) = asyncSpawn negotiation.start() -proc start*(sales: Sales) = +proc start*(sales: Sales) {.async.} = doAssert sales.subscription.isNone, "Sales already started" proc onRequest(requestId: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} = sales.handleRequest(requestId, ask) - proc subscribe {.async.} = - try: - sales.subscription = some await sales.market.subscribeRequests(onRequest) - except CatchableError as e: - error "Unable to start sales", msg = e.msg + try: + sales.subscription = some await sales.market.subscribeRequests(onRequest) + except CatchableError as e: + error "Unable to start sales", msg = e.msg - asyncSpawn subscribe() - -proc stop*(sales: Sales) = +proc stop*(sales: Sales) {.async.} = if subscription =? sales.subscription: sales.subscription = Subscription.none - - proc unsubscribe {.async.} = - try: - await subscription.unsubscribe() - except CatchableError as e: - warn "Unsubscribe failed", msg = e.msg - - asyncSpawn unsubscribe() + try: + await subscription.unsubscribe() + except CatchableError as e: + warn "Unsubscribe failed", msg = e.msg diff --git a/tests/dagger/testsales.nim b/tests/dagger/testsales.nim index 4d83faf0..6220eeb7 100644 --- a/tests/dagger/testsales.nim +++ b/tests/dagger/testsales.nim @@ -24,10 +24,10 @@ suite "Sales": setup: market = MockMarket.new() sales = Sales.new(market) - sales.start() + await sales.start() teardown: - sales.stop() + await sales.stop() test "has no availability initially": check sales.available.len == 0