Skip to content

Commit 9757181

Browse files
committed
Fix a non-deterministic test failure in the task scheduler
We were calling `TaskScheduler.schedule` inside a `Task` in `TaskSchedulerTests`. This means that Swift concurrency could re-order the execution of those tasks and thus change the order in which tasks were enqueued to the `TaskScheduler`. Caused by ahoppen@e295a4e#diff-6a451b3244d65ded9386e2816360b7b1debbe6a519e3795d2046c7dbefa7a7d7R351
1 parent 814f3c5 commit 9757181

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Tests/SKCoreTests/TaskSchedulerTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ final class TaskSchedulerTests: XCTestCase {
195195
}
196196
)
197197
}
198-
199198
}
200199

201200
// MARK: - Test helpers
@@ -354,8 +353,11 @@ fileprivate extension TaskScheduler<ClosureTaskDescription> {
354353
body,
355354
dependencies: dependencies
356355
)
356+
// Make sure that we call `schedule` outside of the `Task` because the execution order of `Task`s is not guaranteed
357+
// and if we called `schedule` inside `Task`, Swift concurrency can re-order the order that we schedule tasks in.
358+
let queuedTask = await self.schedule(priority: priority, taskDescription)
357359
return Task(priority: priority) {
358-
await self.schedule(priority: priority, taskDescription).waitToFinishPropagatingCancellation()
360+
await queuedTask.waitToFinishPropagatingCancellation()
359361
}
360362
}
361363
}

0 commit comments

Comments
 (0)