Skip to content

Commit a524bda

Browse files
committed
Update test_share_with_no_buffering implementation
1 parent a3f4b66 commit a524bda

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

Tests/AsyncAlgorithmsTests/TestShare.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,39 @@ final class TestShare: XCTestCase {
132132

133133
func test_share_with_no_buffering() async {
134134
let shared = [1, 2, 3, 4, 5].async.share(bufferingPolicy: .bounded(0))
135+
136+
let expectation1 = XCTestExpectation(description: "Consumer 1 finished looping")
137+
let expectation2 = XCTestExpectation(description: "Consumer 2 finished looping")
135138

136139
let results1 = Mutex([Int]())
137140
let results2 = Mutex([Int]())
141+
let gate1 = Gate()
142+
let gate2 = Gate()
138143

139144
let consumer1 = Task {
140-
// Consumer 1 reads first element
141-
for await value in shared {
145+
var iterator = shared.makeAsyncIterator()
146+
gate2.open()
147+
await gate1.enter()
148+
while let value = await iterator.next(isolation: nil) {
142149
results1.withLock { $0.append(value) }
143-
// Delay to allow consumer 2 to get ahead
144-
try? await Task.sleep(for: .milliseconds(10))
150+
// Add some delay to consumer 1
151+
try? await Task.sleep(for: .milliseconds(1))
145152
}
153+
expectation1.fulfill()
146154
}
147155

148156
let consumer2 = Task {
149-
// Consumer 2 reads all elements quickly
150-
for await value in shared {
157+
var iterator = shared.makeAsyncIterator()
158+
gate1.open()
159+
await gate2.enter()
160+
while let value = await iterator.next(isolation: nil) {
151161
results2.withLock { $0.append(value) }
152162
}
163+
expectation2.fulfill()
153164
}
154-
165+
166+
await fulfillment(of: [expectation1, expectation2], timeout: 5)
167+
155168
await consumer1.value
156169
await consumer2.value
157170

0 commit comments

Comments
 (0)