2022-11-15 06:12:05 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/algorithm
|
|
|
|
|
|
|
|
import pkg/chronos
|
2024-01-10 21:25:57 +00:00
|
|
|
import pkg/asynctest/chronos/unittest
|
2022-11-15 06:12:05 +00:00
|
|
|
import pkg/stew/byteutils
|
|
|
|
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/blockexchange
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
import ../helpers
|
|
|
|
|
|
|
|
checksuite "Pending Blocks":
|
2022-11-15 06:12:05 +00:00
|
|
|
test "Should add want handle":
|
|
|
|
let
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
blk = bt.Block.new("Hello".toBytes).tryGet
|
2023-03-09 11:23:45 +00:00
|
|
|
|
|
|
|
discard pendingBlocks.getWantHandle(blk.cid)
|
2022-11-15 06:12:05 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
check blk.cid in pendingBlocks
|
2022-11-15 06:12:05 +00:00
|
|
|
|
|
|
|
test "Should resolve want handle":
|
|
|
|
let
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
blk = bt.Block.new("Hello".toBytes).tryGet
|
|
|
|
handle = pendingBlocks.getWantHandle(blk.cid)
|
|
|
|
|
|
|
|
check blk.cid in pendingBlocks
|
2023-11-14 12:02:17 +00:00
|
|
|
pendingBlocks.resolve(@[blk].mapIt(BlockDelivery(blk: it, address: it.address)))
|
2022-11-15 06:12:05 +00:00
|
|
|
check (await handle) == blk
|
|
|
|
check blk.cid notin pendingBlocks
|
|
|
|
|
|
|
|
test "Should cancel want handle":
|
|
|
|
let
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
blk = bt.Block.new("Hello".toBytes).tryGet
|
|
|
|
handle = pendingBlocks.getWantHandle(blk.cid)
|
|
|
|
|
|
|
|
check blk.cid in pendingBlocks
|
|
|
|
await handle.cancelAndWait()
|
|
|
|
check blk.cid notin pendingBlocks
|
|
|
|
|
|
|
|
test "Should expire want handle":
|
|
|
|
let
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
blk = bt.Block.new("Hello".toBytes).tryGet
|
|
|
|
handle = pendingBlocks.getWantHandle(blk.cid, 1.millis)
|
|
|
|
|
|
|
|
check blk.cid in pendingBlocks
|
|
|
|
|
|
|
|
await sleepAsync(10.millis)
|
|
|
|
expect AsyncTimeoutError:
|
|
|
|
discard await handle
|
|
|
|
|
|
|
|
check blk.cid notin pendingBlocks
|
|
|
|
|
|
|
|
test "Should get wants list":
|
|
|
|
let
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
blks = (0..9).mapIt( bt.Block.new(("Hello " & $it).toBytes).tryGet )
|
2023-03-09 11:23:45 +00:00
|
|
|
|
|
|
|
discard blks.mapIt( pendingBlocks.getWantHandle( it.cid ) )
|
2022-11-15 06:12:05 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
blks.mapIt( $it.cid ).sorted(cmp[string]) ==
|
2023-11-14 12:02:17 +00:00
|
|
|
toSeq(pendingBlocks.wantListBlockCids).mapIt( $it ).sorted(cmp[string])
|
2022-11-15 06:12:05 +00:00
|
|
|
|
|
|
|
test "Should get want handles list":
|
|
|
|
let
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
blks = (0..9).mapIt( bt.Block.new(("Hello " & $it).toBytes).tryGet )
|
|
|
|
handles = blks.mapIt( pendingBlocks.getWantHandle( it.cid ) )
|
|
|
|
wantHandles = toSeq(pendingBlocks.wantHandles)
|
|
|
|
|
|
|
|
check wantHandles.len == handles.len
|
2023-11-14 12:02:17 +00:00
|
|
|
pendingBlocks.resolve(blks.mapIt(BlockDelivery(blk: it, address: it.address)))
|
2022-11-15 06:12:05 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
(await allFinished(wantHandles)).mapIt( $it.read.cid ).sorted(cmp[string]) ==
|
|
|
|
(await allFinished(handles)).mapIt( $it.read.cid ).sorted(cmp[string])
|