|
| 1 | +// RUN: %target-run-simple-swift( -swift-version 6 -g %import-libdispatch -import-objc-header %S/Inputs/RunOnMainActor.h -enable-experimental-feature NonIsolatedAsyncInheritsIsolationFromContext ) |
| 2 | + |
| 3 | +// REQUIRES: executable_test |
| 4 | +// REQUIRES: concurrency |
| 5 | +// REQUIRES: concurrency_runtime |
| 6 | +// REQUIRES: libdispatch |
| 7 | +// REQUIRES: asserts |
| 8 | + |
| 9 | +// REQUIRES: swift_feature_NonIsolatedAsyncInheritsIsolationFromContext |
| 10 | + |
| 11 | +// UNSUPPORTED: freestanding |
| 12 | + |
| 13 | +import StdlibUnittest |
| 14 | + |
| 15 | +//////////////////////// |
| 16 | +// MARK: Declarations // |
| 17 | +//////////////////////// |
| 18 | + |
| 19 | +@_silgen_name("dispatch_assert_queue") |
| 20 | +func dispatch_assertQueue(_ ptr: UnsafeRawPointer) |
| 21 | + |
| 22 | +func checkIfOnMainQueue() { |
| 23 | + dispatch_assertQueue(getDispatchMain()) |
| 24 | +} |
| 25 | + |
| 26 | +actor Custom { |
| 27 | +} |
| 28 | + |
| 29 | +@globalActor |
| 30 | +struct CustomActor { |
| 31 | + static var shared: Custom { |
| 32 | + return Custom() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +let tests = TestSuite("NonIsolatedInheritsIsolation") |
| 37 | + |
| 38 | +tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () -> () in |
| 39 | + expectCrashLater() |
| 40 | + checkIfOnMainQueue() |
| 41 | +} |
| 42 | + |
| 43 | +tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () async -> () in |
| 44 | + checkIfOnMainQueue() |
| 45 | +} |
| 46 | + |
| 47 | +tests.test("checkIfOnMainQueue crashes off the main queue") { |
| 48 | + expectCrashLater() |
| 49 | + await { @CustomActor in |
| 50 | + print("=> checkIfOnMainQueue crashes off the main queue") |
| 51 | + checkIfOnMainQueue() |
| 52 | + }() |
| 53 | +} |
| 54 | + |
| 55 | +tests.test("checkIfOnMainQueue crashes off the main queue 2") { @CustomActor () async -> () in |
| 56 | + expectCrashLater() |
| 57 | + print("=> checkIfOnMainQueue crashes off the main queue 2") |
| 58 | + checkIfOnMainQueue() |
| 59 | +} |
| 60 | + |
| 61 | +///////////////// |
| 62 | +// MARK: Tests // |
| 63 | +///////////////// |
| 64 | + |
| 65 | +nonisolated func nonisolatedCheckIfOnMainQueue() async { |
| 66 | + checkIfOnMainQueue() |
| 67 | +} |
| 68 | + |
| 69 | +tests.test("Check if nonisolated inherits MainActor does not crash") { @MainActor () async -> () in |
| 70 | + await nonisolatedCheckIfOnMainQueue() |
| 71 | +} |
| 72 | + |
| 73 | +tests.test("Check if nonisolated inherits CustomActor crashes") { @CustomActor () async -> () in |
| 74 | + expectCrashLater() |
| 75 | + await nonisolatedCheckIfOnMainQueue() |
| 76 | +} |
| 77 | + |
| 78 | +tests.test("Check if nonisolated inheriting nonisolated crashes") { () async -> () in |
| 79 | + // By default this closure is @MainActor isolated. We detach on a background |
| 80 | + // thread and sleep 5 seconds to get the crash. The crash should happen |
| 81 | + // quicker than 5 seconds... we just want to make sure that the code does not |
| 82 | + // exit before we crash. |
| 83 | + expectCrashLater() |
| 84 | + Task.detached { |
| 85 | + await nonisolatedCheckIfOnMainQueue() |
| 86 | + } |
| 87 | + sleep(5) |
| 88 | +} |
| 89 | + |
| 90 | +@MainActor func run() async { |
| 91 | + await runAllTestsAsync() |
| 92 | +} |
| 93 | + |
| 94 | +await run() |
0 commit comments