|  | 
|  | 1 | +//===----------------------------------------------------------------------===// | 
|  | 2 | +// | 
|  | 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project | 
|  | 4 | +// | 
|  | 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | 
|  | 6 | +// Licensed under Apache License v2.0 | 
|  | 7 | +// | 
|  | 8 | +// See LICENSE.txt for license information | 
|  | 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | 
|  | 10 | +// | 
|  | 11 | +// SPDX-License-Identifier: Apache-2.0 | 
|  | 12 | +// | 
|  | 13 | +//===----------------------------------------------------------------------===// | 
|  | 14 | +#if canImport(Darwin) | 
|  | 15 | + | 
|  | 16 | +import Foundation | 
|  | 17 | +import HTTPTypes | 
|  | 18 | +import NIO | 
|  | 19 | +import OpenAPIRuntime | 
|  | 20 | +import XCTest | 
|  | 21 | +@testable import OpenAPIURLSession | 
|  | 22 | + | 
|  | 23 | +enum CancellationPoint: CaseIterable { | 
|  | 24 | +    case beforeSendingHead | 
|  | 25 | +    case beforeSendingRequestBody | 
|  | 26 | +    case partwayThroughSendingRequestBody | 
|  | 27 | +    case beforeConsumingResponseBody | 
|  | 28 | +    case partwayThroughConsumingResponseBody | 
|  | 29 | +    case afterConsumingResponseBody | 
|  | 30 | +} | 
|  | 31 | + | 
|  | 32 | +func testTaskCancelled(_ cancellationPoint: CancellationPoint, transport: URLSessionTransport) async throws { | 
|  | 33 | +    let requestPath = "/hello/world" | 
|  | 34 | +    let requestBodyElements = ["Hello,", "world!"] | 
|  | 35 | +    let requestBodySequence = MockAsyncSequence(elementsToVend: requestBodyElements, gatingProduction: true) | 
|  | 36 | +    let requestBody = HTTPBody( | 
|  | 37 | +        requestBodySequence, | 
|  | 38 | +        length: .known(Int64(requestBodyElements.joined().lengthOfBytes(using: .utf8))), | 
|  | 39 | +        iterationBehavior: .single | 
|  | 40 | +    ) | 
|  | 41 | + | 
|  | 42 | +    let responseBodyMessage = "Hey!" | 
|  | 43 | + | 
|  | 44 | +    let taskShouldCancel = XCTestExpectation(description: "Concurrency task cancelled") | 
|  | 45 | +    let taskCancelled = XCTestExpectation(description: "Concurrency task cancelled") | 
|  | 46 | + | 
|  | 47 | +    try await withThrowingTaskGroup(of: Void.self) { group in | 
|  | 48 | +        let serverPort = try await AsyncTestHTTP1Server.start(connectionTaskGroup: &group) { connectionChannel in | 
|  | 49 | +            try await connectionChannel.executeThenClose { inbound, outbound in | 
|  | 50 | +                var requestPartIterator = inbound.makeAsyncIterator() | 
|  | 51 | +                var accumulatedBody = ByteBuffer() | 
|  | 52 | +                while let requestPart = try await requestPartIterator.next() { | 
|  | 53 | +                    switch requestPart { | 
|  | 54 | +                    case .head(let head): | 
|  | 55 | +                        XCTAssertEqual(head.uri, requestPath) | 
|  | 56 | +                        XCTAssertEqual(head.method, .POST) | 
|  | 57 | +                    case .body(let buffer): accumulatedBody.writeImmutableBuffer(buffer) | 
|  | 58 | +                    case .end: | 
|  | 59 | +                        switch cancellationPoint { | 
|  | 60 | +                        case .beforeConsumingResponseBody, .partwayThroughConsumingResponseBody, | 
|  | 61 | +                            .afterConsumingResponseBody: | 
|  | 62 | +                            XCTAssertEqual( | 
|  | 63 | +                                String(decoding: accumulatedBody.readableBytesView, as: UTF8.self), | 
|  | 64 | +                                requestBodyElements.joined() | 
|  | 65 | +                            ) | 
|  | 66 | +                        case .beforeSendingHead, .beforeSendingRequestBody, .partwayThroughSendingRequestBody: break | 
|  | 67 | +                        } | 
|  | 68 | +                        try await outbound.write(.head(.init(version: .http1_1, status: .ok))) | 
|  | 69 | +                        try await outbound.write(.body(ByteBuffer(string: responseBodyMessage))) | 
|  | 70 | +                        try await outbound.write(.end(nil)) | 
|  | 71 | +                    } | 
|  | 72 | +                } | 
|  | 73 | +            } | 
|  | 74 | +        } | 
|  | 75 | +        debug("Server running on 127.0.0.1:\(serverPort)") | 
|  | 76 | + | 
|  | 77 | +        let task = Task { | 
|  | 78 | +            if case .beforeSendingHead = cancellationPoint { | 
|  | 79 | +                taskShouldCancel.fulfill() | 
|  | 80 | +                await fulfillment(of: [taskCancelled]) | 
|  | 81 | +            } | 
|  | 82 | +            debug("Client starting request") | 
|  | 83 | +            async let (asyncResponse, asyncResponseBody) = try await transport.send( | 
|  | 84 | +                HTTPRequest(method: .post, scheme: nil, authority: nil, path: requestPath), | 
|  | 85 | +                body: requestBody, | 
|  | 86 | +                baseURL: URL(string: "http://127.0.0.1:\(serverPort)")!, | 
|  | 87 | +                operationID: "unused" | 
|  | 88 | +            ) | 
|  | 89 | + | 
|  | 90 | +            if case .beforeSendingRequestBody = cancellationPoint { | 
|  | 91 | +                taskShouldCancel.fulfill() | 
|  | 92 | +                await fulfillment(of: [taskCancelled]) | 
|  | 93 | +            } | 
|  | 94 | + | 
|  | 95 | +            requestBodySequence.openGate(for: 1) | 
|  | 96 | + | 
|  | 97 | +            if case .partwayThroughSendingRequestBody = cancellationPoint { | 
|  | 98 | +                taskShouldCancel.fulfill() | 
|  | 99 | +                await fulfillment(of: [taskCancelled]) | 
|  | 100 | +            } | 
|  | 101 | + | 
|  | 102 | +            requestBodySequence.openGate() | 
|  | 103 | + | 
|  | 104 | +            let (response, maybeResponseBody) = try await (asyncResponse, asyncResponseBody) | 
|  | 105 | + | 
|  | 106 | +            debug("Client received response head: \(response)") | 
|  | 107 | +            XCTAssertEqual(response.status, .ok) | 
|  | 108 | +            let responseBody = try XCTUnwrap(maybeResponseBody) | 
|  | 109 | + | 
|  | 110 | +            if case .beforeConsumingResponseBody = cancellationPoint { | 
|  | 111 | +                taskShouldCancel.fulfill() | 
|  | 112 | +                await fulfillment(of: [taskCancelled]) | 
|  | 113 | +            } | 
|  | 114 | + | 
|  | 115 | +            var iterator = responseBody.makeAsyncIterator() | 
|  | 116 | + | 
|  | 117 | +            _ = try await iterator.next() | 
|  | 118 | + | 
|  | 119 | +            if case .partwayThroughConsumingResponseBody = cancellationPoint { | 
|  | 120 | +                taskShouldCancel.fulfill() | 
|  | 121 | +                await fulfillment(of: [taskCancelled]) | 
|  | 122 | +            } | 
|  | 123 | + | 
|  | 124 | +            while try await iterator.next() != nil { | 
|  | 125 | + | 
|  | 126 | +            } | 
|  | 127 | + | 
|  | 128 | +            if case .afterConsumingResponseBody = cancellationPoint { | 
|  | 129 | +                taskShouldCancel.fulfill() | 
|  | 130 | +                await fulfillment(of: [taskCancelled]) | 
|  | 131 | +            } | 
|  | 132 | + | 
|  | 133 | +        } | 
|  | 134 | + | 
|  | 135 | +        await fulfillment(of: [taskShouldCancel]) | 
|  | 136 | +        task.cancel() | 
|  | 137 | +        taskCancelled.fulfill() | 
|  | 138 | + | 
|  | 139 | +        switch transport.configuration.implementation { | 
|  | 140 | +        case .buffering: | 
|  | 141 | +            switch cancellationPoint { | 
|  | 142 | +            case .beforeSendingHead, .beforeSendingRequestBody, .partwayThroughSendingRequestBody: | 
|  | 143 | +                await XCTAssertThrowsError(try await task.value) { error in XCTAssertTrue(error is CancellationError) } | 
|  | 144 | +            case .beforeConsumingResponseBody, .partwayThroughConsumingResponseBody, .afterConsumingResponseBody: | 
|  | 145 | +                try await task.value | 
|  | 146 | +            } | 
|  | 147 | +        case .streaming: | 
|  | 148 | +            switch cancellationPoint { | 
|  | 149 | +            case .beforeSendingHead: | 
|  | 150 | +                await XCTAssertThrowsError(try await task.value) { error in XCTAssertTrue(error is CancellationError) } | 
|  | 151 | +            case .beforeSendingRequestBody, .partwayThroughSendingRequestBody: | 
|  | 152 | +                await XCTAssertThrowsError(try await task.value) { error in | 
|  | 153 | +                    guard let urlError = error as? URLError else { | 
|  | 154 | +                        XCTFail() | 
|  | 155 | +                        return | 
|  | 156 | +                    } | 
|  | 157 | +                    XCTAssertEqual(urlError.code, .cancelled) | 
|  | 158 | +                } | 
|  | 159 | +            case .beforeConsumingResponseBody, .partwayThroughConsumingResponseBody, .afterConsumingResponseBody: | 
|  | 160 | +                try await task.value | 
|  | 161 | +            } | 
|  | 162 | +        } | 
|  | 163 | + | 
|  | 164 | +        group.cancelAll() | 
|  | 165 | +    } | 
|  | 166 | + | 
|  | 167 | +} | 
|  | 168 | + | 
|  | 169 | +func fulfillment( | 
|  | 170 | +    of expectations: [XCTestExpectation], | 
|  | 171 | +    timeout seconds: TimeInterval = .infinity, | 
|  | 172 | +    enforceOrder enforceOrderOfFulfillment: Bool = false, | 
|  | 173 | +    file: StaticString = #file, | 
|  | 174 | +    line: UInt = #line | 
|  | 175 | +) async { | 
|  | 176 | +    guard | 
|  | 177 | +        case .completed = await XCTWaiter.fulfillment( | 
|  | 178 | +            of: expectations, | 
|  | 179 | +            timeout: seconds, | 
|  | 180 | +            enforceOrder: enforceOrderOfFulfillment | 
|  | 181 | +        ) | 
|  | 182 | +    else { | 
|  | 183 | +        XCTFail("Expectation was not fulfilled", file: file, line: line) | 
|  | 184 | +        return | 
|  | 185 | +    } | 
|  | 186 | +} | 
|  | 187 | + | 
|  | 188 | +extension URLSessionTransportBufferedTests { | 
|  | 189 | +    func testCancellation_beforeSendingHead() async throws { | 
|  | 190 | +        try await testTaskCancelled(.beforeSendingHead, transport: transport) | 
|  | 191 | +    } | 
|  | 192 | + | 
|  | 193 | +    func testCancellation_beforeSendingRequestBody() async throws { | 
|  | 194 | +        try await testTaskCancelled(.beforeSendingRequestBody, transport: transport) | 
|  | 195 | +    } | 
|  | 196 | + | 
|  | 197 | +    func testCancellation_partwayThroughSendingRequestBody() async throws { | 
|  | 198 | +        try await testTaskCancelled(.partwayThroughSendingRequestBody, transport: transport) | 
|  | 199 | +    } | 
|  | 200 | + | 
|  | 201 | +    func testCancellation_beforeConsumingResponseBody() async throws { | 
|  | 202 | +        try await testTaskCancelled(.beforeConsumingResponseBody, transport: transport) | 
|  | 203 | +    } | 
|  | 204 | + | 
|  | 205 | +    func testCancellation_partwayThroughConsumingResponseBody() async throws { | 
|  | 206 | +        try await testTaskCancelled(.partwayThroughConsumingResponseBody, transport: transport) | 
|  | 207 | +    } | 
|  | 208 | + | 
|  | 209 | +    func testCancellation_afterConsumingResponseBody() async throws { | 
|  | 210 | +        try await testTaskCancelled(.afterConsumingResponseBody, transport: transport) | 
|  | 211 | +    } | 
|  | 212 | +} | 
|  | 213 | + | 
|  | 214 | +extension URLSessionTransportStreamingTests { | 
|  | 215 | +    func testCancellation_beforeSendingHead() async throws { | 
|  | 216 | +        try await testTaskCancelled(.beforeSendingHead, transport: transport) | 
|  | 217 | +    } | 
|  | 218 | + | 
|  | 219 | +    func testCancellation_beforeSendingRequestBody() async throws { | 
|  | 220 | +        try await testTaskCancelled(.beforeSendingRequestBody, transport: transport) | 
|  | 221 | +    } | 
|  | 222 | + | 
|  | 223 | +    func testCancellation_partwayThroughSendingRequestBody() async throws { | 
|  | 224 | +        try await testTaskCancelled(.partwayThroughSendingRequestBody, transport: transport) | 
|  | 225 | +    } | 
|  | 226 | + | 
|  | 227 | +    func testCancellation_beforeConsumingResponseBody() async throws { | 
|  | 228 | +        try await testTaskCancelled(.beforeConsumingResponseBody, transport: transport) | 
|  | 229 | +    } | 
|  | 230 | + | 
|  | 231 | +    func testCancellation_partwayThroughConsumingResponseBody() async throws { | 
|  | 232 | +        try await testTaskCancelled(.partwayThroughConsumingResponseBody, transport: transport) | 
|  | 233 | +    } | 
|  | 234 | + | 
|  | 235 | +    func testCancellation_afterConsumingResponseBody() async throws { | 
|  | 236 | +        try await testTaskCancelled(.afterConsumingResponseBody, transport: transport) | 
|  | 237 | +    } | 
|  | 238 | +} | 
|  | 239 | + | 
|  | 240 | +#endif  // canImport(Darwin) | 
0 commit comments