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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -parse-as-library) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed
// rdar://76038845
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
import Distributed
import _Concurrency
struct Boom: Error {
let whoFailed: String
init(_ whoFailed: String) {
self.whoFailed = whoFailed
}
}
distributed actor SomeSpecificDistributedActor {
let state: String = "hi there"
distributed func helloAsyncThrows() async throws -> String {
"local(\(#function))"
}
distributed func helloAsync() async -> String {
"local(\(#function))"
}
distributed func helloThrows() throws -> String {
"local(\(#function))"
}
distributed func hello() -> String {
"local(\(#function))"
}
distributed func callTaskSelf_inner() async throws -> String {
"local(\(#function))"
}
distributed func callTaskSelf() async -> String {
do {
return try await Task {
let called = try await callTaskSelf_inner() // shouldn't use the distributed thunk!
return "local(\(#function)) -> \(called)"
}.value
} catch {
return "WRONG local(\(#function)) thrown(\(error))"
}
}
distributed func callDetachedSelf() async -> String {
do {
return try await Task.detached {
let called = try await self.callTaskSelf_inner() // shouldn't use the distributed thunk!
return "local(\(#function)) -> \(called)"
}.value
} catch {
return "WRONG local(\(#function)) thrown(\(error))"
}
}
// === errors
distributed func helloThrowsImplBoom() throws -> String {
throw Boom("impl")
}
distributed func helloThrowsTransportBoom() throws -> String {
"local(\(#function))"
}
}
// ==== Execute ----------------------------------------------------------------
@_silgen_name("swift_distributed_actor_is_remote")
func __isRemoteActor(_ actor: AnyObject) -> Bool
func __isLocalActor(_ actor: AnyObject) -> Bool {
return !__isRemoteActor(actor)
}
// ==== Fake Transport ---------------------------------------------------------
struct ActorAddress: Sendable, Hashable, Codable {
let address: String
}
struct FakeActorSystem: DistributedActorSystem {
typealias ActorID = ActorAddress
typealias InvocationDecoder = FakeInvocationDecoder
typealias InvocationEncoder = FakeInvocationEncoder
typealias SerializationRequirement = Codable
typealias ResultHandler = FakeResultHandler
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
where Act: DistributedActor,
Act.ID == ActorID {
nil
}
func assignID<Act>(_ actorType: Act.Type) -> ActorID
where Act: DistributedActor {
ActorAddress(address: "")
}
func actorReady<Act>(_ actor: Act)
where Act: DistributedActor,
Act.ID == ActorID {
}
func resignID(_ id: ActorID) {
}
func makeInvocationEncoder() -> InvocationEncoder {
.init()
}
public func remoteCall<Act, Err, Res>(
on actor: Act,
target: RemoteCallTarget,
invocation invocationEncoder: inout InvocationEncoder,
throwing: Err.Type,
returning: Res.Type
) async throws -> Res
where Act: DistributedActor,
Act.ID == ActorID,
Err: Error,
Res: SerializationRequirement {
guard "\(target)" != "main.SomeSpecificDistributedActor.helloThrowsTransportBoom()" else {
throw Boom("system")
}
return "remote(\(target))" as! Res
}
func remoteCallVoid<Act, Err>(
on actor: Act,
target: RemoteCallTarget,
invocation invocationEncoder: inout InvocationEncoder,
throwing: Err.Type
) async throws
where Act: DistributedActor,
Act.ID == ActorID,
Err: Error {
fatalError("not implemented: \(#function)")
}
}
// === Sending / encoding -------------------------------------------------
struct FakeInvocationEncoder: DistributedTargetInvocationEncoder {
typealias SerializationRequirement = Codable
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
mutating func recordArgument<Value: SerializationRequirement>(_ argument: RemoteCallArgument<Value>) throws {}
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
mutating func doneRecording() throws {}
}
// === Receiving / decoding -------------------------------------------------
class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
typealias SerializationRequirement = Codable
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument { fatalError() }
func decodeReturnType() throws -> Any.Type? { nil }
func decodeErrorType() throws -> Any.Type? { nil }
}
public struct FakeResultHandler: DistributedTargetInvocationResultHandler {
public typealias SerializationRequirement = Codable
public func onReturn<Success: SerializationRequirement>(value: Success) async throws {
fatalError("Not implemented: \(#function)")
}
public func onReturnVoid() async throws {
fatalError("Not implemented: \(#function)")
}
public func onThrow<Err: Error>(error: Err) async throws {
fatalError("Not implemented: \(#function)")
}
}
@available(SwiftStdlib 5.5, *)
typealias DefaultDistributedActorSystem = FakeActorSystem
// ==== Execute ----------------------------------------------------------------
func test_remote_invoke(address: ActorAddress, system: FakeActorSystem) async {
func check(actor: SomeSpecificDistributedActor) async {
let personality = __isRemoteActor(actor) ? "remote" : "local"
let h1 = try! await actor.helloAsyncThrows()
print("\(personality) - helloAsyncThrows: \(h1)")
let h2 = try! await actor.helloAsync()
print("\(personality) - helloAsync: \(h2)")
let h3 = try! await actor.helloThrows()
print("\(personality) - helloThrows: \(h3)")
let h4 = try! await actor.hello()
print("\(personality) - hello: \(h4)")
let h5 = try! await actor.callTaskSelf()
print("\(personality) - callTaskSelf: \(h5)")
let h6 = try! await actor.callDetachedSelf()
print("\(personality) - callDetachedSelf: \(h6)")
// error throws
if __isRemoteActor(actor) {
do {
let h7 = try await actor.helloThrowsTransportBoom()
print("WRONG: helloThrowsTransportBoom: should have thrown; got: \(h7)")
} catch {
print("\(personality) - helloThrowsTransportBoom: \(error)")
}
} else {
do {
let h8 = try await actor.helloThrowsImplBoom()
print("WRONG: helloThrowsImplBoom: Should have thrown; got: \(h8)")
} catch {
print("\(personality) - helloThrowsImplBoom: \(error)")
}
}
}
let remote = try! SomeSpecificDistributedActor.resolve(id: address, using: system)
assert(__isRemoteActor(remote) == true, "should be remote")
let local = SomeSpecificDistributedActor(actorSystem: system)
assert(__isRemoteActor(local) == false, "should be local")
print("local isRemote: \(__isRemoteActor(local))")
// CHECK: local isRemote: false
await check(actor: local)
// CHECK: local - helloAsyncThrows: local(helloAsyncThrows())
// CHECK: local - helloAsync: local(helloAsync())
// CHECK: local - helloThrows: local(helloThrows())
// CHECK: local - hello: local(hello())
// CHECK: local - callTaskSelf: local(callTaskSelf()) -> local(callTaskSelf_inner())
// CHECK: local - callDetachedSelf: local(callDetachedSelf()) -> local(callTaskSelf_inner())
// CHECK: local - helloThrowsImplBoom: Boom(whoFailed: "impl")
print("remote isRemote: \(__isRemoteActor(remote))")
// CHECK: remote isRemote: true
await check(actor: remote)
// CHECK: remote - helloAsyncThrows: remote(main.SomeSpecificDistributedActor.helloAsyncThrows())
// CHECK: remote - helloAsync: remote(main.SomeSpecificDistributedActor.helloAsync())
// CHECK: remote - helloThrows: remote(main.SomeSpecificDistributedActor.helloThrows())
// CHECK: remote - hello: remote(main.SomeSpecificDistributedActor.hello())
// CHECK: remote - callTaskSelf: remote(main.SomeSpecificDistributedActor.callTaskSelf())
// CHECK: remote - callDetachedSelf: remote(main.SomeSpecificDistributedActor.callDetachedSelf())
// CHECK: remote - helloThrowsTransportBoom: Boom(whoFailed: "system")
print(local)
print(remote)
}
@main struct Main {
static func main() async {
let address = ActorAddress(address: "")
let system = DefaultDistributedActorSystem()
await test_remote_invoke(address: address, system: system)
}
}
|