-
Notifications
You must be signed in to change notification settings - Fork 125
Avoid precondition failure in write timeout #803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,8 @@ struct HTTP1ConnectionStateMachine { | |
case fireChannelActive | ||
case fireChannelInactive | ||
case fireChannelError(Error, closeConnection: Bool) | ||
|
||
case noAction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove. |
||
} | ||
|
||
private var state: State | ||
|
@@ -359,7 +361,7 @@ struct HTTP1ConnectionStateMachine { | |
|
||
mutating func idleWriteTimeoutTriggered() -> Action { | ||
guard case .inRequest(var requestStateMachine, let close) = self.state else { | ||
preconditionFailure("Invalid state: \(self.state)") | ||
return .noAction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mind adding another test to HTTP1ConnectionStateMachineTests that just tests this edge? |
||
} | ||
|
||
return self.avoidingStateMachineCoW { state -> Action in | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -840,6 +840,81 @@ class HTTP1ClientChannelHandlerTests: XCTestCase { | |
channel.writeAndFlush(request, promise: nil) | ||
XCTAssertEqual(request.events.map(\.kind), [.willExecuteRequest, .requestHeadSent]) | ||
} | ||
|
||
class SlowHandler: ChannelOutboundHandler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this isn't used at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, a relic from a previous test approach. |
||
typealias OutboundIn = HTTPClientRequestPart | ||
typealias OutboundOut = HTTPClientRequestPart | ||
|
||
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { | ||
context.eventLoop.scheduleTask(in: .milliseconds(300)) { | ||
promise?.succeed() | ||
} | ||
} | ||
} | ||
|
||
func testIdleWriteTimeoutOutsideOfRunningState() { | ||
let embedded = EmbeddedChannel() | ||
var maybeTestUtils: HTTP1TestTools? | ||
XCTAssertNoThrow(maybeTestUtils = try embedded.setupHTTP1Connection()) | ||
print("pipeline", embedded.pipeline) | ||
guard let testUtils = maybeTestUtils else { return XCTFail("Expected connection setup works") } | ||
|
||
var maybeRequest: HTTPClient.Request? | ||
XCTAssertNoThrow(maybeRequest = try HTTPClient.Request(url: "http://localhost/")) | ||
guard var request = maybeRequest else { return XCTFail("Expected to be able to create a request") } | ||
|
||
// start a request stream we'll never write to | ||
let streamPromise = embedded.eventLoop.makePromise(of: Void.self) | ||
let streamCallback = { @Sendable (streamWriter: HTTPClient.Body.StreamWriter) -> EventLoopFuture<Void> in | ||
streamPromise.futureResult | ||
} | ||
request.body = .init(contentLength: nil, stream: streamCallback) | ||
|
||
let delegate = NullResponseDelegate() | ||
var maybeRequestBag: RequestBag<NullResponseDelegate>? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hadn't spotted that, thanks. |
||
XCTAssertNoThrow( | ||
maybeRequestBag = try RequestBag( | ||
request: request, | ||
eventLoopPreference: .delegate(on: embedded.eventLoop), | ||
task: .init(eventLoop: embedded.eventLoop, logger: testUtils.logger), | ||
redirectHandler: nil, | ||
connectionDeadline: .now() + .seconds(30), | ||
requestOptions: .forTests( | ||
idleReadTimeout: .milliseconds(10), | ||
idleWriteTimeout: .milliseconds(2) | ||
), | ||
delegate: delegate | ||
) | ||
) | ||
guard let requestBag = maybeRequestBag else { return XCTFail("Expected to be able to create a request bag") } | ||
|
||
testUtils.connection.executeRequest(requestBag) | ||
|
||
XCTAssertNoThrow( | ||
try embedded.receiveHeadAndVerify { | ||
XCTAssertEqual($0.method, .GET) | ||
XCTAssertEqual($0.uri, "/") | ||
XCTAssertEqual($0.headers.first(name: "host"), "localhost") | ||
} | ||
) | ||
|
||
// close the pipeline to simulate a server-side close | ||
// note this happens before we write so the idle write timeout is still running | ||
try! embedded.pipeline.close().wait() | ||
|
||
// advance time to trigger the idle write timeout | ||
// and ensure that the state machine can tolerate this | ||
embedded.embeddedEventLoop.advanceTime(by: .milliseconds(250)) | ||
} | ||
} | ||
|
||
class NullResponseDelegate: HTTPClientResponseDelegate { | ||
typealias Response = Void | ||
|
||
func didFinishRequest(task: AsyncHTTPClient.HTTPClient.Task<Void>) throws { | ||
() | ||
} | ||
|
||
} | ||
|
||
class TestBackpressureWriter { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this new state or can we just re-use
.wait
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reuse
.wait
..wait
is a do nothing. can be renamed in a follow up pr.