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-codesign %t/a.out
5
+ // RUN: %target-run %t/a.out | %FileCheck %s --color
6
+
7
+ // REQUIRES: executable_test
8
+ // REQUIRES: concurrency
9
+ // REQUIRES: distributed
10
+
11
+ // rdar://76038845
12
+ // UNSUPPORTED: use_os_stdlib
13
+ // UNSUPPORTED: back_deployment_runtime
14
+
15
+ import Foundation
16
+ import Distributed
17
+
18
+ final class LocalActorSystem : DistributedActorSystem {
19
+ typealias ActorID = LocalTestingActorID
20
+ typealias ResultHandler = LocalTestingInvocationResultHandler
21
+ typealias InvocationEncoder = LocalTestingInvocationEncoder
22
+ typealias InvocationDecoder = LocalTestingInvocationDecoder
23
+ typealias SerializationRequirement = any Codable
24
+
25
+ func makeInvocationEncoder( ) -> InvocationEncoder { LocalTestingDistributedActorSystem ( ) . makeInvocationEncoder ( ) }
26
+
27
+ func resolve< Act> ( id: ActorID , as actorType: Act . Type ) throws -> Act ? where Act: DistributedActor {
28
+ nil
29
+ }
30
+
31
+ func actorReady< Act> ( _ actor : Act ) where Act: DistributedActor , Act. ID == ActorID {
32
+ }
33
+
34
+ func resignID( _ id: ActorID ) { }
35
+
36
+ func assignID< Act> ( _ actorType: Act . Type ) -> ActorID
37
+ where Act: DistributedActor {
38
+ . init( id: " 42 " )
39
+ }
40
+
41
+ func remoteCall< Act, Err, Res> ( on actor : Act ,
42
+ target: RemoteCallTarget ,
43
+ invocation: inout InvocationEncoder ,
44
+ throwing: Err . Type ,
45
+ returning: Res . Type ) async throws -> Res
46
+ where Act: DistributedActor ,
47
+ Act. ID == ActorID ,
48
+ Err: Error ,
49
+ Res: Codable {
50
+ let decoder = JSONDecoder ( )
51
+ return try ! decoder. decode ( Res . self, from: await fetchData ( ) )
52
+ }
53
+
54
+ func remoteCallVoid< Act, Err> ( on actor : Act ,
55
+ target: RemoteCallTarget ,
56
+ invocation: inout InvocationEncoder ,
57
+ throwing errorType: Err . Type ) async throws
58
+ where Act: DistributedActor ,
59
+ Act. ID == ActorID ,
60
+ Err: Error {
61
+ fatalError ( " not implemented: \( #function) " )
62
+ }
63
+
64
+ func fetchData( ) async -> Data {
65
+ " 42 " . data ( using: . ascii) !
66
+ }
67
+ }
68
+
69
+ distributed actor NotCodableDA < ActorSystem>
70
+ where ActorSystem: DistributedActorSystem < any Codable > {
71
+
72
+ init ( actorSystem: ActorSystem ) async {
73
+ self . actorSystem = actorSystem
74
+
75
+ print ( try ! await self . request ( ) )
76
+ }
77
+
78
+ func request( ) async throws -> Int {
79
+ let target = RemoteCallTarget ( " test.request " )
80
+ var encoder = actorSystem. makeInvocationEncoder ( )
81
+ return try await actorSystem. remoteCall ( on: self ,
82
+ target: target,
83
+ invocation: & encoder,
84
+ throwing: Error . self,
85
+ returning: Int . self)
86
+ }
87
+ }
88
+
89
+ distributed actor CodableDA < ActorSystem> : Codable
90
+ where ActorSystem: DistributedActorSystem < any Codable > ,
91
+ ActorSystem. ActorID: Codable {
92
+
93
+ init ( actorSystem: ActorSystem ) async {
94
+ self . actorSystem = actorSystem
95
+
96
+ print ( try ! await self . request ( ) )
97
+ }
98
+
99
+ func request( ) async throws -> Int {
100
+ let target = RemoteCallTarget ( " test.request " )
101
+ var encoder = actorSystem. makeInvocationEncoder ( )
102
+ return try await actorSystem. remoteCall ( on: self ,
103
+ target: target,
104
+ invocation: & encoder,
105
+ throwing: Error . self,
106
+ returning: Int . self)
107
+ }
108
+ }
109
+
110
+ distributed actor CodableIDDA < ActorSystem>
111
+ where ActorSystem: DistributedActorSystem < any Codable > ,
112
+ ActorSystem. ActorID: Codable {
113
+
114
+ init ( actorSystem: ActorSystem ) async {
115
+ self . actorSystem = actorSystem
116
+
117
+ print ( try ! await self . request ( ) )
118
+ }
119
+
120
+ func request( ) async throws -> Int {
121
+ let target = RemoteCallTarget ( " test.request " )
122
+ var encoder = actorSystem. makeInvocationEncoder ( )
123
+ return try await actorSystem. remoteCall ( on: self ,
124
+ target: target,
125
+ invocation: & encoder,
126
+ throwing: Error . self,
127
+ returning: Int . self)
128
+ }
129
+ }
130
+
131
+ @main struct Main {
132
+ static func main( ) async throws {
133
+ if #available( SwiftStdlib 5 . 9 , * ) {
134
+ let system = LocalActorSystem ( )
135
+ let ncda = await NotCodableDA ( actorSystem: system)
136
+ let cidda = await CodableIDDA ( actorSystem: system)
137
+ let cda = await CodableDA ( actorSystem: system)
138
+
139
+ try await ncda. whenLocal {
140
+ let got = try await $0. request ( )
141
+
142
+ // CHECK: got = 42
143
+ print ( " got = \( got) " )
144
+ }
145
+
146
+ try await cidda. whenLocal {
147
+ let got = try await $0. request ( )
148
+
149
+ // CHECK: got = 42
150
+ print ( " got = \( got) " )
151
+ }
152
+
153
+ let c : any ( DistributedActor & Codable ) = cda
154
+ try await cda. whenLocal {
155
+ let got = try await $0. request ( )
156
+
157
+ // CHECK: got = 42
158
+ print ( " got = \( got) " )
159
+ }
160
+
161
+ // CHECK: OK
162
+ print ( " OK " )
163
+ }
164
+ }
165
+ }
0 commit comments