Skip to content
Open
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 KMPNativeCoroutinesAsync/AsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public struct NativeFlowAsyncSequence<Output, Failure: Error, Unit>: AsyncSequen
defer { self.semaphore.signal() }
self.cancellationError = cancellationError
if let continuation = self.continuation {
continuation.resume(returning: nil)
continuation.resume(throwing: CancellationError())
self.continuation = nil
}
self.nativeCancellable = nil
Expand Down
29 changes: 29 additions & 0 deletions KMPNativeCoroutinesAsyncTests/AsyncSequenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ class AsyncSequenceTests: XCTestCase {
}
XCTAssertTrue(error is CancellationError, "Error should be a CancellationError")
}

@MainActor
func testCancellationWhileNextIsSuspendedThrowsCancellationError() async {
let nextRequested = expectation(description: "The async iterator requested its first value")
var cancelCount = 0
let nativeFlow: NativeFlow<TestValue, Error, Void> = { _, _, cancelCallback in
nextRequested.fulfill()
return {
cancelCount += 1
cancelCallback(NSError(domain: "Ignored", code: 0), ())
}
}
let handle = Task {
for try await _ in asyncSequence(for: nativeFlow) { }
}

// The task inherits MainActor. After the expectation is fulfilled it continues
// synchronously into next() and suspends before this test resumes on MainActor.
await fulfillment(of: [nextRequested], timeout: 1)
handle.cancel()

let result = await handle.result
XCTAssertEqual(cancelCount, 1, "Cancellable should be invoked once")
guard case let .failure(error) = result else {
XCTFail("Task should fail with an error")
return
}
XCTAssertTrue(error is CancellationError, "Error should be a CancellationError")
}

func testCompletionWithCorrectValues() async {
let values = [TestValue(), TestValue(), TestValue(), TestValue(), TestValue()]
Expand Down