Skip to content

Tolerate both CancellationError and URLError in CancellationTests #45

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
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
2 changes: 1 addition & 1 deletion Tests/OpenAPIURLSessionTests/NIOAsyncHTTP1TestServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class AsyncTestHTTP1Server {
for try await connectionChannel in inbound {
group.addTask {
do {
debug("Sevrer handling new connection")
debug("Server handling new connection")
try await connectionHandler(connectionChannel)
debug("Server done handling connection")
} catch { debug("Server error handling connection: \(error)") }
Expand Down
8 changes: 4 additions & 4 deletions Tests/OpenAPIURLSessionTests/TaskCancellationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ func testTaskCancelled(_ cancellationPoint: CancellationPoint, transport: URLSes
await XCTAssertThrowsError(try await task.value) { error in XCTAssertTrue(error is CancellationError) }
case .beforeSendingRequestBody, .partwayThroughSendingRequestBody:
await XCTAssertThrowsError(try await task.value) { error in
guard let urlError = error as? URLError else {
XCTFail()
return
switch error {
case is CancellationError: break
case is URLError: XCTAssertEqual((error as! URLError).code, .cancelled)
default: XCTFail("Unexpected error: \(error)")
}
XCTAssertEqual(urlError.code, .cancelled)
}
case .beforeConsumingResponseBody, .partwayThroughConsumingResponseBody, .afterConsumingResponseBody:
try await task.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,47 @@ struct MockAsyncSequence<Element>: AsyncSequence, Sendable where Element: Sendab
var elementsToVend: [Element]
private let _elementsVended: LockedValueBox<[Element]>
var elementsVended: [Element] { _elementsVended.withValue { $0 } }
private let semaphore: DispatchSemaphore?
private let gateOpeningsStream: AsyncStream<Void>
private let gateOpeningsContinuation: AsyncStream<Void>.Continuation

init(elementsToVend: [Element], gatingProduction: Bool) {
self.elementsToVend = elementsToVend
self._elementsVended = LockedValueBox([])
self.semaphore = gatingProduction ? DispatchSemaphore(value: 0) : nil
(self.gateOpeningsStream, self.gateOpeningsContinuation) = AsyncStream.makeStream(of: Void.self)
if !gatingProduction { openGate() }
}

func openGate(for count: Int) { for _ in 0..<count { semaphore?.signal() } }
func openGate(for count: Int) { for _ in 0..<count { self.gateOpeningsContinuation.yield() } }

func openGate() {
openGate(for: elementsToVend.count + 1) // + 1 for the nil
}

func makeAsyncIterator() -> AsyncIterator {
AsyncIterator(elementsToVend: elementsToVend[...], semaphore: semaphore, elementsVended: _elementsVended)
AsyncIterator(
elementsToVend: elementsToVend[...],
gateOpenings: gateOpeningsStream.makeAsyncIterator(),
elementsVended: _elementsVended
)
}

final class AsyncIterator: AsyncIteratorProtocol {
var elementsToVend: ArraySlice<Element>
var semaphore: DispatchSemaphore?
var gateOpenings: AsyncStream<Void>.Iterator
var elementsVended: LockedValueBox<[Element]>

init(
elementsToVend: ArraySlice<Element>,
semaphore: DispatchSemaphore?,
gateOpenings: AsyncStream<Void>.Iterator,
elementsVended: LockedValueBox<[Element]>
) {
self.elementsToVend = elementsToVend
self.semaphore = semaphore
self.gateOpenings = gateOpenings
self.elementsVended = elementsVended
}

func next() async throws -> Element? {
await withCheckedContinuation { continuation in
semaphore?.wait()
continuation.resume()
}
guard await gateOpenings.next() != nil else { throw CancellationError() }
guard let element = elementsToVend.popFirst() else { return nil }
elementsVended.withValue { $0.append(element) }
return element
Expand Down