Skip to content

Commit a32be44

Browse files
committed
Fix race condition in testHighPriorityTasksGetExecutedBeforeLowPriorityTasks
The assertion in this test was too strict. Even if the high priority tasks are scheduled first, they might finish after the low priority tasks (both high-priority and low-priority tasks sleep 100ms in these tests). For example the following is a valid recording of executions. ``` Set([high(0)]) Set([high(1), high(0)]) Set([high(1), high(2), high(0)]) Set([high(2), high(0), high(3), high(1)]) Set([high(2), high(3), high(1)]) Set([high(2), high(1)]) Set([high(2)]) Set([]) Set([high(4)]) Set([high(5), high(4)]) Set([high(6), high(5), high(4)]) Set([high(6), high(5), high(7), high(4)]) Set([high(6), high(5), high(7)]) Set([high(5), high(7)]) Set([high(5)]) Set([]) Set([high(8)]) Set([high(8), high(9)]) Set([high(8), high(9), low(0)]) Set([high(8), high(9), low(0), low(1)]) Set([high(8), high(9), low(0)]) Set([high(8), high(9)]) Set([high(9)]) ``` Change the assertion to check that all high-priority tasks start executing before the first low-priority task starts executing. rdar://145660858
1 parent 3e7f350 commit a32be44

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Tests/SemanticIndexTests/TaskSchedulerTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ final class TaskSchedulerTests: XCTestCase {
3939
}
4040
},
4141
validate: { (recordings: [Set<TaskID>]) in
42-
// Check that all high-priority tasks get executed before the low-priority tasks
43-
let highPriorityRecordingSlice = recordings.dropLast(while: {
44-
$0.isEmpty || $0.contains(where: \.isLowPriority)
42+
// Check that all high-priority tasks start executing before the low-priority tasks
43+
let highPriorityRecordingSlice = recordings.prefix(while: {
44+
$0.isEmpty || $0.contains(where: \.isHighPriority)
4545
})
46-
assertAllSatisfy(highPriorityRecordingSlice) { !$0.contains(where: \.isLowPriority) }
46+
let taskIdsInHighPriorityRecordingSlice = Set(highPriorityRecordingSlice.flatMap { $0 })
47+
XCTAssert(
48+
taskIdsInHighPriorityRecordingSlice.isSuperset(of: (0..<10).map(TaskID.highPriority)),
49+
"Low priority task started executing before high-priority task. Recording: \(recordings)"
50+
)
4751

4852
// Check that we never have more than the allowed number of low/high priority tasks, respectively
4953
assertAllSatisfy(recordings) { $0.count(where: \.isLowPriority) <= lowPriorityTasks }

0 commit comments

Comments
 (0)