[sales] Ensure that Sales is completely stopped
This commit is contained in:
parent
3f627790f8
commit
8d19476a4c
|
@ -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()
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
proc unsubscribe {.async.} =
|
|
||||||
try:
|
try:
|
||||||
await subscription.unsubscribe()
|
await subscription.unsubscribe()
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
warn "Unsubscribe failed", msg = e.msg
|
warn "Unsubscribe failed", msg = e.msg
|
||||||
|
|
||||||
asyncSpawn unsubscribe()
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue