mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-29 04:54:21 +00:00
Add Interspersed proposal (#259)
* Clean up old evolution proposals # Motivation Our old proposals had a couple of stale links and were not all aligned. This cleans them up. * Add Interspersed proposal # Motivation Add a new evolution proposal for the interspersed algorithm.
This commit is contained in:
1 parent
0c00af2372
commit
b3394663fd
3 files changed
+176
-20
No files matched your search
+23
-16
@@ -66,26 +66,33 @@ final class TestInterspersed: XCTestCase {
|
||||
func test_cancellation() async {
|
||||
let source = Indefinite(value: "test")
|
||||
let sequence = source.async.interspersed(with: "sep")
|
||||
let finished = expectation(description: "finished")
|
||||
let iterated = expectation(description: "iterated")
|
||||
let task = Task {
|
||||
let lockStepChannel = AsyncChannel<Void>()
|
||||
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
let _ = await iterator.next()
|
||||
iterated.fulfill()
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
group.addTask {
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
let _ = await iterator.next()
|
||||
|
||||
while let _ = await iterator.next() { }
|
||||
// Information the parent task that we are consuming
|
||||
await lockStepChannel.send(())
|
||||
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
while let _ = await iterator.next() { }
|
||||
|
||||
finished.fulfill()
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
|
||||
// Information the parent task that we finished consuming
|
||||
await lockStepChannel.send(())
|
||||
}
|
||||
|
||||
// Waiting until the child task started consuming
|
||||
_ = await lockStepChannel.first { _ in true }
|
||||
|
||||
// Now we cancel the child
|
||||
group.cancelAll()
|
||||
|
||||
// Waiting until the child task finished consuming
|
||||
_ = await lockStepChannel.first { _ in true }
|
||||
}
|
||||
// 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