Skip to content

Commit 1e2f3fe

Browse files
authored
Merge pull request #11 from AsyncCommunity/fix/ConcurrentAccessRegulator
project: fix onNext scheduling in ConcurrentAccessRegulator
2 parents aae817b + 6081b19 commit 1e2f3fe

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
**v0.2.0 **
1+
**v0.2.1 - Lithium:**
2+
3+
- Enforce the call of onNext in a determinitisc way
4+
5+
**v0.2.0 - Helium:**
26

37
- AsyncStreams.CurrentValue `element` made public and available with get/set
48
- new Multicast operator
9+
- new Assign operator
510

611
**v0.1.0 - Hydrogen:**
712

Sources/Internal/ConcurrentAccessRegulator.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ final class ConcurrentAccessRegulator<UpstreamAsyncSequence: AsyncSequence>: @un
5858

5959
do {
6060
let next = try await self.upstreamAsyncIterator?.next()
61-
61+
await self.onNext(next)
62+
6263
await self.gate.unlock()
63-
64+
6465
// yield allows to promote other tasks to resume, giving a chance to request a next element
6566
await Task.yield()
66-
67-
await self.onNext(next)
6867
} catch is CancellationError {
6968
await self.onCancel()
7069
} catch {

Tests/AsyncSequences/AsyncSequences+MergeTests.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,17 @@ private struct TimedAsyncSequence<Element>: AsyncSequence, AsyncIteratorProtocol
4848

4949
final class AsyncSequences_MergeTests: XCTestCase {
5050
func testMerge_merges_sequences_according_to_the_timeline_using_asyncSequences() async throws {
51-
// -- 0 ------------------------------- 1200 ---------------------------
52-
// ------- 300 ------------- 900 ------------------------------ 1800 ---
53-
// --------------- 600 --------------------------- 1500 ----------------
54-
// -- a --- c ----- f ------- d --------- b -------- g ---------- e ----
51+
// -- 0 ------------------------------- 1000 ----------------------------- 2000 -
52+
// --------------- 500 --------------------------------- 1500 -------------------
53+
// -- a ----------- d ------------------ b --------------- e --------------- c --
5554
//
56-
// output should be: a c f d b g e
57-
let expectedElements = ["a", "c", "f", "d", "b", "g", "e"]
55+
// output should be: a, d, b, e, c
56+
let expectedElements = ["a", "d", "b", "e", "c"]
5857

59-
let asyncSequence1 = TimedAsyncSequence(intervalInMills: [0, 1200], sequence: ["a", "b"])
60-
let asyncSequence2 = TimedAsyncSequence(intervalInMills: [300, 600, 900], sequence: ["c", "d", "e"])
61-
let asyncSequence3 = TimedAsyncSequence(intervalInMills: [600, 1100], sequence: ["f", "g"])
58+
let asyncSequence1 = TimedAsyncSequence(intervalInMills: [0, 1000, 1000], sequence: ["a", "b", "c"])
59+
let asyncSequence2 = TimedAsyncSequence(intervalInMills: [500, 1000], sequence: ["d", "e"])
6260

63-
let sut = AsyncSequences.Merge(asyncSequence1, asyncSequence2, asyncSequence3)
61+
let sut = AsyncSequences.Merge(asyncSequence1, asyncSequence2)
6462

6563
var receivedElements = [String]()
6664
for try await element in sut {
@@ -187,7 +185,6 @@ final class AsyncSequences_MergeTests: XCTestCase {
187185
var receivedElements = [Int]()
188186
do {
189187
for try await element in sut {
190-
print("Received element \(element)")
191188
receivedElements.append(element)
192189
if element == 1 {
193190
canSend2Expectation.fulfill()

0 commit comments

Comments
 (0)