Skip to content

Commit 970d929

Browse files
committed
Test error propagation when throwing during reading/writing
1 parent 8fd084b commit 970d929

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

Tests/HTTPServerTests/HTTPRequestConcludingAsyncReaderTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,35 @@ struct HTTPRequestConcludingAsyncReaderTests {
134134
}
135135
}
136136

137+
@Test("Throw while reading request")
138+
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
139+
func testThrowingWhileReadingRequest() async throws {
140+
let (stream, source) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream()
141+
142+
let bodyChunks = (0..<10).map { i in ByteBuffer(bytes: [i]) }
143+
for chunk in bodyChunks {
144+
source.yield(.body(chunk))
145+
}
146+
source.yield(.end([.cookie: "test"]))
147+
source.finish()
148+
149+
// Check that the read error is propagated
150+
try await #require(throws: TestError.errorWhileReading) {
151+
let requestReader = HTTPRequestConcludingAsyncReader(
152+
iterator: stream.makeAsyncIterator(),
153+
readerState: .init()
154+
)
155+
156+
_ = try await requestReader.consumeAndConclude { bodyReader in
157+
var bodyReader = bodyReader
158+
159+
try await bodyReader.read { element in
160+
throw TestError.errorWhileReading
161+
}
162+
}
163+
}
164+
}
165+
137166
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
138167
@Test("More bytes available than consumption limit")
139168
func testCollectMoreBytesThanAvailable() async throws {

Tests/HTTPServerTests/HTTPResponseConcludingAsyncWriterTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ struct HTTPResponseConcludingAsyncWriterTests {
5555
#expect(trailer == .end(self.trailerSampleOne))
5656
}
5757

58+
@Test("Throw while writing response")
59+
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
60+
func testThrowWhileProducing() async throws {
61+
let (writer, sink) = NIOAsyncChannelOutboundWriter<HTTPResponsePart>.makeTestingWriter()
62+
63+
// Check that the write error is propagated
64+
try await #require(throws: TestError.errorWhileWriting) {
65+
let responseWriter = HTTPResponseConcludingAsyncWriter(writer: writer, writerState: .init())
66+
try await responseWriter.produceAndConclude { bodyWriter in
67+
var bodyWriter = bodyWriter
68+
69+
// Write an element
70+
try await bodyWriter.write(self.bodySampleOne.span)
71+
// Then throw
72+
throw TestError.errorWhileWriting
73+
}
74+
}
75+
76+
var responseIterator = sink.makeAsyncIterator()
77+
78+
let firstElement = try #require(await responseIterator.next())
79+
#expect(firstElement == .body(.init(bytes: self.bodySampleOne)))
80+
}
81+
5882
@Test("Write multiple elements and multiple trailers")
5983
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
6084
func testProduceMultipleElementsAndMultipleTrailers() async throws {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// An error type for use in tests
2+
enum TestError: Error {
3+
case errorWhileReading
4+
case errorWhileWriting
5+
}

0 commit comments

Comments
 (0)