| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103 |
- /*
- * Copyright 2023, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import XCTest
- @testable import GRPCCore
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- final class BufferedStreamTests: XCTestCase {
- // MARK: - sequenceDeinitialized
- func testSequenceDeinitialized_whenNoIterator() async throws {
- var (stream, source): (BufferedStream?, BufferedStream.Source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source.onTermination = {
- onTerminationContinuation.finish()
- }
- await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- withExtendedLifetime(stream) {}
- stream = nil
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- do {
- _ = try { try source.write(2) }()
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is AlreadyFinishedError)
- }
- group.cancelAll()
- }
- }
- func testSequenceDeinitialized_whenIterator() async throws {
- var (stream, source): (BufferedStream?, BufferedStream.Source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- var iterator = stream?.makeAsyncIterator()
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source.onTermination = {
- onTerminationContinuation.finish()
- }
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- try withExtendedLifetime(stream) {
- let writeResult = try source.write(1)
- writeResult.assertIsProducerMore()
- }
- stream = nil
- do {
- let writeResult = try { try source.write(2) }()
- writeResult.assertIsProducerMore()
- } catch {
- XCTFail("Expected no error to be thrown")
- }
- let element1 = try await iterator?.next()
- XCTAssertEqual(element1, 1)
- let element2 = try await iterator?.next()
- XCTAssertEqual(element2, 2)
- group.cancelAll()
- }
- }
- func testSequenceDeinitialized_whenFinished() async throws {
- var (stream, source): (BufferedStream?, BufferedStream.Source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source.onTermination = {
- onTerminationContinuation.finish()
- }
- await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- withExtendedLifetime(stream) {
- source.finish(throwing: nil)
- }
- stream = nil
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- do {
- _ = try { try source.write(1) }()
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is AlreadyFinishedError)
- }
- group.cancelAll()
- }
- }
- func testSequenceDeinitialized_whenStreaming_andSuspendedProducer() async throws {
- var (stream, source): (BufferedStream?, BufferedStream.Source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 2)
- )
- _ = try { try source.write(1) }()
- do {
- try await withCheckedThrowingContinuation { continuation in
- source.write(1) { result in
- continuation.resume(with: result)
- }
- stream = nil
- _ = stream?.makeAsyncIterator()
- }
- } catch {
- XCTAssertTrue(error is AlreadyFinishedError)
- }
- }
- // MARK: - iteratorInitialized
- func testIteratorInitialized_whenInitial() async throws {
- let (stream, _) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- _ = stream.makeAsyncIterator()
- }
- func testIteratorInitialized_whenStreaming() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- try await source.write(1)
- var iterator = stream.makeAsyncIterator()
- let element = try await iterator.next()
- XCTAssertEqual(element, 1)
- }
- func testIteratorInitialized_whenSourceFinished() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- try await source.write(1)
- source.finish(throwing: nil)
- var iterator = stream.makeAsyncIterator()
- let element1 = try await iterator.next()
- XCTAssertEqual(element1, 1)
- let element2 = try await iterator.next()
- XCTAssertNil(element2)
- }
- func testIteratorInitialized_whenFinished() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- source.finish(throwing: nil)
- var iterator = stream.makeAsyncIterator()
- let element = try await iterator.next()
- XCTAssertNil(element)
- }
- // MARK: - iteratorDeinitialized
- func testIteratorDeinitialized_whenInitial() async throws {
- var (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source.onTermination = {
- onTerminationContinuation.finish()
- }
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- var iterator: BufferedStream<Int>.AsyncIterator? = stream.makeAsyncIterator()
- iterator = nil
- _ = try await iterator?.next()
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- }
- func testIteratorDeinitialized_whenStreaming() async throws {
- var (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source.onTermination = {
- onTerminationContinuation.finish()
- }
- try await source.write(1)
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- var iterator: BufferedStream<Int>.AsyncIterator? = stream.makeAsyncIterator()
- iterator = nil
- _ = try await iterator?.next()
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- }
- func testIteratorDeinitialized_whenSourceFinished() async throws {
- var (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source.onTermination = {
- onTerminationContinuation.finish()
- }
- try await source.write(1)
- source.finish(throwing: nil)
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- var iterator: BufferedStream<Int>.AsyncIterator? = stream.makeAsyncIterator()
- iterator = nil
- _ = try await iterator?.next()
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- }
- func testIteratorDeinitialized_whenFinished() async throws {
- var (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source.onTermination = {
- onTerminationContinuation.finish()
- }
- source.finish(throwing: nil)
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- var iterator: BufferedStream<Int>.AsyncIterator? = stream.makeAsyncIterator()
- iterator = nil
- _ = try await iterator?.next()
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- }
- func testIteratorDeinitialized_whenStreaming_andSuspendedProducer() async throws {
- var (stream, source): (BufferedStream?, BufferedStream.Source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 2)
- )
- var iterator: BufferedStream<Int>.AsyncIterator? = stream?.makeAsyncIterator()
- stream = nil
- _ = try { try source.write(1) }()
- do {
- try await withCheckedThrowingContinuation { continuation in
- source.write(1) { result in
- continuation.resume(with: result)
- }
- iterator = nil
- }
- } catch {
- XCTAssertTrue(error is AlreadyFinishedError)
- }
- _ = try await iterator?.next()
- }
- // MARK: - sourceDeinitialized
- func testSourceDeinitialized_whenInitial() async throws {
- var (stream, source): (BufferedStream, BufferedStream.Source?) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source?.onTermination = {
- onTerminationContinuation.finish()
- }
- await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- source = nil
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- withExtendedLifetime(stream) {}
- }
- func testSourceDeinitialized_whenStreaming_andEmptyBuffer() async throws {
- var (stream, source): (BufferedStream, BufferedStream.Source?) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source?.onTermination = {
- onTerminationContinuation.finish()
- }
- try await source?.write(1)
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- var iterator: BufferedStream<Int>.AsyncIterator? = stream.makeAsyncIterator()
- _ = try await iterator?.next()
- source = nil
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- }
- func testSourceDeinitialized_whenStreaming_andNotEmptyBuffer() async throws {
- var (stream, source): (BufferedStream, BufferedStream.Source?) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source?.onTermination = {
- onTerminationContinuation.finish()
- }
- try await source?.write(1)
- try await source?.write(2)
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- var iterator: BufferedStream<Int>.AsyncIterator? = stream.makeAsyncIterator()
- _ = try await iterator?.next()
- source = nil
- _ = await onTerminationIterator.next()
- _ = try await iterator?.next()
- _ = try await iterator?.next()
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- }
- func testSourceDeinitialized_whenSourceFinished() async throws {
- var (stream, source): (BufferedStream, BufferedStream.Source?) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source?.onTermination = {
- onTerminationContinuation.finish()
- }
- try await source?.write(1)
- try await source?.write(2)
- source?.finish(throwing: nil)
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- var iterator: BufferedStream<Int>.AsyncIterator? = stream.makeAsyncIterator()
- _ = try await iterator?.next()
- source = nil
- _ = await onTerminationIterator.next()
- _ = try await iterator?.next()
- _ = try await iterator?.next()
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- }
- func testSourceDeinitialized_whenFinished() async throws {
- var (stream, source): (BufferedStream, BufferedStream.Source?) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 5, high: 10)
- )
- let (onTerminationStream, onTerminationContinuation) = AsyncStream<Void>.makeStream()
- source?.onTermination = {
- onTerminationContinuation.finish()
- }
- source?.finish(throwing: nil)
- await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while !Task.isCancelled {
- onTerminationContinuation.yield()
- try await Task.sleep(nanoseconds: 200_000_000)
- }
- }
- var onTerminationIterator = onTerminationStream.makeAsyncIterator()
- _ = await onTerminationIterator.next()
- _ = stream.makeAsyncIterator()
- source = nil
- _ = await onTerminationIterator.next()
- let terminationResult: Void? = await onTerminationIterator.next()
- XCTAssertNil(terminationResult)
- group.cancelAll()
- }
- }
- func testSourceDeinitialized_whenStreaming_andSuspendedProducer() async throws {
- var (stream, source): (BufferedStream, BufferedStream.Source?) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 0, high: 0)
- )
- let (producerStream, producerContinuation) = AsyncThrowingStream<Void, Error>.makeStream()
- var iterator = stream.makeAsyncIterator()
- source?.write(1) {
- producerContinuation.yield(with: $0)
- }
- _ = try await iterator.next()
- source = nil
- do {
- try await producerStream.first { _ in true }
- XCTFail("We expected to throw here")
- } catch {
- XCTAssertTrue(error is AlreadyFinishedError)
- }
- }
- // MARK: - write
- func testWrite_whenInitial() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 2, high: 5)
- )
- try await source.write(1)
- var iterator = stream.makeAsyncIterator()
- let element = try await iterator.next()
- XCTAssertEqual(element, 1)
- }
- func testWrite_whenStreaming_andNoConsumer() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 2, high: 5)
- )
- try await source.write(1)
- try await source.write(2)
- var iterator = stream.makeAsyncIterator()
- let element1 = try await iterator.next()
- XCTAssertEqual(element1, 1)
- let element2 = try await iterator.next()
- XCTAssertEqual(element2, 2)
- }
- func testWrite_whenStreaming_andSuspendedConsumer() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 2, high: 5)
- )
- try await withThrowingTaskGroup(of: Int?.self) { group in
- group.addTask {
- return try await stream.first { _ in true }
- }
- // This is always going to be a bit racy since we need the call to next() suspend
- try await Task.sleep(nanoseconds: 500_000_000)
- try await source.write(1)
- let element = try await group.next()
- XCTAssertEqual(element, 1)
- }
- }
- func testWrite_whenStreaming_andSuspendedConsumer_andEmptySequence() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 2, high: 5)
- )
- try await withThrowingTaskGroup(of: Int?.self) { group in
- group.addTask {
- return try await stream.first { _ in true }
- }
- // This is always going to be a bit racy since we need the call to next() suspend
- try await Task.sleep(nanoseconds: 500_000_000)
- try await source.write(contentsOf: [])
- try await source.write(contentsOf: [1])
- let element = try await group.next()
- XCTAssertEqual(element, 1)
- }
- }
- // MARK: - enqueueProducer
- func testEnqueueProducer_whenStreaming_andAndCancelled() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 2)
- )
- let (producerStream, producerSource) = AsyncThrowingStream<Void, Error>.makeStream()
- try await source.write(1)
- let writeResult = try { try source.write(2) }()
- switch writeResult {
- case .produceMore:
- preconditionFailure()
- case .enqueueCallback(let callbackToken):
- source.cancelCallback(callbackToken: callbackToken)
- source.enqueueCallback(callbackToken: callbackToken) { result in
- producerSource.yield(with: result)
- }
- }
- do {
- _ = try await producerStream.first { _ in true }
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is CancellationError)
- }
- let element = try await stream.first { _ in true }
- XCTAssertEqual(element, 1)
- }
- func testEnqueueProducer_whenStreaming_andAndCancelled_andAsync() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 2)
- )
- try await source.write(1)
- await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- try await source.write(2)
- }
- group.cancelAll()
- do {
- try await group.next()
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is CancellationError)
- }
- }
- let element = try await stream.first { _ in true }
- XCTAssertEqual(element, 1)
- }
- func testEnqueueProducer_whenStreaming_andInterleaving() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 1)
- )
- var iterator = stream.makeAsyncIterator()
- let (producerStream, producerSource) = AsyncThrowingStream<Void, Error>.makeStream()
- let writeResult = try { try source.write(1) }()
- switch writeResult {
- case .produceMore:
- preconditionFailure()
- case .enqueueCallback(let callbackToken):
- let element = try await iterator.next()
- XCTAssertEqual(element, 1)
- source.enqueueCallback(callbackToken: callbackToken) { result in
- producerSource.yield(with: result)
- }
- }
- do {
- _ = try await producerStream.first { _ in true }
- } catch {
- XCTFail("Expected no error to be thrown")
- }
- }
- func testEnqueueProducer_whenStreaming_andSuspending() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 1)
- )
- var iterator = stream.makeAsyncIterator()
- let (producerStream, producerSource) = AsyncThrowingStream<Void, Error>.makeStream()
- let writeResult = try { try source.write(1) }()
- switch writeResult {
- case .produceMore:
- preconditionFailure()
- case .enqueueCallback(let callbackToken):
- source.enqueueCallback(callbackToken: callbackToken) { result in
- producerSource.yield(with: result)
- }
- }
- let element = try await iterator.next()
- XCTAssertEqual(element, 1)
- do {
- _ = try await producerStream.first { _ in true }
- } catch {
- XCTFail("Expected no error to be thrown")
- }
- }
- func testEnqueueProducer_whenFinished() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 1)
- )
- var iterator = stream.makeAsyncIterator()
- let (producerStream, producerSource) = AsyncThrowingStream<Void, Error>.makeStream()
- let writeResult = try { try source.write(1) }()
- switch writeResult {
- case .produceMore:
- preconditionFailure()
- case .enqueueCallback(let callbackToken):
- source.finish(throwing: nil)
- source.enqueueCallback(callbackToken: callbackToken) { result in
- producerSource.yield(with: result)
- }
- }
- let element = try await iterator.next()
- XCTAssertEqual(element, 1)
- do {
- _ = try await producerStream.first { _ in true }
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is AlreadyFinishedError)
- }
- }
- // MARK: - cancelProducer
- func testCancelProducer_whenStreaming() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 2)
- )
- let (producerStream, producerSource) = AsyncThrowingStream<Void, Error>.makeStream()
- try await source.write(1)
- let writeResult = try { try source.write(2) }()
- switch writeResult {
- case .produceMore:
- preconditionFailure()
- case .enqueueCallback(let callbackToken):
- source.enqueueCallback(callbackToken: callbackToken) { result in
- producerSource.yield(with: result)
- }
- source.cancelCallback(callbackToken: callbackToken)
- }
- do {
- _ = try await producerStream.first { _ in true }
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is CancellationError)
- }
- let element = try await stream.first { _ in true }
- XCTAssertEqual(element, 1)
- }
- func testCancelProducer_whenSourceFinished() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 2)
- )
- let (producerStream, producerSource) = AsyncThrowingStream<Void, Error>.makeStream()
- try await source.write(1)
- let writeResult = try { try source.write(2) }()
- switch writeResult {
- case .produceMore:
- preconditionFailure()
- case .enqueueCallback(let callbackToken):
- source.enqueueCallback(callbackToken: callbackToken) { result in
- producerSource.yield(with: result)
- }
- source.finish(throwing: nil)
- source.cancelCallback(callbackToken: callbackToken)
- }
- do {
- _ = try await producerStream.first { _ in true }
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is AlreadyFinishedError)
- }
- let element = try await stream.first { _ in true }
- XCTAssertEqual(element, 1)
- }
- // MARK: - finish
- func testFinish_whenStreaming_andConsumerSuspended() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 1)
- )
- try await withThrowingTaskGroup(of: Int?.self) { group in
- group.addTask {
- return try await stream.first { $0 == 2 }
- }
- // This is always going to be a bit racy since we need the call to next() suspend
- try await Task.sleep(nanoseconds: 500_000_000)
- source.finish(throwing: nil)
- let element = try await group.next()
- XCTAssertEqual(element, .some(nil))
- }
- }
- func testFinish_whenInitial() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 1, high: 1)
- )
- source.finish(throwing: CancellationError())
- do {
- for try await _ in stream {}
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is CancellationError)
- }
- }
- // MARK: - Backpressure
- func testBackPressure() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 2, high: 4)
- )
- let (backPressureEventStream, backPressureEventContinuation) = AsyncStream.makeStream(
- of: Void.self
- )
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- while true {
- backPressureEventContinuation.yield(())
- try await source.write(contentsOf: [1])
- }
- }
- var backPressureEventIterator = backPressureEventStream.makeAsyncIterator()
- var iterator = stream.makeAsyncIterator()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- _ = try await iterator.next()
- _ = try await iterator.next()
- _ = try await iterator.next()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- group.cancelAll()
- }
- }
- func testBackPressureSync() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 2, high: 4)
- )
- let (backPressureEventStream, backPressureEventContinuation) = AsyncStream.makeStream(
- of: Void.self
- )
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- @Sendable func yield() {
- backPressureEventContinuation.yield(())
- source.write(contentsOf: [1]) { result in
- switch result {
- case .success:
- yield()
- case .failure:
- break
- }
- }
- }
- yield()
- }
- var backPressureEventIterator = backPressureEventStream.makeAsyncIterator()
- var iterator = stream.makeAsyncIterator()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- _ = try await iterator.next()
- _ = try await iterator.next()
- _ = try await iterator.next()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- await backPressureEventIterator.next()
- group.cancelAll()
- }
- }
- func testThrowsError() async throws {
- let (stream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 2, high: 4)
- )
- try await source.write(1)
- try await source.write(2)
- source.finish(throwing: CancellationError())
- var elements = [Int]()
- var iterator = stream.makeAsyncIterator()
- do {
- while let element = try await iterator.next() {
- elements.append(element)
- }
- XCTFail("Expected an error to be thrown")
- } catch {
- XCTAssertTrue(error is CancellationError)
- XCTAssertEqual(elements, [1, 2])
- }
- let element = try await iterator.next()
- XCTAssertNil(element)
- }
- func testAsyncSequenceWrite() async throws {
- let (stream, continuation) = AsyncStream<Int>.makeStream()
- let (backpressuredStream, source) = BufferedStream.makeStream(
- of: Int.self,
- backPressureStrategy: .watermark(low: 2, high: 4)
- )
- continuation.yield(1)
- continuation.yield(2)
- continuation.finish()
- try await source.write(contentsOf: stream)
- source.finish(throwing: nil)
- let elements = try await backpressuredStream.collect()
- XCTAssertEqual(elements, [1, 2])
- }
- }
- extension BufferedStream.Source.WriteResult {
- func assertIsProducerMore() {
- switch self {
- case .produceMore:
- return
- case .enqueueCallback:
- XCTFail("Expected produceMore")
- }
- }
- func assertIsEnqueueCallback() {
- switch self {
- case .produceMore:
- XCTFail("Expected enqueueCallback")
- case .enqueueCallback:
- return
- }
- }
- }
|