Skip to content

Fix request head continuation misuse #666

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

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ extension Transaction {
self.state = .executing(context, .requestHeadSent, .waitingForResponseHead)
return .none
case .deadlineExceededWhileQueued(let continuation):
return .cancelAndFail(executor, continuation, with: HTTPClientError.deadlineExceeded)
let error = HTTPClientError.deadlineExceeded
self.state = .finished(error: error, nil)
return .cancelAndFail(executor, continuation, with: error)

case .finished(error: .some, .none):
return .cancel(executor)
Expand Down
1 change: 1 addition & 0 deletions Tests/AsyncHTTPClientTests/TransactionTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension TransactionTests {
static var allTests: [(String, (TransactionTests) -> () throws -> Void)] {
return [
("testCancelAsyncRequest", testCancelAsyncRequest),
("testDeadlineExceededWhileQueuedAndExecutorImmediatelyCancelsTask", testDeadlineExceededWhileQueuedAndExecutorImmediatelyCancelsTask),
("testResponseStreamingWorks", testResponseStreamingWorks),
("testIgnoringResponseBodyWorks", testIgnoringResponseBodyWorks),
("testWriteBackpressureWorks", testWriteBackpressureWorks),
Expand Down
49 changes: 49 additions & 0 deletions Tests/AsyncHTTPClientTests/TransactionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,55 @@ final class TransactionTests: XCTestCase {
}
}

func testDeadlineExceededWhileQueuedAndExecutorImmediatelyCancelsTask() {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return }
XCTAsyncTest {
let embeddedEventLoop = EmbeddedEventLoop()
defer { XCTAssertNoThrow(try embeddedEventLoop.syncShutdownGracefully()) }

var request = HTTPClientRequest(url: "https://localhost/")
request.method = .GET
var maybePreparedRequest: PreparedRequest?
XCTAssertNoThrow(maybePreparedRequest = try PreparedRequest(request))
guard let preparedRequest = maybePreparedRequest else {
return XCTFail("Expected to have a request here.")
}
let (transaction, responseTask) = await Transaction.makeWithResultTask(
request: preparedRequest,
preferredEventLoop: embeddedEventLoop
)

let queuer = MockTaskQueuer()
transaction.requestWasQueued(queuer)

transaction.deadlineExceeded()

struct Executor: HTTPRequestExecutor {
func writeRequestBodyPart(_: NIOCore.IOData, request: AsyncHTTPClient.HTTPExecutableRequest, promise: NIOCore.EventLoopPromise<Void>?) {
XCTFail()
}

func finishRequestBodyStream(_ task: AsyncHTTPClient.HTTPExecutableRequest, promise: NIOCore.EventLoopPromise<Void>?) {
XCTFail()
}

func demandResponseBodyStream(_: AsyncHTTPClient.HTTPExecutableRequest) {
XCTFail()
}

func cancelRequest(_ task: AsyncHTTPClient.HTTPExecutableRequest) {
task.fail(HTTPClientError.cancelled)
}
}

transaction.willExecuteRequest(Executor())

await XCTAssertThrowsError(try await responseTask.value) { error in
XCTAssertEqualTypeAndValue(error, HTTPClientError.deadlineExceeded)
}
}
}

func testResponseStreamingWorks() {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return }
XCTAsyncTest {
Expand Down