Im trying to run a test actor that makes a network request and if you call it while it still has a request going it will cancel the first request and throw an CancellationError. Im using a Task() to simulate it being called twice asynchrony and the code seems to be working fine. I can see the correct error being thrown and the catch-block being executed. Below is the code.
func testCancellation() async throws {
//setup code
let expectation = XCTestExpectation(description: "Cancellation Error thrown")
let autocompleteHandler = AutocompleteHandler(session: session)
Task() {
do {
let _ = try await autocompleteHandler.autocomplete(query: "Grizzly Bear")
} catch {
if error is CancellationError { expectation.fulfill() }
}
}
Task() {
do {
let _ = try await autocompleteHandler.autocomplete(query: "Grizzly Bear")
} catch {
if error is CancellationError { expectation.fulfill() }
}
}
wait(for: [expectation], timeout: 1)
}
When running this I get the following error:
Asynchronous wait failed: Exceeded timeout of 1 seconds, with unfulfilled expectations: "Cancellation Error thrown".
But as I said. I can see the catch-block being executed and the error is of the correct type and expectation.fulfill() is being called.