[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") ContractInteractions.new("ws://localhost:8545")
proc start*(interactions: ContractInteractions) {.async.} = proc start*(interactions: ContractInteractions) {.async.} =
interactions.sales.start() await interactions.sales.start()
await interactions.proving.start() await interactions.proving.start()
proc stop*(interactions: ContractInteractions) {.async.} = proc stop*(interactions: ContractInteractions) {.async.} =
interactions.sales.stop() await interactions.sales.stop()
await interactions.proving.stop() await interactions.proving.stop()

View File

@ -135,28 +135,21 @@ proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) =
asyncSpawn negotiation.start() asyncSpawn negotiation.start()
proc start*(sales: Sales) = proc start*(sales: Sales) {.async.} =
doAssert sales.subscription.isNone, "Sales already started" doAssert sales.subscription.isNone, "Sales already started"
proc onRequest(requestId: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} = proc onRequest(requestId: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} =
sales.handleRequest(requestId, ask) sales.handleRequest(requestId, ask)
proc subscribe {.async.} = try:
try: sales.subscription = some await sales.market.subscribeRequests(onRequest)
sales.subscription = some await sales.market.subscribeRequests(onRequest) except CatchableError as e:
except CatchableError as e: error "Unable to start sales", msg = e.msg
error "Unable to start sales", msg = e.msg
asyncSpawn subscribe() proc stop*(sales: Sales) {.async.} =
proc stop*(sales: Sales) =
if subscription =? sales.subscription: if subscription =? sales.subscription:
sales.subscription = Subscription.none sales.subscription = Subscription.none
try:
proc unsubscribe {.async.} = await subscription.unsubscribe()
try: except CatchableError as e:
await subscription.unsubscribe() warn "Unsubscribe failed", msg = e.msg
except CatchableError as e:
warn "Unsubscribe failed", msg = e.msg
asyncSpawn unsubscribe()

View File

@ -24,10 +24,10 @@ suite "Sales":
setup: setup:
market = MockMarket.new() market = MockMarket.new()
sales = Sales.new(market) sales = Sales.new(market)
sales.start() await sales.start()
teardown: teardown:
sales.stop() await sales.stop()
test "has no availability initially": test "has no availability initially":
check sales.available.len == 0 check sales.available.len == 0