Files
swift-mirror/test/Distributed/Runtime/distributed_actor_remote_functions.swift
Konrad `ktoso` Malawski 5ab8e0834d [Distributed] Reimplement distributed call thunks completely in AST (#41616)
* [Distributed] dist actor always has default executor (currently)

* [Distributed] extra test for missing makeEncoder

* [DistributedDecl] Add DistributedActorSystem to known SDK types

* [DistributedActor] ok progress on getting the system via witness

* [Distributed] allow hop-to `let any: any X` where X is DistActor

* [Distributed] AST: Add an accessor to determine whether type is distributed actor

- Classes have specialized method on their declarations
- Archetypes and existentials check their conformances for
  presence of `DistributedActor` protocol.

* [Distributed] AST: Account for distributed members declared in class extensions

`getConcreteReplacementForProtocolActorSystemType` should use `getSelfClassDecl`
otherwise it wouldn't be able to find actor if the member is declared in an extension.

* [Distributed] fix ad-hoc requirement checks for 'mutating'

[PreChecker] LookupDC might be null, so account for that

* [Distributed] Completed AST synthesis for dist thunk

* [Distributed][ASTDumper] print pretty distributed in right color in AST dumps

* wip on making the local/remote calls

* using the _local to mark the localCall as known local

* [Distributed] fix passing Never when not throwing

* fix lifetime of mangled string

* [Distributed] Implement recordGenericSubstitution

* [Distributed] Dont add .

* [Distributed] dont emit thunk when func broken

* [Distributed] fix tests; cleanups

* [Distributed] cleanup, move is... funcs to DistributedDecl

* [Distributed] Remove SILGen for distributed thunks, it is in Sema now!

* [Distributed]  no need to check stored props in protocols

* remote not used flag

* fix mangling test

* [Distributed] Synthesis: Don't re-use AST nodes for `decodeArgument` references

* [Distributed] Synthesis: Make sure that each thunk parameter has an internal name

* [Distributed/Synthesis] NFC: Add a comment regarding empty internal parameter names

* [Distributed] NFC: Adjust distributed thunk manglings in the accessor section test-cases

* cleanup

* [Distributed] NFC: Adjust distributed thunk manglings in the accessor thunk test-cases

* review follow ups

* xfail some linux tests for now so we can land the AST thunk

* Update distributed_actor_remote_functions.swift

Co-authored-by: Pavel Yaskevich <xedin@apache.org>
2022-03-10 23:58:23 +09:00

266 lines
8.7 KiB
Swift

// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -Xfrontend -enable-experimental-distributed -parse-as-library) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed
// rdar://76038845
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
// FIXME(distributed): rdar://90078069
// UNSUPPORTED: linux
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
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.mangledName != "$s4main28SomeSpecificDistributedActorC24helloThrowsTransportBoomSSyYaKFTE" else {
throw Boom("system")
}
return "remote(\(target.mangledName))" 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<Argument: SerializationRequirement>(_ argument: Argument) 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 }
}
@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(system: 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)
// TODO(distributed): remote - helloAsyncThrows: remote(_remote_impl_helloAsyncThrows())
// CHECK: remote - helloAsyncThrows: remote($s4main28SomeSpecificDistributedActorC16helloAsyncThrowsSSyYaKFTE)
// TODO(distributed): remote - helloAsync: remote()
// CHECK: remote - helloAsync: remote($s4main28SomeSpecificDistributedActorC10helloAsyncSSyYaKFTE)
// TODO(distributed): remote - helloThrows: remote(_remote_impl_helloThrows())
// CHECK: remote - helloThrows: remote($s4main28SomeSpecificDistributedActorC11helloThrowsSSyYaKFTE)
// TODO(distributed): remote - hello: remote(_remote_impl_hello())
// CHECK: remote - hello: remote($s4main28SomeSpecificDistributedActorC5helloSSyYaKFTE)
// TODO(distributed): remote - callTaskSelf: remote(_remote_impl_callTaskSelf())
// CHECK: remote - callTaskSelf: remote($s4main28SomeSpecificDistributedActorC12callTaskSelfSSyYaKFTE)
// TODO(distributed): remote - callDetachedSelf: remote(_remote_impl_callDetachedSelf())
// CHECK: remote - callDetachedSelf: remote($s4main28SomeSpecificDistributedActorC16callDetachedSelfSSyYaKFTE)
// 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)
}
}