mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-30 04:58:44 +00:00
Add a package layout and initial implementation and tests for AsyncLazySequence (#2)
* Add a package layout and initial implementation and tests for AsyncLazySequence; a type for converting non asynchronous sequences into asynchronous ones * Add a first iteration pass expectation to ensure that tasks spawn for testing cancellation of AsyncLazySequence * Simplify the iterator for AsyncLazySequence
This commit is contained in:
1 parent
7f073235cb
commit
361c7926d1
6 files changed
+327
No files matched your search
@@ -0,0 +1,22 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
struct Indefinite<Element: Sendable>: Sequence, IteratorProtocol, Sendable {
|
||||
let value: Element
|
||||
|
||||
func next() -> Element? {
|
||||
return value
|
||||
}
|
||||
|
||||
func makeIterator() -> Indefinite<Element> {
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
final class ReportingSequence<Element>: Sequence, IteratorProtocol {
|
||||
enum Event: Equatable, CustomStringConvertible {
|
||||
case next
|
||||
case makeIterator
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .next: return "next()"
|
||||
case .makeIterator: return "makeIterator()"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var events = [Event]()
|
||||
var elements: [Element]
|
||||
|
||||
init(_ elements: [Element]) {
|
||||
self.elements = elements
|
||||
}
|
||||
|
||||
func next() -> Element? {
|
||||
events.append(.next)
|
||||
guard elements.count > 0 else {
|
||||
return nil
|
||||
}
|
||||
return elements.removeFirst()
|
||||
}
|
||||
|
||||
func makeIterator() -> ReportingSequence {
|
||||
events.append(.makeIterator)
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
final class ReportingAsyncSequence<Element>: AsyncSequence, AsyncIteratorProtocol {
|
||||
enum Event: Equatable, CustomStringConvertible {
|
||||
case next
|
||||
case makeAsyncIterator
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .next: return "next()"
|
||||
case .makeAsyncIterator: return "makeAsyncIterator()"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var events = [Event]()
|
||||
var elements: [Element]
|
||||
|
||||
init(_ elements: [Element]) {
|
||||
self.elements = elements
|
||||
}
|
||||
|
||||
func next() async -> Element? {
|
||||
events.append(.next)
|
||||
guard elements.count > 0 else {
|
||||
return nil
|
||||
}
|
||||
return elements.removeFirst()
|
||||
}
|
||||
|
||||
func makeAsyncIterator() -> ReportingAsyncSequence {
|
||||
events.append(.makeAsyncIterator)
|
||||
return self
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import AsyncAlgorithms
|
||||
|
||||
final class TestLazy: XCTestCase {
|
||||
func test_array() async {
|
||||
let source = [1, 2, 3, 4]
|
||||
let expected = source
|
||||
let sequence = source.async
|
||||
|
||||
var actual = [Int]()
|
||||
for await item in sequence {
|
||||
actual.append(item)
|
||||
}
|
||||
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
func test_set() async {
|
||||
let source: Set = [1, 2, 3, 4]
|
||||
let expected = source
|
||||
let sequence = source.async
|
||||
|
||||
var actual = Set<Int>()
|
||||
for await item in sequence {
|
||||
actual.insert(item)
|
||||
}
|
||||
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
func test_empty() async {
|
||||
let source = EmptyCollection<Int>()
|
||||
let expected = [Int]()
|
||||
let sequence = source.async
|
||||
|
||||
var actual = [Int]()
|
||||
for await item in sequence {
|
||||
actual.append(item)
|
||||
}
|
||||
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
func test_iteration() async {
|
||||
let source = ReportingSequence([1, 2, 3])
|
||||
let sequence = source.async
|
||||
XCTAssertEqual(source.events, [])
|
||||
var collected = [Int]()
|
||||
for await item in sequence {
|
||||
collected.append(item)
|
||||
}
|
||||
XCTAssertEqual(collected, [1, 2, 3])
|
||||
XCTAssertEqual(source.events, [
|
||||
.makeIterator,
|
||||
.next,
|
||||
.next,
|
||||
.next,
|
||||
.next
|
||||
])
|
||||
}
|
||||
|
||||
func test_manual_iteration() async {
|
||||
let source = ReportingSequence([1, 2, 3])
|
||||
let sequence = source.async
|
||||
XCTAssertEqual(source.events, [])
|
||||
var collected = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
collected.append(item)
|
||||
}
|
||||
XCTAssertEqual(collected, [1, 2, 3])
|
||||
XCTAssertEqual(source.events, [
|
||||
.makeIterator,
|
||||
.next,
|
||||
.next,
|
||||
.next,
|
||||
.next
|
||||
])
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertEqual(pastEnd, nil)
|
||||
// ensure that iterating past the end does not invoke next again
|
||||
XCTAssertEqual(source.events, [
|
||||
.makeIterator,
|
||||
.next,
|
||||
.next,
|
||||
.next,
|
||||
.next
|
||||
])
|
||||
}
|
||||
|
||||
func test_cancellation() async {
|
||||
let source = Indefinite(value: "test")
|
||||
let sequence = source.async
|
||||
let finished = expectation(description: "finished")
|
||||
let iterated = expectation(description: "iterated")
|
||||
let task = Task {
|
||||
var firstIteration = false
|
||||
for await _ in sequence {
|
||||
if !firstIteration {
|
||||
firstIteration = true
|
||||
iterated.fulfill()
|
||||
}
|
||||
}
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user