|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-build-swift -Xfrontend -disable-availability-checking -parse-as-library %s -o %t/a.out |
| 3 | +// RUN: %target-codesign %t/a.out |
| 4 | +// RUN: %target-run %t/a.out |
| 5 | + |
| 6 | +// REQUIRES: executable_test |
| 7 | +// REQUIRES: concurrency |
| 8 | +// REQUIRES: concurrency_runtime |
| 9 | +// UNSUPPORTED: back_deployment_runtime |
| 10 | + |
| 11 | +// UNSUPPORTED: back_deploy_concurrency |
| 12 | +// UNSUPPORTED: use_os_stdlib |
| 13 | +// UNSUPPORTED: freestanding |
| 14 | + |
| 15 | +import StdlibUnittest |
| 16 | + |
| 17 | +func checkAssumeMainActor(echo: MainActorEcho) /* synchronous! */ { |
| 18 | + // Echo.get("any") // error: main actor isolated, cannot perform async call here |
| 19 | + assumeOnMainActorExecutor { |
| 20 | + let input = "example" |
| 21 | + let got = echo.get(input) |
| 22 | + precondition(got == "example", "Expected echo to match \(input)") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +@MainActor |
| 27 | +func mainActorCallCheck(echo: MainActorEcho) { |
| 28 | + checkAssumeMainActor(echo: echo) |
| 29 | +} |
| 30 | + |
| 31 | +actor MainFriend { |
| 32 | + nonisolated var unownedExecutor: UnownedSerialExecutor { |
| 33 | + MainActor.sharedUnownedExecutor |
| 34 | + } |
| 35 | + |
| 36 | + func callCheck(echo: MainActorEcho) { |
| 37 | + checkAssumeMainActor(echo: echo) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func checkAssumeSomeone(someone: Someone) /* synchronous */ { |
| 42 | + // someone.something // can't access, would need a hop but we can't |
| 43 | + assumeOnActorExecutor(someone) { someone in |
| 44 | + let something = someone.something |
| 45 | + let expected = "isolated something" |
| 46 | + precondition(something == expected, "expected '\(expected)', got: \(something)") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +actor Someone { |
| 51 | + func callCheckMainActor(echo: MainActorEcho) { |
| 52 | + checkAssumeMainActor(echo: echo) |
| 53 | + } |
| 54 | + |
| 55 | + func callCheckSomeone() { |
| 56 | + checkAssumeSomeone(someone: self) |
| 57 | + } |
| 58 | + |
| 59 | + var something: String { |
| 60 | + "isolated something" |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +actor SomeonesFriend { |
| 65 | + let someone: Someone |
| 66 | + nonisolated var unownedExecutor: UnownedSerialExecutor { |
| 67 | + self.someone.unownedExecutor |
| 68 | + } |
| 69 | + |
| 70 | + init(someone: Someone) { |
| 71 | + self.someone = someone |
| 72 | + } |
| 73 | + |
| 74 | + func callCheckSomeone() { |
| 75 | + checkAssumeSomeone(someone: someone) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +actor CompleteStranger { |
| 80 | + let someone: Someone |
| 81 | + init(someone: Someone) { |
| 82 | + self.someone = someone |
| 83 | + } |
| 84 | + |
| 85 | + func callCheckSomeone() { |
| 86 | + checkAssumeSomeone(someone: someone) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +@MainActor |
| 91 | +final class MainActorEcho { |
| 92 | + func get(_ key: String) -> String { |
| 93 | + key |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +@main struct Main { |
| 98 | + static func main() async { |
| 99 | + let tests = TestSuite("AssumeActorExecutor") |
| 100 | + |
| 101 | + let echo = MainActorEcho() |
| 102 | + |
| 103 | + if #available(SwiftStdlib 5.9, *) { |
| 104 | + // === MainActor -------------------------------------------------------- |
| 105 | + |
| 106 | + tests.test("assumeOnMainActorExecutor: assume the main executor, from 'main() async'") { |
| 107 | + await checkAssumeMainActor(echo: echo) |
| 108 | + } |
| 109 | + |
| 110 | + tests.test("assumeOnMainActorExecutor: assume the main executor, from MainActor method") { |
| 111 | + await mainActorCallCheck(echo: echo) |
| 112 | + } |
| 113 | + |
| 114 | + tests.test("assumeOnMainActorExecutor: assume the main executor, from actor on MainActor executor") { |
| 115 | + await MainFriend().callCheck(echo: echo) |
| 116 | + } |
| 117 | + |
| 118 | + tests.test("assumeOnMainActorExecutor: wrongly assume the main executor, from actor on other executor") { |
| 119 | + expectCrashLater(withMessage: "Incorrect actor executor assumption; Expected 'MainActor' executor.") |
| 120 | + await Someone().callCheckMainActor(echo: echo) |
| 121 | + } |
| 122 | + |
| 123 | + // === some Actor ------------------------------------------------------- |
| 124 | + |
| 125 | + let someone = Someone() |
| 126 | + tests.test("assumeOnActorExecutor: wrongly assume someone's executor, from 'main() async'") { |
| 127 | + expectCrashLater(withMessage: "Incorrect actor executor assumption; Expected same executor as a.Someone.") |
| 128 | + checkAssumeSomeone(someone: someone) |
| 129 | + } |
| 130 | + |
| 131 | + tests.test("assumeOnActorExecutor: wrongly assume someone's executor, from MainActor method") { |
| 132 | + expectCrashLater(withMessage: "Incorrect actor executor assumption; Expected same executor as a.Someone.") |
| 133 | + checkAssumeSomeone(someone: someone) |
| 134 | + } |
| 135 | + |
| 136 | + tests.test("assumeOnActorExecutor: assume someone's executor, from Someone") { |
| 137 | + await someone.callCheckSomeone() |
| 138 | + } |
| 139 | + |
| 140 | + tests.test("assumeOnActorExecutor: assume someone's executor, from actor on the Someone.unownedExecutor") { |
| 141 | + await SomeonesFriend(someone: someone).callCheckSomeone() |
| 142 | + } |
| 143 | + |
| 144 | + tests.test("assumeOnActorExecutor: wrongly assume the main executor, from actor on other executor") { |
| 145 | + expectCrashLater(withMessage: "Incorrect actor executor assumption; Expected same executor as a.Someone.") |
| 146 | + await CompleteStranger(someone: someone).callCheckSomeone() |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + await runAllTestsAsync() |
| 153 | + } |
| 154 | +} |
0 commit comments