nim-dagger/tests/codex/testchunking.nim
Arnaud f25c555d59
Chore/update nim version (#1052)
* Move to version 2.0.6

* Update nim-confutils submodule to latest version

* Update dependencies

* Update Nim version to 2.0.12

* Add gcsafe pragma

* Add missing import

* Update specific conf for Nim 2.x

* Fix method signatures

* Revert erasure coding attempt to fix bug

* More gcsafe pragma

* Duplicate code from libp2p because it is not exported anymore

* Fix camelcase function names

* Use alreadySeen because need is not a bool anymore

* newLPStreamReadError does not exist anymore so use another error

* Replace ValidIpAddress by IpAddress

* Add gcsafe pragma

* Restore maintenance parameter deleted by mistake when removing esasure coding fix attempt code

* Update method signatures

* Copy LPStreamReadError code from libp2p which was removed

* Fix camel case

* Fix enums in tests

* Fix camel case

* Extract node components to a variable to make Nim 2 happy

* Update the tests using ValidIpAddress to IpAddress

* Fix cast for value which is already an option

* Set nim version to 2.0.x for CI

* Set nim version to 2.0.x for CI

* Move to miniupnp version 2.2.4 to avoid symlink error

* Set core.symlinks to false for Windows for miniupnp >= 2.2.5 support

* Update to Nim 2.0.14

* Update CI nim versions to 2.0.14

* Try with GCC 14

* Replace apt-fast by apt-get

* Update ubuntu runner to latest

* Use Ubuntu 20.04 for coverage

* Disable CI cache for coverage

* Add coverage property description

* Remove commented test

* Check the node value of seen instead of using alreadySeen

* Fix the merge. The taskpool work was reverted.

* Update nim-ethers submodule

* Remove deprecated ValidIpAddress. Fix missing case and imports.

* Fix a weird issue where nim-confutils cannot find NatAny

* Fix tests and remove useless static keyword
2025-01-10 14:12:37 +00:00

121 lines
3.6 KiB
Nim

import pkg/stew/byteutils
import pkg/codex/chunker
import pkg/codex/logutils
import pkg/chronos
import ../asynctest
import ./helpers
# Trying to use a CancelledError or LPStreamError value for toRaise
# will produce a compilation error;
# Error: only a 'ref object' can be raised
# This is because they are not ref object but plain object.
# CancelledError* = object of FutureError
# LPStreamError* = object of LPError
type
CrashingStreamWrapper* = ref object of LPStream
toRaise*: proc(): void {.gcsafe, raises: [CancelledError, LPStreamError].}
method readOnce*(
self: CrashingStreamWrapper,
pbytes: pointer,
nbytes: int
): Future[int] {.gcsafe, async: (raises: [CancelledError, LPStreamError]).} =
self.toRaise()
asyncchecksuite "Chunking":
test "should return proper size chunks":
var offset = 0
let contents = [1.byte, 2, 3, 4, 5, 6, 7, 8, 9, 0]
proc reader(data: ChunkBuffer, len: int): Future[int]
{.gcsafe, async, raises: [Defect].} =
let read = min(contents.len - offset, len)
if read == 0:
return 0
copyMem(data, unsafeAddr contents[offset], read)
offset += read
return read
let chunker = Chunker.new(
reader = reader,
chunkSize = 2'nb)
check:
(await chunker.getBytes()) == [1.byte, 2]
(await chunker.getBytes()) == [3.byte, 4]
(await chunker.getBytes()) == [5.byte, 6]
(await chunker.getBytes()) == [7.byte, 8]
(await chunker.getBytes()) == [9.byte, 0]
(await chunker.getBytes()) == []
chunker.offset == offset
test "should chunk LPStream":
let stream = BufferStream.new()
let chunker = LPStreamChunker.new(
stream = stream,
chunkSize = 2'nb)
proc writer() {.async.} =
for d in [@[1.byte, 2, 3, 4], @[5.byte, 6, 7, 8], @[9.byte, 0]]:
await stream.pushData(d)
await stream.pushEof()
await stream.close()
let writerFut = writer()
check:
(await chunker.getBytes()) == [1.byte, 2]
(await chunker.getBytes()) == [3.byte, 4]
(await chunker.getBytes()) == [5.byte, 6]
(await chunker.getBytes()) == [7.byte, 8]
(await chunker.getBytes()) == [9.byte, 0]
(await chunker.getBytes()) == []
chunker.offset == 10
await writerFut
test "should chunk file":
let
path = currentSourcePath()
file = open(path)
fileChunker = FileChunker.new(file = file, chunkSize = 256'nb, pad = false)
var data: seq[byte]
while true:
let buff = await fileChunker.getBytes()
if buff.len <= 0:
break
check buff.len <= fileChunker.chunkSize.int
data.add(buff)
check:
string.fromBytes(data) == readFile(path)
fileChunker.offset == data.len
proc raiseStreamException(exc: ref CancelledError | ref LPStreamError) {.async.} =
let stream = CrashingStreamWrapper.new()
let chunker = LPStreamChunker.new(
stream = stream,
chunkSize = 2'nb)
stream.toRaise = proc(): void {.raises: [CancelledError, LPStreamError].} =
raise exc
discard (await chunker.getBytes())
test "stream should forward LPStreamError":
expect LPStreamError:
await raiseStreamException(newException(LPStreamError, "test error"))
test "stream should catch LPStreamEOFError":
await raiseStreamException(newException(LPStreamEOFError, "test error"))
test "stream should forward CancelledError":
expect CancelledError:
await raiseStreamException(newException(CancelledError, "test error"))
test "stream should forward LPStreamError":
expect LPStreamError:
await raiseStreamException(newException(LPStreamError, "test error"))