Skip to content

Commit 7eaa9cf

Browse files
committed
Execute tasks with large time gaps
Given tasks with large time gaps Then all the tasks are executed in order of scheduling
1 parent b7197a2 commit 7eaa9cf

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

Sources/AsyncSwiftly/TestingTaskGroup.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,26 @@ public struct TestingTaskGroup: ~Copyable {
6969
}
7070

7171
public mutating func addTask(at rawStep: Int, operation: sending @escaping @isolated(any) () async -> Void) {
72-
let instant = Clock.Instant(when: .step(rawStep))
72+
let duration = Clock.Step.step(rawStep)
73+
let instant = Clock.Instant(when: duration)
7374
let nextInstant = instant.advanced(by: .step(1))
7475
let executor = OperationExecutor(instant: nextInstant, queue: queue)
7576

77+
let shift: () -> Void = { [queue, clock] in
78+
var from = clock.now
79+
80+
repeat {
81+
let step = from
82+
queue.enqueue(until: step) {
83+
queue.dequeue(step)
84+
queue.advance()
85+
}
86+
from = from.advanced(by: .step(1))
87+
} while from <= instant
88+
}
89+
7690
group.addTask { [queue] in
77-
queue.enqueue(until: instant) {
78-
queue.dequeue(instant)
79-
queue.advance()
80-
}
91+
shift()
8192
await withTaskExecutorPreference(executor, operation: operation)
8293
queue.enqueue(until: nextInstant) {
8394
queue.dequeue(nextInstant)

Tests/AsyncSwiftlyTests/TestingTaskGroupTests.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct TestingTaskGroupTests {
1414
@Test("Given tasks are scheduled at the same time, Then all the tasks are executed in order of enqueueing")
1515
func executeTasksInOrderOfEnqueueing() async throws {
1616
let (stream, continuation) = AsyncStream.makeStream(of: Int.self)
17-
let operations = 0..<5
17+
let operations = 0..<100
1818

1919
try await withTestingTaskGroup { group in
2020
for operation in operations {
@@ -32,7 +32,7 @@ struct TestingTaskGroupTests {
3232
@Test("Given tasks are scheduled at different times, Then all the tasks are executed in order of scheduling")
3333
func executeTasksInOrderOfScheduling() async throws {
3434
let (stream, continuation) = AsyncStream.makeStream(of: Int.self)
35-
let source = 0..<5
35+
let source = 0..<100
3636

3737
try await withTestingTaskGroup { group in
3838
for (time, operation) in zip(source, source).reversed() {
@@ -47,6 +47,26 @@ struct TestingTaskGroupTests {
4747
await #expect(stream.collect() == Array(source))
4848
}
4949

50+
@Test("Given tasks with large time gaps, Then all the tasks are executed in order of scheduling")
51+
func executeTasksWithLargeTimeGaps() async throws {
52+
let (stream, continuation) = AsyncStream.makeStream(of: Int.self)
53+
54+
try await withTestingTaskGroup { group in
55+
group.addTask(at: 10) {
56+
continuation.yield(1)
57+
}
58+
group.addTask(at: 20) {
59+
continuation.yield(2)
60+
}
61+
group.addTask(at: 50) {
62+
continuation.yield(3)
63+
}
64+
}
65+
66+
continuation.finish()
67+
68+
await #expect(stream.collect() == [1, 2, 3])
69+
}
5070
@Test("Given task suspended by dependency, When another task resolves dependency, Then dependent task resumes its work")
5171
func resumeDependentTaskWhenDependencyIsResolved() async throws {
5272
let order = AsyncStream.makeStream(of: Int.self)

0 commit comments

Comments
 (0)