Skip to content

Commit 6a0164d

Browse files
committed
[Distributed] Reuse fake actor systems more in tests
1 parent 7f427c9 commit 6a0164d

36 files changed

+239
-1347
lines changed

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ bool swift::checkDistributedActorSystemAdHocProtocolRequirements(
134134
"func remoteCall<Act, Err, Res>(\n"
135135
" on actor: Act,\n"
136136
" target: RemoteCallTarget,\n"
137-
" invocationDecoder: inout InvocationDecoder,\n"
137+
" invocation: inout InvocationEncoder,\n"
138138
" throwing: Err.Type,\n"
139139
" returning: Res.Type\n"
140140
") async throws -> Res\n"
@@ -157,7 +157,7 @@ bool swift::checkDistributedActorSystemAdHocProtocolRequirements(
157157
"func remoteCallVoid<Act, Err>(\n"
158158
" on actor: Act,\n"
159159
" target: RemoteCallTarget,\n"
160-
" invocationDecoder: inout InvocationDecoder,\n"
160+
" invocation: inout InvocationEncoder,\n"
161161
" throwing: Err.Type\n"
162162
") async throws\n"
163163
" where Act: DistributedActor,\n"

lib/sourcekitd.framework/Headers

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/sourcekitd.framework/Resources

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/sourcekitd.framework/Versions/Current

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/sourcekitd.framework/sourcekitd

Lines changed: 0 additions & 1 deletion
This file was deleted.

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public protocol DistributedActorSystem: Sendable {
122122
// func remoteCall<Act, Err, Res>(
123123
// on actor: Act,
124124
// target: RemoteCallTarget,
125-
// invocationDecoder: inout InvocationDecoder,
125+
// invocation: inout InvocationEncoder,
126126
// throwing: Err.Type,
127127
// returning: Res.Type
128128
// ) async throws -> Res
@@ -157,7 +157,7 @@ extension DistributedActorSystem {
157157
invocationDecoder: inout InvocationDecoder,
158158
handler: ResultHandler
159159
) async throws where Act: DistributedActor,
160-
Act.ID == ActorID,
160+
// Act.ID == ActorID, // FIXME(distributed): can we bring this back?
161161
ResultHandler: DistributedTargetInvocationResultHandler {
162162
// NOTE: this implementation is not the most efficient, nor final, version of this func
163163
// we end up demangling the name multiple times, perform more heap allocations than

test/Distributed/Inputs/FakeDistributedActorSystems.swift

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
169169
}
170170

171171
private var remoteCallResult: Any? = nil
172+
private var remoteCallError: Error? = nil
172173

173174
public func remoteCall<Act, Err, Res>(
174175
on actor: Act,
@@ -191,16 +192,30 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
191192
fatalError("Attempted to call mock 'roundtrip' on unknown actor: \(actor.id), known: \(active.id)")
192193
}
193194

194-
let resultHandler = FakeRoundtripResultHandler { self.remoteCallResult = $0 }
195+
let resultHandler = FakeRoundtripResultHandler { value in
196+
self.remoteCallResult = value
197+
self.remoteCallError = nil
198+
} onError: { error in
199+
self.remoteCallResult = nil
200+
self.remoteCallError = error
201+
}
195202
try await executeDistributedTarget(
196203
on: active,
197204
mangledTargetName: target.mangledName,
198205
invocationDecoder: &invocation,
199206
handler: resultHandler
200207
)
201208

202-
print(" << remoteCall return: \(remoteCallResult!)")
203-
return remoteCallResult! as! Res
209+
switch (remoteCallResult, remoteCallError) {
210+
case (.some(let value), nil):
211+
print(" << remoteCall return: \(value)")
212+
return remoteCallResult! as! Res
213+
case (nil, .some(let error)):
214+
print(" << remoteCall throw: \(error)")
215+
throw error
216+
default:
217+
fatalError("No reply!")
218+
}
204219
}
205220
return try await _openExistential(targetActor, do: doIt)
206221
}
@@ -209,13 +224,46 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
209224
on actor: Act,
210225
target: RemoteCallTarget,
211226
invocation: inout InvocationEncoder,
212-
throwing: Err.Type
227+
throwing errorType: Err.Type
213228
) async throws
214229
where Act: DistributedActor,
215230
Act.ID == ActorID,
216231
Err: Error {
217-
print("remoteCallVoid: on:\(actor), target:\(target), invocation:\(invocation), throwing:\(throwing)")
218-
return ()
232+
print(" >> remoteCallVoid: on:\(actor)), target:\(target), invocation:\(invocation), throwing:\(String(reflecting: errorType))")
233+
guard let targetActor = activeActors[actor.id] else {
234+
fatalError("Attempted to call mock 'roundtrip' on: \(actor.id) without active actor")
235+
}
236+
237+
func doIt<A: DistributedActor>(active: A) async throws {
238+
guard (actor.id) == active.id as! ActorID else {
239+
fatalError("Attempted to call mock 'roundtrip' on unknown actor: \(actor.id), known: \(active.id)")
240+
}
241+
242+
let resultHandler = FakeRoundtripResultHandler { value in
243+
self.remoteCallResult = value
244+
self.remoteCallError = nil
245+
} onError: { error in
246+
self.remoteCallResult = nil
247+
self.remoteCallError = error
248+
}
249+
try await executeDistributedTarget(
250+
on: active,
251+
mangledTargetName: target.mangledName,
252+
invocationDecoder: &invocation,
253+
handler: resultHandler
254+
)
255+
256+
switch (remoteCallResult, remoteCallError) {
257+
case (.some, nil):
258+
return
259+
case (nil, .some(let error)):
260+
print(" << remoteCall throw: \(error)")
261+
throw error
262+
default:
263+
fatalError("No reply!")
264+
}
265+
}
266+
try await _openExistential(targetActor, do: doIt)
219267
}
220268

221269
}
@@ -290,19 +338,22 @@ public struct FakeRoundtripInvocation: DistributedTargetInvocationEncoder, Distr
290338
public struct FakeRoundtripResultHandler: DistributedTargetInvocationResultHandler {
291339
public typealias SerializationRequirement = Codable
292340

293-
let store: (Any) -> Void
294-
init(_ store: @escaping (Any) -> Void) {
295-
self.store = store
341+
let storeReturn: (any Any) -> Void
342+
let storeError: (any Error) -> Void
343+
init(_ storeReturn: @escaping (Any) -> Void, onError storeError: @escaping (Error) -> Void) {
344+
self.storeReturn = storeReturn
345+
self.storeError = storeError
296346
}
297347

348+
// FIXME(distributed): can we return void here?
298349
public func onReturn<Res>(value: Res) async throws {
299350
print(" << onReturn: \(value)")
300-
store(value)
351+
storeReturn(value)
301352
}
302353

303354
public func onThrow<Err: Error>(error: Err) async throws {
304355
print(" << onThrow: \(error)")
305-
store(error)
356+
storeError(error)
306357
}
307358
}
308359

test/Distributed/Inputs/dynamic_replacement_da_decl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ final class FakeActorSystem: DistributedActorSystem {
6767
func remoteCall<Act, Err, Res>(
6868
on actor: Act,
6969
target: RemoteCallTarget,
70-
invocationDecoder: InvocationDecoder,
70+
invocation: InvocationDecoder,
7171
throwing: Err.Type,
7272
returning: Res.Type
7373
) async throws -> Res

test/Distributed/Runtime/distributed_actor_decode.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ final class FakeActorSystem: DistributedActorSystem {
7676
func remoteCall<Act, Err, Res>(
7777
on actor: Act,
7878
target: RemoteCallTarget,
79-
invocationDecoder: inout InvocationDecoder,
79+
invocation invocationEncoder: inout InvocationEncoder,
8080
throwing: Err.Type,
8181
returning: Res.Type
8282
) async throws -> Res
@@ -90,7 +90,7 @@ final class FakeActorSystem: DistributedActorSystem {
9090
func remoteCallVoid<Act, Err>(
9191
on actor: Act,
9292
target: RemoteCallTarget,
93-
invocationDecoder: inout InvocationDecoder,
93+
invocation invocationEncoder: inout InvocationEncoder,
9494
throwing: Err.Type
9595
) async throws
9696
where Act: DistributedActor,
@@ -247,7 +247,7 @@ class TestDecoder: Decoder {
247247
// ==== Execute ----------------------------------------------------------------
248248

249249
func test() {
250-
let system = FakeActorSystem()
250+
let system = DefaultDistributedActorSystem()
251251

252252
// CHECK: assign type:DA, address:ActorAddress(address: "xxx")
253253
// CHECK: ready actor:DA(ActorAddress(address: "xxx"))

test/Distributed/Runtime/distributed_actor_deinit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ final class FakeActorSystem: @unchecked Sendable, DistributedActorSystem {
9595
func remoteCall<Act, Err, Res>(
9696
on actor: Act,
9797
target: RemoteCallTarget,
98-
invocationDecoder: inout InvocationDecoder,
98+
invocation invocationEncoder: inout InvocationEncoder,
9999
throwing: Err.Type,
100100
returning: Res.Type
101101
) async throws -> Res
@@ -109,7 +109,7 @@ final class FakeActorSystem: @unchecked Sendable, DistributedActorSystem {
109109
func remoteCallVoid<Act, Err>(
110110
on actor: Act,
111111
target: RemoteCallTarget,
112-
invocationDecoder: inout InvocationDecoder,
112+
invocation invocationEncoder: inout InvocationEncoder,
113113
throwing: Err.Type
114114
) async throws
115115
where Act: DistributedActor,
@@ -157,7 +157,7 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
157157
// ==== Execute ----------------------------------------------------------------
158158

159159
func test() {
160-
let system = FakeActorSystem()
160+
let system = DefaultDistributedActorSystem()
161161

162162
// no lifecycle things make sense for a normal actor, double check we didn't emit them
163163
print("before A")

test/Distributed/Runtime/distributed_actor_dynamic_remote_func.swift

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)