Skip to content

Commit c3cafcc

Browse files
committed
[Distributed] NFC: Split FakeInvocation into Encoder and Decoder and adjust tests
1 parent 6c4b9c8 commit c3cafcc

26 files changed

+357
-299
lines changed

test/Distributed/Inputs/FakeDistributedActorSystems.swift

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public struct ActorAddress: Hashable, Sendable, Codable {
3838

3939
public struct FakeActorSystem: DistributedActorSystem {
4040
public typealias ActorID = ActorAddress
41-
public typealias InvocationDecoder = FakeInvocation
42-
public typealias InvocationEncoder = FakeInvocation
41+
public typealias InvocationDecoder = FakeInvocationDecoder
42+
public typealias InvocationEncoder = FakeInvocationEncoder
4343
public typealias SerializationRequirement = Codable
4444

4545
// just so that the struct does not become "trivial"
@@ -103,30 +103,13 @@ public struct FakeActorSystem: DistributedActorSystem {
103103
}
104104
}
105105

106-
public class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
107-
public typealias SerializationRequirement = Codable
108-
109-
public func recordGenericSubstitution<T>(_ type: T.Type) throws {}
110-
public func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
111-
public func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
112-
public func recordErrorType<E: Error>(_ type: E.Type) throws {}
113-
public func doneRecording() throws {}
114-
115-
// === Receiving / decoding -------------------------------------------------
116-
117-
public func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
118-
public func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
119-
public func decodeReturnType() throws -> Any.Type? { nil }
120-
public func decodeErrorType() throws -> Any.Type? { nil }
121-
}
122-
123106
// ==== Fake Roundtrip Transport -----------------------------------------------
124107

125108
// TODO: not thread safe...
126109
public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked Sendable {
127110
public typealias ActorID = ActorAddress
128-
public typealias InvocationEncoder = FakeRoundtripInvocation
129-
public typealias InvocationDecoder = FakeRoundtripInvocation
111+
public typealias InvocationEncoder = FakeInvocationEncoder
112+
public typealias InvocationDecoder = FakeInvocationDecoder
130113
public typealias SerializationRequirement = Codable
131114

132115
var activeActors: [ActorID: any DistributedActor] = [:]
@@ -157,7 +140,7 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
157140
print("X resign id: \(id)")
158141
}
159142

160-
public func makeInvocationEncoder() -> FakeRoundtripInvocation {
143+
public func makeInvocationEncoder() -> InvocationEncoder {
161144
.init()
162145
}
163146

@@ -195,7 +178,7 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
195178
try await executeDistributedTarget(
196179
on: active,
197180
mangledTargetName: target.mangledName,
198-
invocationDecoder: &invocation,
181+
invocationDecoder: invocation.makeDecoder(),
199182
handler: resultHandler
200183
)
201184

@@ -242,7 +225,7 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
242225
try await executeDistributedTarget(
243226
on: active,
244227
mangledTargetName: target.mangledName,
245-
invocationDecoder: &invocation,
228+
invocationDecoder: invocation.makeDecoder(),
246229
handler: resultHandler
247230
)
248231

@@ -261,7 +244,7 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
261244

262245
}
263246

264-
public struct FakeRoundtripInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
247+
public struct FakeInvocationEncoder : DistributedTargetInvocationEncoder {
265248
public typealias SerializationRequirement = Codable
266249

267250
var genericSubs: [Any.Type] = []
@@ -290,18 +273,45 @@ public struct FakeRoundtripInvocation: DistributedTargetInvocationEncoder, Distr
290273
print(" > done recording")
291274
}
292275

293-
// === decoding --------------------------------------------------------------
276+
public func makeDecoder() -> FakeInvocationDecoder {
277+
return .init(
278+
args: arguments,
279+
substitutions: genericSubs,
280+
returnType: returnType,
281+
errorType: errorType
282+
)
283+
}
284+
}
285+
286+
// === decoding --------------------------------------------------------------
287+
public class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
288+
public typealias SerializationRequirement = Codable
289+
290+
var genericSubs: [Any.Type] = []
291+
var arguments: [Any] = []
292+
var returnType: Any.Type? = nil
293+
var errorType: Any.Type? = nil
294+
295+
var argumentIndex: Int = 0
296+
297+
fileprivate init(
298+
args: [Any],
299+
substitutions: [Any.Type] = [],
300+
returnType: Any.Type? = nil,
301+
errorType: Any.Type? = nil
302+
) {
303+
self.arguments = args
304+
self.genericSubs = substitutions
305+
self.returnType = returnType
306+
self.errorType = errorType
307+
}
294308

295309
public func decodeGenericSubstitutions() throws -> [Any.Type] {
296310
print(" > decode generic subs: \(genericSubs)")
297311
return genericSubs
298312
}
299313

300-
var argumentIndex: Int = 0
301-
public mutating func decodeNextArgument<Argument>(
302-
_ argumentType: Argument.Type,
303-
into pointer: UnsafeMutablePointer<Argument>
304-
) throws {
314+
public func decodeNextArgument<Argument>() throws -> Argument {
305315
guard argumentIndex < arguments.count else {
306316
fatalError("Attempted to decode more arguments than stored! Index: \(argumentIndex), args: \(arguments)")
307317
}
@@ -312,8 +322,8 @@ public struct FakeRoundtripInvocation: DistributedTargetInvocationEncoder, Distr
312322
}
313323

314324
print(" > decode argument: \(argument)")
315-
pointer.pointee = argument
316325
argumentIndex += 1
326+
return argument
317327
}
318328

319329
public func decodeErrorType() throws -> Any.Type? {

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_echo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func test() async throws {
3838
let ref = try Greeter.resolve(id: local.id, using: system)
3939

4040
let reply = try await ref.echo(name: "Caplin")
41-
// CHECK: >> remoteCall: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC4echo4nameS2S_tFTE"), invocation:FakeRoundtripInvocation(genericSubs: [], arguments: ["Caplin"], returnType: Optional(Swift.String), errorType: nil, argumentIndex: 0), throwing:Swift.Never, returning:Swift.String
41+
// CHECK: >> remoteCall: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC4echo4nameS2S_tFTE"), invocation:FakeInvocationEncoder(genericSubs: [], arguments: ["Caplin"], returnType: Optional(Swift.String), errorType: nil), throwing:Swift.Never, returning:Swift.String
4242

4343
// CHECK: << remoteCall return: Echo: Caplin
4444
print("reply: \(reply)")

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_empty.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func test() async throws {
3737
let ref = try Greeter.resolve(id: local.id, using: system)
3838

3939
try await ref.empty()
40-
// CHECK: >> remoteCallVoid: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC5emptyyyFTE"), invocation:FakeRoundtripInvocation(genericSubs: [], arguments: [], returnType: nil, errorType: nil, argumentIndex: 0), throwing:Swift.Never
40+
// CHECK: >> remoteCallVoid: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC5emptyyyFTE"), invocation:FakeInvocationEncoder(genericSubs: [], arguments: [], returnType: nil, errorType: nil), throwing:Swift.Never
4141
// CHECK: << onReturn: ()
4242
}
4343

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_hello.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func test() async throws {
3838
let ref = try Greeter.resolve(id: local.id, using: system)
3939

4040
let response = try await ref.hello()
41-
// CHECK: >> remoteCall: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC5helloSSyFTE"), invocation:FakeRoundtripInvocation(genericSubs: [], arguments: [], returnType: Optional(Swift.String), errorType: nil, argumentIndex: 0), throwing:Swift.Never, returning:Swift.String
41+
// CHECK: >> remoteCall: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC5helloSSyFTE"), invocation:FakeInvocationEncoder(genericSubs: [], arguments: [], returnType: Optional(Swift.String), errorType: nil), throwing:Swift.Never, returning:Swift.String
4242

4343
print("response: \(response)")
4444
// CHECK: response: Hello, World!

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_take.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func test() async throws {
3838
let ref = try Greeter.resolve(id: local.id, using: system)
3939

4040
try await ref.take(name: "Caplin")
41-
// CHECK: >> remoteCallVoid: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC4take4nameySS_tFTE"), invocation:FakeRoundtripInvocation(genericSubs: [], arguments: ["Caplin"], returnType: nil, errorType: nil, argumentIndex: 0), throwing:Swift.Never
41+
// CHECK: >> remoteCallVoid: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC4take4nameySS_tFTE"), invocation:FakeInvocationEncoder(genericSubs: [], arguments: ["Caplin"], returnType: nil, errorType: nil), throwing:Swift.Never
4242
// CHECK: take: Caplin
4343

4444
}

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_takeThrowReturn.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func test() async throws {
4141

4242
do {
4343
let value = try await ref.takeThrowReturn(name: "Example")
44-
// CHECK: >> remoteCall: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC15takeThrowReturn4nameS2S_tYaKFTE"), invocation:FakeRoundtripInvocation(genericSubs: [], arguments: ["Example"], returnType: Optional(Swift.String), errorType: Optional(Swift.Error.Protocol), argumentIndex: 0), throwing:Swift.Error.Protocol, returning:Swift.String
44+
// CHECK: >> remoteCall: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC15takeThrowReturn4nameS2S_tYaKFTE"), invocation:FakeInvocationEncoder(genericSubs: [], arguments: ["Example"], returnType: Optional(Swift.String), errorType: Optional(Swift.Error.Protocol)), throwing:Swift.Error.Protocol, returning:Swift.String
4545

4646
print("did not throw")
4747
// CHECK-NOT: did not throw

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_take_two.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func test() async throws {
4444
let ref = try Greeter.resolve(id: local.id, using: system)
4545

4646
try await ref.take(name: "Caplin", int: 1337)
47-
// CHECK: >> remoteCallVoid: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC4take4name3intySS_SitFTE"), invocation:FakeRoundtripInvocation(genericSubs: [], arguments: ["Caplin", 1337], returnType: nil, errorType: nil, argumentIndex: 0), throwing:Swift.Never
47+
// CHECK: >> remoteCallVoid: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC4take4name3intySS_SitFTE"), invocation:FakeInvocationEncoder(genericSubs: [], arguments: ["Caplin", 1337], returnType: nil, errorType: nil), throwing:Swift.Never
4848

4949
// try await ref.take(name: "Caplin", int: 1337, clazz: .init()) // FIXME(distributed): crashes
5050

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_throw.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func test() async throws {
4141

4242
do {
4343
try await ref.maybeThrows()
44-
// CHECK: >> remoteCallVoid: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC11maybeThrowsyyKFTE"), invocation:FakeRoundtripInvocation(genericSubs: [], arguments: [], returnType: nil, errorType: Optional(Swift.Error.Protocol), argumentIndex: 0), throwing:Swift.Error.Protocol
44+
// CHECK: >> remoteCallVoid: on:main.Greeter), target:RemoteCallTarget(_mangledName: "$s4main7GreeterC11maybeThrowsyyKFTE"), invocation:FakeInvocationEncoder(genericSubs: [], arguments: [], returnType: nil, errorType: Optional(Swift.Error.Protocol)), throwing:Swift.Error.Protocol
4545

4646
print("did not throw")
4747
// CHECK-NOT: did not throw

test/Distributed/Runtime/distributed_actor_isRemote.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ struct ActorAddress: Sendable, Hashable, Codable {
3535

3636
struct FakeActorSystem: DistributedActorSystem {
3737
typealias ActorID = ActorAddress
38-
typealias InvocationDecoder = FakeInvocation
39-
typealias InvocationEncoder = FakeInvocation
38+
typealias InvocationDecoder = FakeInvocationDecoder
39+
typealias InvocationEncoder = FakeInvocationEncoder
4040
typealias SerializationRequirement = Codable
4141

4242
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
@@ -93,16 +93,20 @@ struct FakeActorSystem: DistributedActorSystem {
9393
}
9494
}
9595

96-
class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
96+
// === Sending / encoding -------------------------------------------------
97+
struct FakeInvocationEncoder: DistributedTargetInvocationEncoder {
9798
typealias SerializationRequirement = Codable
9899

99-
func recordGenericSubstitution<T>(_ type: T.Type) throws {}
100-
func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
101-
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
102-
func recordErrorType<E: Error>(_ type: E.Type) throws {}
103-
func doneRecording() throws {}
100+
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
101+
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
102+
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
103+
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
104+
mutating func doneRecording() throws {}
105+
}
104106

105-
// === Receiving / decoding -------------------------------------------------
107+
// === Receiving / decoding -------------------------------------------------
108+
class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
109+
typealias SerializationRequirement = Codable
106110

107111
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
108112
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }

test/Distributed/Runtime/distributed_actor_local.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ struct ActorAddress: Sendable, Hashable, Codable {
4141

4242
struct FakeActorSystem: DistributedActorSystem {
4343
typealias ActorID = ActorAddress
44-
typealias InvocationDecoder = FakeInvocation
45-
typealias InvocationEncoder = FakeInvocation
44+
typealias InvocationDecoder = FakeInvocationDecoder
45+
typealias InvocationEncoder = FakeInvocationEncoder
4646
typealias SerializationRequirement = Codable
4747

4848
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
@@ -97,16 +97,20 @@ struct FakeActorSystem: DistributedActorSystem {
9797

9898
}
9999

100-
class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
100+
// === Sending / encoding -------------------------------------------------
101+
struct FakeInvocationEncoder: DistributedTargetInvocationEncoder {
101102
typealias SerializationRequirement = Codable
102103

103-
func recordGenericSubstitution<T>(_ type: T.Type) throws {}
104-
func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
105-
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
106-
func recordErrorType<E: Error>(_ type: E.Type) throws {}
107-
func doneRecording() throws {}
104+
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
105+
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
106+
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
107+
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
108+
mutating func doneRecording() throws {}
109+
}
108110

109-
// === Receiving / decoding -------------------------------------------------
111+
// === Receiving / decoding -------------------------------------------------
112+
class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
113+
typealias SerializationRequirement = Codable
110114

111115
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
112116
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }

0 commit comments

Comments
 (0)