|
15 | 15 | @testable import AsyncHTTPClient
|
16 | 16 | import NIOCore
|
17 | 17 | import NIOEmbedded
|
| 18 | +import NIOHTTP1 |
18 | 19 | import XCTest
|
19 | 20 |
|
20 | 21 | final class Transaction_StateMachineTests: XCTestCase {
|
@@ -70,6 +71,87 @@ final class Transaction_StateMachineTests: XCTestCase {
|
70 | 71 | }
|
71 | 72 | #endif
|
72 | 73 | }
|
| 74 | + |
| 75 | + func testQueuedRequestGetsRemovedWhenDeadlineExceeded() { |
| 76 | + #if compiler(>=5.5) && canImport(_Concurrency) |
| 77 | + guard #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) else { return } |
| 78 | + XCTAsyncTest { |
| 79 | + func workaround(_ continuation: CheckedContinuation<HTTPClientResponse, Error>) { |
| 80 | + var state = Transaction.StateMachine(continuation) |
| 81 | + let queuer = MockTaskQueuer() |
| 82 | + |
| 83 | + state.requestWasQueued(queuer) |
| 84 | + |
| 85 | + let failAction = state.deadlineExceeded() |
| 86 | + guard case .cancel(let continuation, let scheduler, nil, nil) = failAction else { |
| 87 | + return XCTFail("Unexpected fail action: \(failAction)") |
| 88 | + } |
| 89 | + XCTAssertIdentical(scheduler as? MockTaskQueuer, queuer) |
| 90 | + |
| 91 | + continuation.resume(throwing: HTTPClientError.deadlineExceeded) |
| 92 | + } |
| 93 | + |
| 94 | + await XCTAssertThrowsError(try await withCheckedThrowingContinuation(workaround)) |
| 95 | + } |
| 96 | + #endif |
| 97 | + } |
| 98 | + |
| 99 | + func testScheduledRequestGetsRemovedWhenDeadlineExceeded() { |
| 100 | + #if compiler(>=5.5) && canImport(_Concurrency) |
| 101 | + guard #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) else { return } |
| 102 | + let eventLoop = EmbeddedEventLoop() |
| 103 | + XCTAsyncTest { |
| 104 | + func workaround(_ continuation: CheckedContinuation<HTTPClientResponse, Error>) { |
| 105 | + var state = Transaction.StateMachine(continuation) |
| 106 | + let executor = MockRequestExecutor(eventLoop: eventLoop) |
| 107 | + let queuer = MockTaskQueuer() |
| 108 | + |
| 109 | + XCTAssertEqual(state.willExecuteRequest(executor), .none) |
| 110 | + state.requestWasQueued(queuer) |
| 111 | + |
| 112 | + let failAction = state.deadlineExceeded() |
| 113 | + guard case .cancel(let continuation, nil, let rexecutor, nil) = failAction else { |
| 114 | + return XCTFail("Unexpected fail action: \(failAction)") |
| 115 | + } |
| 116 | + XCTAssertIdentical(rexecutor as? MockRequestExecutor, executor) |
| 117 | + |
| 118 | + continuation.resume(throwing: HTTPClientError.deadlineExceeded) |
| 119 | + } |
| 120 | + |
| 121 | + await XCTAssertThrowsError(try await withCheckedThrowingContinuation(workaround)) |
| 122 | + } |
| 123 | + #endif |
| 124 | + } |
| 125 | + |
| 126 | + func testRequestWithHeadReceivedGetNotCancelledWhenDeadlineExceeded() { |
| 127 | + #if compiler(>=5.5) && canImport(_Concurrency) |
| 128 | + guard #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) else { return } |
| 129 | + let eventLoop = EmbeddedEventLoop() |
| 130 | + XCTAsyncTest { |
| 131 | + func workaround(_ continuation: CheckedContinuation<HTTPClientResponse, Error>) { |
| 132 | + var state = Transaction.StateMachine(continuation) |
| 133 | + let executor = MockRequestExecutor(eventLoop: eventLoop) |
| 134 | + let queuer = MockTaskQueuer() |
| 135 | + |
| 136 | + XCTAssertEqual(state.willExecuteRequest(executor), .none) |
| 137 | + state.requestWasQueued(queuer) |
| 138 | + let head = HTTPResponseHead(version: .http1_1, status: .ok) |
| 139 | + let receiveResponseHeadAction = state.receiveResponseHead(head) |
| 140 | + guard case .succeedResponseHead(head, let continuation) = receiveResponseHeadAction else { |
| 141 | + return XCTFail("Unexpected action: \(receiveResponseHeadAction)") |
| 142 | + } |
| 143 | + |
| 144 | + let failAction = state.deadlineExceeded() |
| 145 | + guard case .none = failAction else { |
| 146 | + return XCTFail("Unexpected fail action: \(failAction)") |
| 147 | + } |
| 148 | + continuation.resume(throwing: HTTPClientError.deadlineExceeded) |
| 149 | + } |
| 150 | + |
| 151 | + await XCTAssertThrowsError(try await withCheckedThrowingContinuation(workaround)) |
| 152 | + } |
| 153 | + #endif |
| 154 | + } |
73 | 155 | }
|
74 | 156 |
|
75 | 157 | #if compiler(>=5.5) && canImport(_Concurrency)
|
|
0 commit comments