Skip to content

Commit 9184733

Browse files
committed
Add some first drafts at unit tests for verifying share behaviors
1 parent e342eb0 commit 9184733

File tree

4 files changed

+611
-7
lines changed

4 files changed

+611
-7
lines changed

Evolution/NNNN-share.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ The order of the interleaving of the prints are not guaranteed; however the orde
9898
If the creation were instead altered to the following:
9999

100100
```swift
101-
let exampleSource = [0, 1, 2, 3, 4].async.share(bufferingPolicy: .bufferingNewest(2))
101+
let exampleSource = [0, 1, 2, 3, 4].async.share(bufferingPolicy: .bufferingLatest(2))
102102
```
103103

104104
The output would print the possible ordering of:

Sources/AsyncAlgorithms/AsyncShareSequence.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ struct AsyncShareSequence<Base: AsyncSequence>: Sendable where Base.Element: Sen
227227
var generation = 0
228228
var sides = [Int: Side.State]()
229229
var iteratingTask: IteratingTask
230-
var buffer = [Element]()
231-
var finished = false
232-
var failure: Failure?
230+
private(set) var buffer = [Element]()
231+
private(set) var finished = false
232+
private(set) var failure: Failure?
233233
var cancelled = false
234234
var limit: UnsafeContinuation<Bool, Never>?
235235
var demand: UnsafeContinuation<Void, Never>?
@@ -485,11 +485,10 @@ struct AsyncShareSequence<Base: AsyncSequence>: Sendable where Base.Element: Sen
485485
if let element {
486486
state.enqueue(element)
487487
} else {
488-
state.finished = true
488+
state.finish()
489489
}
490490
case .failure(let failure):
491-
state.finished = true
492-
state.failure = failure
491+
state.fail(failure)
493492
}
494493
for (id, side) in state.sides {
495494
if let continuation = side.continuaton {

Tests/AsyncAlgorithmsTests/Support/GatedSequence.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
public struct GatedSequence<Element> {
13+
public typealias Failure = Never
1314
let elements: [Element]
1415
let gates: [Gate]
1516
var index = 0
@@ -44,6 +45,15 @@ extension GatedSequence: AsyncSequence {
4445
await gate.enter()
4546
return element
4647
}
48+
49+
public mutating func next(isolation actor: isolated (any Actor)?) async throws(Never) -> Element? {
50+
guard gatedElements.count > 0 else {
51+
return nil
52+
}
53+
let (element, gate) = gatedElements.removeFirst()
54+
await gate.enter()
55+
return element
56+
}
4757
}
4858

4959
public func makeAsyncIterator() -> Iterator {

0 commit comments

Comments
 (0)