nim-dagger/tests/codex/teststorestream.nim
Jaremy Creechley 7efa9177df
Bump deps take2 (#492)
* extra utilities and tweaks
* add atlas lock
* update ignores
* break build into it's own script
* update url rules
* base off codexdht's
* compile fixes for Nim 1.6.14
* update submodules
* convert mapFailure to procs to work around type resolution issues
* add toml parser for multiaddress
* change error type on keyutils
* bump nimbus build to use 1.6.14
* update gitignore
* adding new deps submodules
* bump nim ci version
* even more fixes
* more libp2p changes
* update keys
* fix eventually function
* adding coverage test file
* move coverage to build.nims
* use nimcache/coverage
* move libp2p import for tests into helper.nim
* remove named bin
* bug fixes for networkpeers (from Dmitriy)

---------

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
2023-08-01 16:47:57 -07:00

108 lines
2.6 KiB
Nim

import pkg/chronos
import pkg/asynctest
import pkg/questionable/results
import ./helpers
import pkg/codex/streams
import pkg/codex/stores
import pkg/codex/manifest
import pkg/codex/blocktype as bt
asyncchecksuite "StoreStream":
var
manifest: Manifest
store: BlockStore
stream: StoreStream
# Check that `buf` contains `size` bytes with values start, start+1...
proc sequentialBytes(buf: seq[byte], size: int, start: int): bool =
for i in 0..<size:
if int(buf[i]) != start+i:
return false
return true
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],
]
teardown:
await stream.close()
setup:
store = CacheStore.new()
manifest = Manifest.new(blockSize = 10'nb).tryGet()
stream = StoreStream.new(store, manifest)
for d in data:
let blk = bt.Block.new(d).tryGet()
manifest.add(blk.cid)
(await store.putBlock(blk)).tryGet()
test "Read all blocks < blockSize":
var
buf = newSeq[byte](8)
n = 0
while not stream.atEof:
let read = (await stream.readOnce(addr buf[0], buf.len))
if not stream.atEof:
check read == 8
else:
check read == 4
check sequentialBytes(buf,read,n)
n += read
test "Read all blocks == blockSize":
var
buf = newSeq[byte](10)
n = 0
while not stream.atEof:
let read = (await stream.readOnce(addr buf[0], buf.len))
check read == 10
check sequentialBytes(buf,read,n)
n += read
test "Read all blocks > blockSize":
var
buf = newSeq[byte](11)
n = 0
while not stream.atEof:
let read = (await stream.readOnce(addr buf[0], buf.len))
if not stream.atEof:
check read == 11
else:
check read == 1
check sequentialBytes(buf,read,n)
n += read
test "Read exact bytes within block boundary":
var
buf = newSeq[byte](5)
await stream.readExactly(addr buf[0], 5)
check sequentialBytes(buf,5,0)
test "Read exact bytes outside of block boundary":
var
buf = newSeq[byte](15)
await stream.readExactly(addr buf[0], 15)
check sequentialBytes(buf,15,0)