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