Skip to content

Instantly share code, notes, and snippets.

@NikolaiRuhe
Created June 13, 2022 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolaiRuhe/20394860c081e6cbed774ca79d388af1 to your computer and use it in GitHub Desktop.
Save NikolaiRuhe/20394860c081e6cbed774ca79d388af1 to your computer and use it in GitHub Desktop.
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