|
| 1 | +// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library) |
| 2 | + |
| 3 | +// REQUIRES: executable_test |
| 4 | +// REQUIRES: concurrency |
| 5 | +// REQUIRES: libdispatch |
| 6 | + |
| 7 | +// rdar://76038845 |
| 8 | +// REQUIRES: concurrency_runtime |
| 9 | +// UNSUPPORTED: back_deployment_runtime |
| 10 | + |
| 11 | +import StdlibUnittest |
| 12 | +import Darwin |
| 13 | +import Dispatch |
| 14 | + |
| 15 | +func loopUntil(priority: TaskPriority) async { |
| 16 | + while (Task.currentPriority != priority) { |
| 17 | + await Task.sleep(1_000_000_000) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func print(_ s: String = "") { |
| 22 | + fputs("\(s)\n", stderr) |
| 23 | +} |
| 24 | + |
| 25 | +func expectedBasePri(priority: TaskPriority) -> TaskPriority { |
| 26 | + let basePri = Task.basePriority! |
| 27 | + |
| 28 | + print("Testing basePri matching expected pri - \(basePri) == \(priority)") |
| 29 | + expectEqual(basePri, priority) |
| 30 | + return basePri |
| 31 | +} |
| 32 | + |
| 33 | +func expectedCurrentPri(priority: TaskPriority) -> TaskPriority { |
| 34 | + let curPri = Task.currentPriority |
| 35 | + print("Testing curPri matching expected pri - \(curPri) == \(priority)") |
| 36 | + expectEqual(curPri, priority) |
| 37 | + return curPri |
| 38 | +} |
| 39 | + |
| 40 | +func testNestedTaskPriority(basePri: TaskPriority, curPri: TaskPriority) async { |
| 41 | + let _ = expectedBasePri(priority: basePri) |
| 42 | + let _ = expectedCurrentPri(priority: curPri) |
| 43 | +} |
| 44 | + |
| 45 | +@main struct Main { |
| 46 | + static func main() async { |
| 47 | + |
| 48 | + let top_level = detach { /* To detach from main actor when running work */ |
| 49 | + |
| 50 | + let tests = TestSuite("Task base priority") |
| 51 | + if #available(SwiftStdlib 5.1, *) { |
| 52 | + |
| 53 | + tests.test("Structured concurrency base priority propagation") { |
| 54 | + let task = Task(priority: .background) { |
| 55 | + await loopUntil(priority: .default) |
| 56 | + |
| 57 | + let basePri = expectedBasePri(priority: .background) |
| 58 | + let curPri = expectedCurrentPri(priority: .default) |
| 59 | + |
| 60 | + // Structured concurrency via async let, escalated priority of |
| 61 | + // parent should propagate |
| 62 | + print("Testing propagation for async let structured concurrency child") |
| 63 | + async let child = testNestedTaskPriority(basePri: basePri, curPri: curPri) |
| 64 | + await child |
| 65 | + |
| 66 | + let dispatchGroup = DispatchGroup() |
| 67 | + // Structured concurrency via task groups, escalated priority should |
| 68 | + // propagate |
| 69 | + await withTaskGroup(of: Void.self, returning: Void.self) { group in |
| 70 | + dispatchGroup.enter() |
| 71 | + group.addTask { |
| 72 | + print("Testing propagation for task group regular child") |
| 73 | + let _ = await testNestedTaskPriority(basePri: basePri, curPri: curPri) |
| 74 | + dispatchGroup.leave() |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + dispatchGroup.enter() |
| 79 | + group.addTask(priority: .utility) { |
| 80 | + print("Testing propagation for task group child with specified priority") |
| 81 | + let _ = await testNestedTaskPriority(basePri: .utility, curPri: curPri) |
| 82 | + dispatchGroup.leave() |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Wait for child tasks to finish running, don't await since that |
| 87 | + // will escalate them |
| 88 | + dispatchGroup.wait() |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + await task.value // Escalate task BG->DEF |
| 93 | + } |
| 94 | + |
| 95 | + tests.test("Unstructured base priority propagation") { |
| 96 | + let task = Task(priority : .background) { |
| 97 | + await loopUntil(priority: .default) |
| 98 | + |
| 99 | + let basePri = expectedBasePri(priority: .background) |
| 100 | + let _ = expectedCurrentPri(priority: .default) |
| 101 | + |
| 102 | + let group = DispatchGroup() |
| 103 | + |
| 104 | + // Create an unstructured task |
| 105 | + group.enter() |
| 106 | + let _ = Task { |
| 107 | + let _ = await testNestedTaskPriority(basePri: basePri, curPri: basePri) |
| 108 | + group.leave() |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + // Wait for unstructured task to finish running, don't await it |
| 113 | + // since that will escalate |
| 114 | + group.wait() |
| 115 | + } |
| 116 | + |
| 117 | + await task.value // Escalate task BG->DEF |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + await runAllTestsAsync() |
| 122 | + } |
| 123 | + |
| 124 | + await top_level.value |
| 125 | + } |
| 126 | +} |
0 commit comments