nim-codex/tests/codex/testpurchasing.nim

107 lines
3.6 KiB
Nim
Raw Normal View History

2022-03-23 12:57:48 +00:00
import std/times
import pkg/asynctest
import pkg/chronos
import pkg/upraises
2022-03-23 12:57:48 +00:00
import pkg/stint
2022-05-19 19:56:03 +00:00
import pkg/codex/purchasing
2022-03-23 12:57:48 +00:00
import ./helpers/mockmarket
import ./helpers/mockclock
2022-03-23 12:57:48 +00:00
import ./examples
suite "Purchasing":
var purchasing: Purchasing
var market: MockMarket
var clock: MockClock
var request: StorageRequest
2022-03-23 12:57:48 +00:00
setup:
market = MockMarket.new()
clock = MockClock.new()
purchasing = Purchasing.new(market, clock)
request = StorageRequest(
ask: StorageAsk(
2022-08-02 12:21:12 +00:00
slots: uint8.example.uint64,
slotSize: uint32.example.u256,
duration: uint16.example.u256,
2022-08-02 12:21:12 +00:00
reward: uint8.example.u256
)
)
2022-03-23 12:57:48 +00:00
test "submits a storage request when asked":
discard purchasing.purchase(request)
let submitted = market.requested[0]
2022-08-02 12:21:12 +00:00
check submitted.ask.slots == request.ask.slots
check submitted.ask.slotSize == request.ask.slotSize
check submitted.ask.duration == request.ask.duration
2022-07-20 12:11:00 +00:00
check submitted.ask.reward == request.ask.reward
2022-03-23 12:57:48 +00:00
test "remembers purchases":
let purchase1 = purchasing.purchase(request)
let purchase2 = purchasing.purchase(request)
check purchasing.getPurchase(purchase1.id) == some purchase1
check purchasing.getPurchase(purchase2.id) == some purchase2
2022-03-23 12:57:48 +00:00
test "has a default value for proof probability":
check purchasing.proofProbability != 0.u256
test "can change default value for proof probability":
purchasing.proofProbability = 42.u256
discard purchasing.purchase(request)
check market.requested[0].ask.proofProbability == 42.u256
2022-03-23 12:57:48 +00:00
test "can override proof probability per request":
request.ask.proofProbability = 42.u256
discard purchasing.purchase(request)
check market.requested[0].ask.proofProbability == 42.u256
2022-03-23 12:57:48 +00:00
test "has a default value for request expiration interval":
check purchasing.requestExpiryInterval != 0.u256
test "can change default value for request expiration interval":
purchasing.requestExpiryInterval = 42.u256
let start = getTime().toUnix()
discard purchasing.purchase(request)
check market.requested[0].expiry == (start + 42).u256
2022-03-23 12:57:48 +00:00
test "can override expiry time per request":
let expiry = (getTime().toUnix() + 42).u256
request.expiry = expiry
discard purchasing.purchase(request)
check market.requested[0].expiry == expiry
2022-03-23 12:57:48 +00:00
test "includes a random nonce in every storage request":
discard purchasing.purchase(request)
discard purchasing.purchase(request)
check market.requested[0].nonce != market.requested[1].nonce
2022-03-28 10:28:22 +00:00
test "succeeds when request is fulfilled":
2022-03-28 10:28:22 +00:00
let purchase = purchasing.purchase(request)
let request = market.requested[0]
market.emitRequestFulfilled(request.id)
2022-03-28 10:28:22 +00:00
await purchase.wait()
check purchase.error.isNone
2022-03-28 12:40:41 +00:00
test "fails when request times out":
2022-03-28 12:40:41 +00:00
let purchase = purchasing.purchase(request)
let request = market.requested[0]
clock.set(request.expiry.truncate(int64))
expect PurchaseTimeout:
await purchase.wait()
test "supports request cancelled subscription when request times out":
let purchase = purchasing.purchase(request)
let request = market.requested[0]
var receivedIds: seq[RequestId]
clock.set(request.expiry.truncate(int64))
proc onRequestCancelled(id: RequestId) {.gcsafe, upraises:[].} =
receivedIds.add(id)
let subscription = await market.subscribeRequestCancelled(
request.id,
onRequestCancelled)
try:
await purchase.wait()
except PurchaseTimeout:
check receivedIds == @[request.id]
await subscription.unsubscribe()