Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rnro committed Sep 12, 2024
1 parent e52a349 commit 7c3d82c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/NIOCore/SingleStepByteToMessageDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ public final class NIOSingleStepByteToMessageProcessor<Decoder: NIOSingleStepByt
}

self._buffer = nil // To avoid CoW
defer { self._buffer = buffer }

let result = try body(&buffer)
self._buffer = buffer
return result
}

Expand Down Expand Up @@ -272,7 +273,6 @@ public final class NIOSingleStepByteToMessageProcessor<Decoder: NIOSingleStepByt
throw ByteToMessageDecoderError.PayloadTooLargeError()
}

// force unwrapping is safe because nil case is handled already and asserted above
if let readerIndex = self._buffer?.readerIndex, readerIndex > 0, self.decoder.shouldReclaimBytes(buffer: self._buffer!) {
self._buffer!.discardReadBytes()
}
Expand Down
11 changes: 9 additions & 2 deletions Tests/NIOCoreTests/SingleStepByteToMessageDecoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ public final class NIOSingleStepByteToMessageDecoderTest: XCTestCase {
struct DecodeLastError: Error {}

func decodeLast(buffer: inout NIOCore.ByteBuffer, seenEOF: Bool) throws -> NIOCore.ByteBuffer? {
buffer = ByteBuffer() // to allow the decode loop to exit
throw DecodeLastError()
}

Expand All @@ -574,10 +575,16 @@ public final class NIOSingleStepByteToMessageDecoderTest: XCTestCase {

let decoder = ThrowingOnLastDecoder()
let b2mp = NIOSingleStepByteToMessageProcessor(decoder)
var errorObserved = false
XCTAssertNoThrow(try b2mp.process(buffer: ByteBuffer(string: "1\n\n2\n3\n")) { line in
XCTAssertThrowsError(try b2mp.finishProcessing(seenEOF: true) { _ in }) { error in
XCTAssertNotNil(error as? ThrowingOnLastDecoder.DecodeLastError)
// We will throw an error to exit the decoding within the nested process call prematurely.
// Unless this is carefully handled we can be left in an inconsistent state which the outer call will encounter
do {
try b2mp.finishProcessing(seenEOF: true) { _ in }
} catch let error as ThrowingOnLastDecoder.DecodeLastError {
errorObserved = true
}
})
XCTAssertTrue(errorObserved)
}
}

0 comments on commit 7c3d82c

Please sign in to comment.