Created
June 13, 2022 12:23
-
-
Save NikolaiRuhe/20394860c081e6cbed774ca79d388af1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testRequestCancellation() async throws { | |
// Used to synchronize unit test with request task. | |
let meeting = TaskMeeting() | |
// This request just waits for the unit test in its handler. | |
let sut = Request(responseHandler: { _ in | |
try? await meeting.rendezvous {} | |
}) | |
// Start unstructured Task that sends the request. | |
let requestTask = Task { try await sut.send() } | |
// Wait for the request task to proceed to the handler. | |
try await meeting.rendezvous { | |
// Now, during the handler call, cancel the request's task | |
requestTask.cancel() | |
} | |
// Await the request's result | |
let result = await requestTask.result | |
// Make sure the request was cancelled | |
XCTAssert(result.isCancelled, "not cancelled") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment