|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift |
| 3 | +// RUN: %target-build-swift -Xfrontend -disable-availability-checking -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out |
| 4 | +// RUN: %target-codesign %t/a.out |
| 5 | +// RUN: %target-run %t/a.out |
| 6 | + |
| 7 | +// REQUIRES: executable_test |
| 8 | +// REQUIRES: concurrency |
| 9 | +// REQUIRES: distributed |
| 10 | +// REQUIRES: concurrency_runtime |
| 11 | +// UNSUPPORTED: back_deployment_runtime |
| 12 | + |
| 13 | +// UNSUPPORTED: back_deploy_concurrency |
| 14 | +// UNSUPPORTED: use_os_stdlib |
| 15 | +// UNSUPPORTED: freestanding |
| 16 | + |
| 17 | +import StdlibUnittest |
| 18 | +import Distributed |
| 19 | +import FakeDistributedActorSystems |
| 20 | + |
| 21 | +typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem |
| 22 | + |
| 23 | +func checkAssumeLocalDistributedActor(actor: MainFriend) /* synchronous! */ -> String { |
| 24 | + assumeOnLocalDistributedActorExecutor(actor) { dist in |
| 25 | + print("gained access to: \(dist.isolatedProperty)") |
| 26 | + return dist.isolatedProperty |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func checkAssumeMainActor(actor: MainFriend) /* synchronous! */ { |
| 31 | + assumeOnMainActorExecutor { |
| 32 | + print("yay") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +@MainActor |
| 37 | +func check(actor: MainFriend) { |
| 38 | + _ = checkAssumeLocalDistributedActor(actor: actor) |
| 39 | + checkAssumeMainActor(actor: actor) |
| 40 | +} |
| 41 | + |
| 42 | +distributed actor MainFriend { |
| 43 | + nonisolated var unownedExecutor: UnownedSerialExecutor { |
| 44 | + print("get unowned executor") |
| 45 | + return MainActor.sharedUnownedExecutor |
| 46 | + } |
| 47 | + |
| 48 | + let isolatedProperty: String = "Hello there!" |
| 49 | + |
| 50 | + distributed func test(x: Int) async throws { |
| 51 | + print("executed: \(#function)") |
| 52 | + defer { |
| 53 | + print("done executed: \(#function)") |
| 54 | + } |
| 55 | + return checkAssumeMainActor(actor: self) |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +actor OtherMain { |
| 61 | + nonisolated var unownedExecutor: UnownedSerialExecutor { |
| 62 | + return MainActor.sharedUnownedExecutor |
| 63 | + } |
| 64 | + |
| 65 | + func checkAssumeLocalDistributedActor(actor: MainFriend) /* synchronous! */ { |
| 66 | + _ = assumeOnLocalDistributedActorExecutor(actor) { dist in |
| 67 | + print("gained access to: \(dist.isolatedProperty)") |
| 68 | + return dist.isolatedProperty |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +@main struct Main { |
| 74 | + static func main() async { |
| 75 | + let tests = TestSuite("AssumeLocalDistributedActorExecutor") |
| 76 | + |
| 77 | + let system = FakeRoundtripActorSystem() |
| 78 | + let distLocal = MainFriend(actorSystem: system) |
| 79 | + |
| 80 | + if #available(SwiftStdlib 5.9, *) { |
| 81 | + |
| 82 | + tests.test("assumeOnLocalDistributedActorExecutor: assume the main executor, inside the DistributedMainFriend local actor") { |
| 83 | + _ = checkAssumeLocalDistributedActor(actor: distLocal) |
| 84 | + try! await distLocal.test(x: 42) |
| 85 | + } |
| 86 | + |
| 87 | + tests.test("assumeOnLocalDistributedActorExecutor: assume same actor as the DistributedMainFriend") { |
| 88 | + await OtherMain().checkAssumeLocalDistributedActor(actor: distLocal) |
| 89 | + try! await distLocal.test(x: 42) |
| 90 | + } |
| 91 | + |
| 92 | + tests.test("assumeOnLocalDistributedActorExecutor: wrongly assume the same actor as the DistributedmainFriend") { |
| 93 | + await OtherMain().checkAssumeLocalDistributedActor(actor: distLocal) |
| 94 | + } |
| 95 | + |
| 96 | + tests.test("assumeOnLocalDistributedActorExecutor: on remote actor reference") { |
| 97 | + expectCrashLater(withMessage: "Cannot assume to be 'isolated MainFriend' since distributed actor 'a.MainFriend' is remote.") |
| 98 | + let remoteRef = try! MainFriend.resolve(id: distLocal.id, using: system) |
| 99 | + await OtherMain().checkAssumeLocalDistributedActor(actor: remoteRef) |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + await runAllTestsAsync() |
| 106 | + } |
| 107 | +} |
0 commit comments