1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-distributed -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed
// rdar://76038845
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
// UNSUPPORTED: freestanding
// FIXME(distributed): Distributed actors currently have some issues on windows rdar://82593574
// UNSUPPORTED: OS=windows-msvc
import StdlibUnittest
import Distributed
import FakeDistributedActorSystems
typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem
@available(SwiftStdlib 5.9, *)
distributed actor MainWorker: Worker {
nonisolated var unownedExecutor: UnownedSerialExecutor {
print("get unowned executor")
return MainActor.sharedUnownedExecutor
}
}
@available(SwiftStdlib 5.9, *)
distributed actor NormalWorker: Worker {
// empty on purpose, default executor
}
protocol Worker: DistributedActor {
}
extension Worker {
distributed func preconditionSameExecutor(as other: some Worker) {
other.preconditionIsolated("Expected for [\(self)] share executor with [\(other)]")
}
}
actor EnqueueTest {
let unownedExecutor: UnownedSerialExecutor
var field: Int = 0
init(unownedExecutor: UnownedSerialExecutor) {
self.unownedExecutor = unownedExecutor
}
func test() {
// do something, so the test call does not get optimized away (if it was just an empty method)
self.field += 1
}
}
@main struct Main {
static func main() async {
let tests = TestSuite("AssumeDistributedActorExecutor")
if #available(SwiftStdlib 5.9, *) {
let system = DefaultDistributedActorSystem()
let normalLocalWorker = NormalWorker(actorSystem: system)
precondition(__isLocalActor(normalLocalWorker), "must be local")
let normalRemoteWorker = try! NormalWorker.resolve(id: normalLocalWorker.id, using: system)
precondition(__isRemoteActor(normalRemoteWorker), "must be remote")
precondition(normalLocalWorker.id == normalRemoteWorker.id, "IDs must be equal")
tests.test("exactly the same actor") {
try! await normalLocalWorker.preconditionSameExecutor(as: normalLocalWorker)
}
tests.test("different normal local worker, not same executor") {
expectCrashLater(withMessage: "Incorrect actor executor assumption; Expected 'UnownedSerialExecutor(executor: (Opaque Value))' executor. Expected for [main.NormalWorker] share executor with main.NormalWorker")
let other = NormalWorker(actorSystem: system)
try! await normalLocalWorker.preconditionSameExecutor(as: other)
}
tests.test("remote actor reference should have crash-on-enqueue executor") {
expectCrashLater(withMessage: "Attempted to enqueue ExecutorJob (ExecutorJob(id: 1)) on executor of remote distributed actor reference!")
// we do the bad idea of taking an executor from a remote worker
// and then force another actor to run on it; this will cause an enqueue on the "crash on enqueue" executor.
let wrongUse = EnqueueTest(unownedExecutor: normalRemoteWorker.unownedExecutor)
await wrongUse.test()
}
}
await runAllTestsAsync()
}
}
|