|
| 1 | +// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -Xfrontend -disable-dynamic-actor-isolation -swift-version 6 -g -import-objc-header %S/Inputs/RunOnMainActor.h %import-libdispatch ) |
| 2 | + |
| 3 | +// REQUIRES: executable_test |
| 4 | +// REQUIRES: concurrency |
| 5 | +// REQUIRES: concurrency_runtime |
| 6 | +// REQUIRES: libdispatch |
| 7 | +// REQUIRES: asserts |
| 8 | + |
| 9 | +// UNSUPPORTED: freestanding |
| 10 | + |
| 11 | +// For now we do not support back deployment or use os stdlib |
| 12 | +// UNSUPPORTED: back_deployment_concurrency |
| 13 | +// UNSUPPORTED: use_os_stdlib |
| 14 | + |
| 15 | +import StdlibUnittest |
| 16 | +import Dispatch |
| 17 | + |
| 18 | +// MARK: Test runOnMainActor in all modes. We use the one future proof API in |
| 19 | +// dispatch: dispatch_assert_queue. |
| 20 | + |
| 21 | +//////////////////////// |
| 22 | +// MARK: Declarations // |
| 23 | +//////////////////////// |
| 24 | + |
| 25 | +@_silgen_name("dispatch_assert_queue") |
| 26 | +func dispatch_assertQueue(_ ptr: UnsafeRawPointer) |
| 27 | + |
| 28 | +func checkIfOnMainQueue() { |
| 29 | + dispatch_assertQueue(getDispatchMain()) |
| 30 | +} |
| 31 | + |
| 32 | +actor Custom { |
| 33 | +} |
| 34 | + |
| 35 | +@globalActor |
| 36 | +struct CustomActor { |
| 37 | + static var shared: Custom { |
| 38 | + return Custom() |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +///////////////// |
| 43 | +// MARK: Tests // |
| 44 | +///////////////// |
| 45 | + |
| 46 | +let tests = TestSuite("RunOnMainActor") |
| 47 | + |
| 48 | +tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () -> () in |
| 49 | + // Why do we crash if this is synchronous. |
| 50 | + expectCrashLater() |
| 51 | + checkIfOnMainQueue() |
| 52 | +} |
| 53 | + |
| 54 | +tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () async -> () in |
| 55 | + checkIfOnMainQueue() |
| 56 | +} |
| 57 | + |
| 58 | +tests.test("checkIfOnMainQueue crashes off the main queue") { |
| 59 | + expectCrashLater() |
| 60 | + await { @CustomActor in |
| 61 | + print("=> checkIfOnMainQueue crashes off the main queue") |
| 62 | + checkIfOnMainQueue() |
| 63 | + }() |
| 64 | +} |
| 65 | + |
| 66 | +tests.test("checkIfOnMainQueue crashes off the main queue 2") { @CustomActor () async -> () in |
| 67 | + expectCrashLater() |
| 68 | + print("=> checkIfOnMainQueue crashes off the main queue 2") |
| 69 | + checkIfOnMainQueue() |
| 70 | +} |
| 71 | + |
| 72 | +tests.test("checkIfOnMainQueue crashes using pure dispatch queue") { @CustomActor () async -> () in |
| 73 | + expectCrashLater() |
| 74 | + |
| 75 | + let queue = DispatchQueue(label: "") |
| 76 | + |
| 77 | + await withCheckedContinuation { cont in |
| 78 | + queue.async { |
| 79 | + checkIfOnMainQueue() |
| 80 | + cont.resume() |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +tests.test("RunOnMainActor on MainActor") { @MainActor () async -> () in |
| 86 | + // This is main actor isolated. |
| 87 | + _taskRunOnMainActor { @MainActor in |
| 88 | + print("=> RunOnMainActor on MainActor") |
| 89 | + checkIfOnMainQueue() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +tests.test("RunOnMainActor off MainActor") { |
| 94 | + await { @CustomActor in |
| 95 | + _taskRunOnMainActor { @MainActor in |
| 96 | + print("=> RunOnMainActor off MainActor") |
| 97 | + checkIfOnMainQueue() |
| 98 | + } |
| 99 | + }() |
| 100 | +} |
| 101 | + |
| 102 | +tests.test("RunOnMainActor off MainActor 2") { @CustomActor () async -> () in |
| 103 | + _taskRunOnMainActor { |
| 104 | + print("=> RunOnMainActor off MainActor 2") |
| 105 | + checkIfOnMainQueue() |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// This succeeds since assert understands that a child queue runs on the main |
| 110 | +// queue. |
| 111 | +tests.test("checkIfOnMainQueue in SubQueue from MainActor") { @CustomActor () async -> () in |
| 112 | + let childQueue = DispatchQueue.init(label: "", qos: .background, attributes: [], autoreleaseFrequency: .inherit, target: DispatchQueue.main) |
| 113 | + |
| 114 | + print("=> checkIfOnMainQueue in SubQueue from MainActor start!") |
| 115 | + |
| 116 | + // We cannot use the checked continuation, since we are not going to come back |
| 117 | + // to the custom actor. |
| 118 | + await withCheckedContinuation { cont in |
| 119 | + childQueue.async { |
| 120 | + print("=> checkIfOnMainQueue in SubQueue from MainActor") |
| 121 | + checkIfOnMainQueue() |
| 122 | + cont.resume() |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// This should not fail when backwards deployed since we use the backwards |
| 128 | +// compatibility trick. |
| 129 | +tests.test("taskRunOnMainActor in SubQueue from MainActor") { @CustomActor () async -> () in |
| 130 | + let childQueue = DispatchQueue.init(label: "", qos: .background, attributes: [], autoreleaseFrequency: .inherit, target: DispatchQueue.main) |
| 131 | + |
| 132 | + await withCheckedContinuation { cont in |
| 133 | + childQueue.async { |
| 134 | + _taskRunOnMainActor { @MainActor () -> () in |
| 135 | + print("=> taskRunOnMainActor in SubQueue from MainActor") |
| 136 | + checkIfOnMainQueue() |
| 137 | + cont.resume() |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +tests.test("taskRunOnMainActor in SubQueue off MainActor") { @CustomActor () async -> () in |
| 144 | + let d = DispatchQueue.init(label: "", qos: .background) |
| 145 | + let childQueue = DispatchQueue.init(label: "", qos: .background, attributes: [], autoreleaseFrequency: .inherit, target: d) |
| 146 | + |
| 147 | + await withCheckedContinuation { cont in |
| 148 | + childQueue.async { |
| 149 | + _taskRunOnMainActor { @MainActor in |
| 150 | + print("=> taskRunOnMainActor in SubQueue from MainActor") |
| 151 | + checkIfOnMainQueue() |
| 152 | + cont.resume() |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +await runAllTestsAsync() |
0 commit comments