nim-codex/tests/dagger/teststorestream.nim
Dmitriy Ryajov d3dbbc75fa
Extract Discovery engine (#99)
* don't force logging syncs

* Add failing test

* wip discovery engine

* re-add chronicles sinks

* wip

* move network related stuff to own folder

* move peer related stuff to own folder

* extract discovery into it's own engine

* update imports

* move pending blocks into engine module

* add top level exports

* update imports

* update import paths

* update imports

* support for inflight request filtering and tests

* use `remove` instead of `del`

* fix sorting in `selectCheapest`

* re-org test file structure

* fix to use discovery engine

* file re-org

* fix compilation

* fixup discovery to use async handlers

* more re-org

* rework with support for discovery engine

* add logging

* use defaults

* wip: reworking with discoveryengine

* wip: more test fixes

* more logging

* use ordered table

* use `bt` for blocktype Block

* fix tests

* make tests work with discovery engine

* expose all node components

* fix to work with discovery engine

* wip

* propagate cancellation in listBlocks

* start/stop disc engine in blockexc engine

* remove disc engine start/stop

* wire up discovery engine

* misc comments and imports

* pass discovery to dagger node

* set sleep timers

* unused imports

* misc

* don't spawn a task, await it

* don't await handlers

* trace logging

* reduce default sleep time

Co-authored-by: Tanguy <tanguy@status.im>
2022-05-18 20:29:15 -06:00

96 lines
2.3 KiB
Nim

import pkg/chronos
import pkg/asynctest
import pkg/libp2p
import pkg/questionable/results
import ./helpers
import pkg/dagger/streams
import pkg/dagger/stores
import pkg/dagger/manifest
import pkg/dagger/rng
import pkg/dagger/blocktype as bt
suite "StoreStream":
var
manifest: Manifest
store: BlockStore
stream: StoreStream
let
data = [
[byte 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[byte 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[byte 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[byte 30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[byte 40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[byte 50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[byte 60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[byte 70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[byte 80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[byte 90, 91, 92, 93, 94, 95, 96, 97, 98, 99],
]
setup:
store = CacheStore.new()
manifest = Manifest.new(blockSize = 10).tryGet()
stream = StoreStream.new(store, manifest)
for d in data:
let
blk = bt.Block.new(d).tryGet()
manifest.add(blk.cid)
if not (await store.putBlock(blk)):
raise newException(CatchableError, "Unable to store block " & $blk.cid)
test "Read all blocks < blockSize":
var
buf = newSeq[byte](8)
while not stream.atEof:
let
read = (await stream.readOnce(addr buf[0], buf.len))
if stream.atEof.not:
check read == 8
else:
check read == 4
test "Read all blocks == blockSize":
var
buf = newSeq[byte](10)
while not stream.atEof:
let
read = (await stream.readOnce(addr buf[0], buf.len))
check read == 10
test "Read all blocks > blockSize":
var
buf = newSeq[byte](11)
while not stream.atEof:
let
read = (await stream.readOnce(addr buf[0], buf.len))
if stream.atEof.not:
check read == 11
else:
check read == 1
test "Read exact bytes within block boundary":
var
buf = newSeq[byte](5)
await stream.readExactly(addr buf[0], 5)
check buf == [byte 0, 1, 2, 3, 4]
test "Read exact bytes outside of block boundary":
var
buf = newSeq[byte](15)
await stream.readExactly(addr buf[0], 15)
check buf == [byte 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]