mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-02 13:33:10 +00:00
fix(sales): do not crash when retrieving request fails (#1248)
This commit is contained in:
parent
2e1306ac2d
commit
2dd436bfb7
@ -12,7 +12,7 @@ export asyncstatemachine
|
||||
|
||||
type
|
||||
SaleState* = ref object of State
|
||||
SaleError* = ref object of CodexError
|
||||
SaleError* = object of CodexError
|
||||
|
||||
method onCancelled*(
|
||||
state: SaleState, request: StorageRequest
|
||||
|
||||
@ -51,7 +51,9 @@ method run*(
|
||||
await agent.subscribe()
|
||||
|
||||
without request =? data.request:
|
||||
raiseAssert "no sale request"
|
||||
error "request could not be retrieved", id = data.requestId
|
||||
let error = newException(SaleError, "request could not be retrieved")
|
||||
return some State(SaleErrored(error: error))
|
||||
|
||||
let slotId = slotId(data.requestId, data.slotIndex)
|
||||
let state = await market.slotState(slotId)
|
||||
|
||||
@ -38,6 +38,11 @@ method run*(
|
||||
await agent.retrieveRequest()
|
||||
await agent.subscribe()
|
||||
|
||||
without request =? data.request:
|
||||
error "request could not be retrieved", id = data.requestId
|
||||
let error = newException(SaleError, "request could not be retrieved")
|
||||
return some State(SaleErrored(error: error))
|
||||
|
||||
let slotId = slotId(data.requestId, data.slotIndex)
|
||||
let slotState = await market.slotState(slotId)
|
||||
|
||||
|
||||
@ -72,6 +72,12 @@ asyncchecksuite "sales state 'preparing'":
|
||||
let next = state.onSlotFilled(request.id, slotIndex)
|
||||
check !next of SaleFilled
|
||||
|
||||
test "run switches to errored when the request cannot be retrieved":
|
||||
agent = newSalesAgent(context, request.id, slotIndex, StorageRequest.none)
|
||||
let next = !(await state.run(agent))
|
||||
check next of SaleErrored
|
||||
check SaleErrored(next).error.msg == "request could not be retrieved"
|
||||
|
||||
proc createAvailability(enabled = true) {.async.} =
|
||||
let a = await reservations.createAvailability(
|
||||
availability.totalSize,
|
||||
|
||||
@ -20,15 +20,22 @@ suite "sales state 'unknown'":
|
||||
let slotId = slotId(request.id, slotIndex)
|
||||
|
||||
var market: MockMarket
|
||||
var context: SalesContext
|
||||
var agent: SalesAgent
|
||||
var state: SaleUnknown
|
||||
|
||||
setup:
|
||||
market = MockMarket.new()
|
||||
let context = SalesContext(market: market)
|
||||
agent = newSalesAgent(context, request.id, slotIndex, StorageRequest.none)
|
||||
context = SalesContext(market: market)
|
||||
agent = newSalesAgent(context, request.id, slotIndex, request.some)
|
||||
state = SaleUnknown.new()
|
||||
|
||||
test "switches to error state when the request cannot be retrieved":
|
||||
agent = newSalesAgent(context, request.id, slotIndex, StorageRequest.none)
|
||||
let next = await state.run(agent)
|
||||
check !next of SaleErrored
|
||||
check SaleErrored(!next).error.msg == "request could not be retrieved"
|
||||
|
||||
test "switches to error state when on chain state cannot be fetched":
|
||||
let next = await state.run(agent)
|
||||
check !next of SaleErrored
|
||||
@ -37,6 +44,7 @@ suite "sales state 'unknown'":
|
||||
market.slotState[slotId] = SlotState.Free
|
||||
let next = await state.run(agent)
|
||||
check !next of SaleErrored
|
||||
check SaleErrored(!next).error.msg == "Slot state on chain should not be 'free'"
|
||||
|
||||
test "switches to filled state when on chain state is 'filled'":
|
||||
market.slotState[slotId] = SlotState.Filled
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user