Skip to content

Commit

Permalink
Adds tests for other stream exceptions. Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed Oct 10, 2024
1 parent 53fea6c commit eb7dfc0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions codex/chunker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ proc new*(
except CancelledError as error:
raise error
except LPStreamError as error:
warn "LPStream error", err = error.msg
error "LPStream error", err = error.msg
raise error
except CatchableError as exc:
trace "CatchableError exception", exc = exc.msg
error "CatchableError exception", exc = exc.msg
raise newException(Defect, exc.msg)

return res
Expand Down Expand Up @@ -130,7 +130,7 @@ proc new*(
except CancelledError as error:
raise error
except CatchableError as exc:
trace "CatchableError exception", exc = exc.msg
error "CatchableError exception", exc = exc.msg
raise newException(Defect, exc.msg)

return total
Expand Down
24 changes: 20 additions & 4 deletions tests/codex/testchunking.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import pkg/stew/byteutils
import pkg/codex/chunker
import pkg/codex/logutils
Expand Down Expand Up @@ -89,13 +88,30 @@ asyncchecksuite "Chunking":
string.fromBytes(data) == readFile(path)
fileChunker.offset == data.len

test "Should forward LPStreamError":
proc raiseStreamException(exc: ref CatchableError) {.async.} =
let stream = CrashingStreamWrapper.new()
let chunker = LPStreamChunker.new(
stream = stream,
chunkSize = 2'nb)

stream.toRaise = newException(LPStreamError, "test error")
stream.toRaise = 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:
discard (await chunker.getBytes())
await raiseStreamException(newException(LPStreamError, "test error"))

test "stream should convert other exceptions to defect":
expect Defect:
await raiseStreamException(newException(CatchableError, "test error"))

0 comments on commit eb7dfc0

Please sign in to comment.