[sales] Ensure that Sales is completely stopped

This commit is contained in:
Mark Spanbroek 2022-05-17 16:40:57 +02:00 committed by markspanbroek
parent 3f627790f8
commit 8d19476a4c
3 changed files with 14 additions and 21 deletions

View File

@ -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()

View File

@ -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
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()

View File

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