mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-02 21:43:11 +00:00
* cleanup imports and logs * add BlockHandle type * revert deps * refactor: async error handling and future tracking improvements - Update async procedures to use explicit raises annotation - Modify TrackedFutures to handle futures with no raised exceptions - Replace `asyncSpawn` with explicit future tracking - Update test suites to use `unittest2` - Standardize error handling across network and async components - Remove deprecated error handling patterns This commit introduces a more robust approach to async error handling and future management, improving type safety and reducing potential runtime errors. * bump nim-serde * remove asyncSpawn * rework background downloads and prefetch * imporove logging * refactor: enhance async procedures with error handling and raise annotations * misc cleanup * misc * refactor: implement allFinishedFailed to aggregate future results with success and failure tracking * refactor: update error handling in reader procedures to raise ChunkerError and CancelledError * refactor: improve error handling in wantListHandler and accountHandler procedures * refactor: simplify LPStreamReadError creation by consolidating parameters * refactor: enhance error handling in AsyncStreamWrapper to catch unexpected errors * refactor: enhance error handling in advertiser and discovery loops to improve resilience * misc * refactor: improve code structure and readability * remove cancellation from addSlotToQueue * refactor: add assertion for unexpected errors in local store checks * refactor: prevent tracking of finished futures and improve test assertions * refactor: improve error handling in local store checks * remove usage of msgDetail * feat: add initial implementation of discovery engine and related components * refactor: improve task scheduling logic by removing unnecessary break statement * break after scheduling a task * make taskHandler cancelable * refactor: update async handlers to raise CancelledError * refactor(advertiser): streamline error handling and improve task flow in advertise loops * fix: correct spelling of "divisible" in error messages and comments * refactor(discovery): simplify discovery task loop and improve error handling * refactor(engine): filter peers before processing in cancelBlocks procedure
114 lines
3.6 KiB
Nim
114 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: [ChunkerError, CancelledError]).} =
|
|
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":
|
|
try:
|
|
await raiseStreamException(newException(LPStreamError, "test error"))
|
|
except ChunkerError as exc:
|
|
check exc.parent of LPStreamError
|
|
except CatchableError as exc:
|
|
checkpoint("Unexpected error: " & exc.msg)
|
|
fail()
|
|
|
|
test "stream should catch LPStreamEOFError":
|
|
await raiseStreamException(newException(LPStreamEOFError, "test error"))
|
|
|
|
test "stream should forward CancelledError":
|
|
expect CancelledError:
|
|
await raiseStreamException(newException(CancelledError, "test error"))
|