|
| 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 -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out |
| 4 | +// RUN: %target-run %t/a.out | %FileCheck %s --color --dump-input=always |
| 5 | + |
| 6 | +// REQUIRES: executable_test |
| 7 | +// REQUIRES: concurrency |
| 8 | +// REQUIRES: distributed |
| 9 | + |
| 10 | +// rdar://76038845 |
| 11 | +// UNSUPPORTED: use_os_stdlib |
| 12 | +// UNSUPPORTED: back_deployment_runtime |
| 13 | + |
| 14 | +// FIXME(distributed): Distributed actors currently have some issues on windows, isRemote always returns false. rdar://82593574 |
| 15 | +// UNSUPPORTED: OS=windows-msvc |
| 16 | + |
| 17 | +import Distributed |
| 18 | +import FakeDistributedActorSystems |
| 19 | + |
| 20 | + |
| 21 | +typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem |
| 22 | + |
| 23 | +protocol DistributedWorker: DistributedActor where ActorSystem == DefaultDistributedActorSystem { |
| 24 | + associatedtype WorkItem: Sendable & Codable |
| 25 | + associatedtype WorkResult: Sendable & Codable |
| 26 | + |
| 27 | + distributed func submit(work: WorkItem) async throws -> WorkResult |
| 28 | +} |
| 29 | + |
| 30 | +distributed actor TheWorker: DistributedWorker { |
| 31 | + typealias ActorSystem = DefaultDistributedActorSystem |
| 32 | + typealias WorkItem = String |
| 33 | + typealias WorkResult = String |
| 34 | + |
| 35 | + distributed func submit(work: WorkItem) async throws -> WorkResult { |
| 36 | + "\(Self.self) echo: \(work)" |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +actor WorkerPool<Worker: DistributedWorker> { |
| 41 | + typealias ActorSystem = FakeRoundtripActorSystem |
| 42 | + typealias WorkItem = Worker.WorkItem |
| 43 | + typealias WorkResult = Worker.WorkResult |
| 44 | + |
| 45 | + let actorSystem: ActorSystem |
| 46 | + let worker: Worker |
| 47 | + |
| 48 | + init(worker: Worker, actorSystem: ActorSystem) { |
| 49 | + self.worker = worker |
| 50 | + self.actorSystem = actorSystem |
| 51 | + } |
| 52 | + |
| 53 | + func submit(work: WorkItem) async throws -> WorkResult { |
| 54 | + let worker = try await self.selectWorker() |
| 55 | + return try await worker.submit(work: work) |
| 56 | + } |
| 57 | + |
| 58 | + func selectWorker() async throws -> Worker { |
| 59 | + return self.worker |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func test() async throws { |
| 64 | + let system = DefaultDistributedActorSystem() |
| 65 | + |
| 66 | + let w = TheWorker(actorSystem: system) |
| 67 | + let remoteW = try! TheWorker.resolve(id: w.id, using: system) |
| 68 | + print("remoteW is remote: \(__isRemoteActor(remoteW))") |
| 69 | + |
| 70 | + // direct calls work ok: |
| 71 | +// let reply = try await remoteW.submit(work: "Hello") |
| 72 | + |
| 73 | + // Fails with: |
| 74 | + // Cannot handle types other than extensions and actor declarations in distributed function checking. |
| 75 | + // UNREACHABLE executed at /Users/ktoso/code/swift-project/swift/lib/Sema/TypeCheckDistributed.cpp:514! |
| 76 | + func callWorker<W: DistributedWorker>(w: W) async throws -> String where W.WorkItem == String, W.WorkResult == String { |
| 77 | + try await w.submit(work: "Hello") |
| 78 | + } |
| 79 | + let reply = try await callWorker(w: remoteW) |
| 80 | + |
| 81 | +// let pool = WorkerPool<TheWorker>(worker: remoteW, actorSystem: system) |
| 82 | +// let reply = try await pool.submit(work: "Hello") |
| 83 | + // CHECK: >> remoteCall: on:main.TheWorker, target:main.TheWorker.submit(work:), invocation:FakeInvocationEncoder(genericSubs: [], arguments: ["Hello"], returnType: Optional(Swift.String), errorType: Optional(Swift.Error)), throwing:Swift.Error, returning:Swift.String |
| 84 | + |
| 85 | + // CHECK: << remoteCall return: TheWorker echo: Hello |
| 86 | + print("reply: \(reply)") |
| 87 | + // CHECK: reply: TheWorker echo: Hello |
| 88 | +} |
| 89 | + |
| 90 | +@main struct Main { |
| 91 | + static func main() async { |
| 92 | + try! await test() |
| 93 | + } |
| 94 | +} |
0 commit comments