Skip to content

Commit 8277b0f

Browse files
committed
Code review
1 parent a194dea commit 8277b0f

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift

+21-8
Original file line numberDiff line numberDiff line change
@@ -1149,14 +1149,27 @@ struct CollectEverythingLogHandler: LogHandler {
11491149
}
11501150
}
11511151

1152-
class StreamDelegate: HTTPClientResponseDelegate {
1152+
/// A ``HTTPClientResponseDelegate`` that buffers the incoming response parts for the consumer. The consumer can
1153+
/// consume the bytes by calling ``next()`` on the delegate.
1154+
///
1155+
/// The sole purpose of this class is to enable straight-line stream tests.
1156+
class ResponseStreamDelegate: HTTPClientResponseDelegate {
11531157
typealias Response = Void
11541158

11551159
enum State {
1160+
/// The delegate is in the idle state. There are no http response parts to be buffered
1161+
/// and the consumer did not signal a demand. Transitions to all other states are allowed.
11561162
case idle
1163+
/// The consumer has signaled a demand for more bytes, but none where available. Can
1164+
/// transition to `.idle` (when new bytes arrive), `.finished` (when the stream finishes or fails)
11571165
case waitingForBytes(EventLoopPromise<ByteBuffer?>)
1166+
/// The consumer has signaled no further demand but bytes keep arriving. Valid transitions
1167+
/// to `.idle` (when bytes are consumed), `.finished` (when bytes are consumed, and the
1168+
/// stream has ended), `.failed` (if an error is forwarded)
11581169
case buffering(ByteBuffer, done: Bool)
1170+
/// Stores an error for consumption. Valid transitions are: `.finished`, when the error was consumed.
11591171
case failed(Error)
1172+
/// The stream has finished and all bytes or errors where consumed.
11601173
case finished
11611174
}
11621175

@@ -1207,24 +1220,24 @@ class StreamDelegate: HTTPClientResponseDelegate {
12071220
// MARK: HTTPClientResponseDelegate
12081221

12091222
func didSendRequestHead(task: HTTPClient.Task<Response>, _ head: HTTPRequestHead) {
1210-
XCTAssert(self.eventLoop.inEventLoop)
1223+
self.eventLoop.preconditionInEventLoop()
12111224
}
12121225

12131226
func didSendRequestPart(task: HTTPClient.Task<Response>, _ part: IOData) {
1214-
XCTAssert(self.eventLoop.inEventLoop)
1227+
self.eventLoop.preconditionInEventLoop()
12151228
}
12161229

12171230
func didSendRequest(task: HTTPClient.Task<Response>) {
1218-
XCTAssert(self.eventLoop.inEventLoop)
1231+
self.eventLoop.preconditionInEventLoop()
12191232
}
12201233

12211234
func didReceiveHead(task: HTTPClient.Task<Response>, _ head: HTTPResponseHead) -> EventLoopFuture<Void> {
1222-
XCTAssert(self.eventLoop.inEventLoop)
1235+
self.eventLoop.preconditionInEventLoop()
12231236
return task.eventLoop.makeSucceededVoidFuture()
12241237
}
12251238

12261239
func didReceiveBodyPart(task: HTTPClient.Task<Response>, _ buffer: ByteBuffer) -> EventLoopFuture<Void> {
1227-
XCTAssert(self.eventLoop.inEventLoop)
1240+
self.eventLoop.preconditionInEventLoop()
12281241

12291242
switch self.state {
12301243
case .idle:
@@ -1244,7 +1257,7 @@ class StreamDelegate: HTTPClientResponseDelegate {
12441257
}
12451258

12461259
func didReceiveError(task: HTTPClient.Task<Response>, _ error: Error) {
1247-
XCTAssert(self.eventLoop.inEventLoop)
1260+
self.eventLoop.preconditionInEventLoop()
12481261

12491262
switch self.state {
12501263
case .idle:
@@ -1260,7 +1273,7 @@ class StreamDelegate: HTTPClientResponseDelegate {
12601273
}
12611274

12621275
func didFinishRequest(task: HTTPClient.Task<Response>) throws {
1263-
XCTAssert(self.eventLoop.inEventLoop)
1276+
self.eventLoop.preconditionInEventLoop()
12641277

12651278
switch self.state {
12661279
case .idle:

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,7 @@ class HTTPClientTests: XCTestCase {
28312831
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))
28322832
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) }
28332833

2834-
let delegate = StreamDelegate(eventLoop: delegateEL)
2834+
let delegate = ResponseStreamDelegate(eventLoop: delegateEL)
28352835

28362836
let body: HTTPClient.Body = .stream { writer in
28372837
let finalPromise = writeEL.makePromise(of: Void.self)

0 commit comments

Comments
 (0)