From 75cea45e61d3fe17ff6bcba119f4d09d4151fe8a Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Wed, 5 Apr 2023 17:37:43 +0100 Subject: [PATCH] Throw `CancellationError` if `NIOThrowingAsyncSequenceProducer.AsyncIterator.next()` is cancelled instead of returning `nil` (#2399) * Throw `CancellationError` if `NIOThrowingAsyncSequenceProducer.AsyncIterator.next()` is cancelled instead of returning `nil` * Update doc comment * Fix typo --- .../AsyncSequences/NIOAsyncSequenceProducer.swift | 3 ++- .../NIOThrowingAsyncSequenceProducer.swift | 10 +++++----- .../AsyncSequences/NIOThrowingAsyncSequenceTests.swift | 6 ++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Sources/NIOCore/AsyncSequences/NIOAsyncSequenceProducer.swift b/Sources/NIOCore/AsyncSequences/NIOAsyncSequenceProducer.swift index 2022aa798..23cf131d2 100644 --- a/Sources/NIOCore/AsyncSequences/NIOAsyncSequenceProducer.swift +++ b/Sources/NIOCore/AsyncSequences/NIOAsyncSequenceProducer.swift @@ -198,7 +198,8 @@ extension NIOAsyncSequenceProducer { @inlinable public func next() async -> Element? { - return try! await self._throwingIterator.next() + // this call will only throw if cancelled and we want to just return nil in that case + return try? await self._throwingIterator.next() } } } diff --git a/Sources/NIOCore/AsyncSequences/NIOThrowingAsyncSequenceProducer.swift b/Sources/NIOCore/AsyncSequences/NIOThrowingAsyncSequenceProducer.swift index 7ad9ad10c..ee28abb73 100644 --- a/Sources/NIOCore/AsyncSequences/NIOThrowingAsyncSequenceProducer.swift +++ b/Sources/NIOCore/AsyncSequences/NIOThrowingAsyncSequenceProducer.swift @@ -498,8 +498,8 @@ extension NIOThrowingAsyncSequenceProducer { return delegate - case .resumeContinuationWithNilAndCallDidTerminate(let continuation): - continuation.resume(returning: nil) + case .resumeContinuationWithCancellationErrorAndCallDidTerminate(let continuation): + continuation.resume(throwing: CancellationError()) let delegate = self._delegate self._delegate = nil @@ -868,9 +868,9 @@ extension NIOThrowingAsyncSequenceProducer { enum CancelledAction { /// Indicates that ``NIOAsyncSequenceProducerDelegate/didTerminate()`` should be called. case callDidTerminate - /// Indicates that the continuation should be resumed with `nil` and + /// Indicates that the continuation should be resumed with a `CancellationError` and /// that ``NIOAsyncSequenceProducerDelegate/didTerminate()`` should be called. - case resumeContinuationWithNilAndCallDidTerminate(CheckedContinuation) + case resumeContinuationWithCancellationErrorAndCallDidTerminate(CheckedContinuation) /// Indicates that nothing should be done. case none } @@ -889,7 +889,7 @@ extension NIOThrowingAsyncSequenceProducer { // and we can transition to finished here and inform the delegate self._state = .finished(iteratorInitialized: iteratorInitialized) - return .resumeContinuationWithNilAndCallDidTerminate(continuation) + return .resumeContinuationWithCancellationErrorAndCallDidTerminate(continuation) case .streaming(_, _, continuation: .none, _, let iteratorInitialized): self._state = .finished(iteratorInitialized: iteratorInitialized) diff --git a/Tests/NIOCoreTests/AsyncSequences/NIOThrowingAsyncSequenceTests.swift b/Tests/NIOCoreTests/AsyncSequences/NIOThrowingAsyncSequenceTests.swift index 36a9e63e4..de4649060 100644 --- a/Tests/NIOCoreTests/AsyncSequences/NIOThrowingAsyncSequenceTests.swift +++ b/Tests/NIOCoreTests/AsyncSequences/NIOThrowingAsyncSequenceTests.swift @@ -457,9 +457,11 @@ final class NIOThrowingAsyncSequenceProducerTests: XCTestCase { try await Task.sleep(nanoseconds: 1_000_000) task.cancel() - let value = try await task.value + let result = await task.result XCTAssertEqualWithoutAutoclosure(await self.delegate.events.prefix(1).collect(), [.didTerminate]) - XCTAssertNil(value) + await XCTAssertThrowsError(try result.get()) { error in + XCTAssertTrue(error is CancellationError) + } } func testTaskCancel_whenStreaming_andNotSuspended() async throws {