Skip to content

Commit d5c9a4b

Browse files
committed
[Distributed] NFC: Add SerializationRequirement conformance to decoder in tests
1 parent 3a3f78b commit d5c9a4b

23 files changed

+132
-72
lines changed

test/Distributed/Inputs/FakeDistributedActorSystems.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
311311
return genericSubs
312312
}
313313

314-
public func decodeNextArgument<Argument>() throws -> Argument {
314+
public func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument {
315315
guard argumentIndex < arguments.count else {
316316
fatalError("Attempted to decode more arguments than stored! Index: \(argumentIndex), args: \(arguments)")
317317
}

test/Distributed/Inputs/dynamic_replacement_da_decl.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ struct ActorAddress: Hashable, Sendable, Codable {
3636

3737
final class FakeActorSystem: DistributedActorSystem {
3838
typealias ActorID = ActorAddress
39-
typealias InvocationDecoder = FakeInvocation
40-
typealias InvocationEncoder = FakeInvocation
39+
typealias InvocationDecoder = FakeInvocationDecoder
40+
typealias InvocationEncoder = FakeInvocationEncoder
4141
typealias SerializationRequirement = Codable
4242

4343
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
@@ -82,19 +82,23 @@ struct FakeDistributedSystemError: DistributedActorSystemError {
8282
let message: String
8383
}
8484

85-
class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
85+
// === Sending / encoding -------------------------------------------------
86+
struct FakeInvocationEncoder: DistributedTargetInvocationEncoder {
8687
typealias SerializationRequirement = Codable
8788

88-
func recordGenericSubstitution<T>(_ type: T.Type) throws {}
89-
func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
90-
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
91-
func recordErrorType<E: Error>(_ type: E.Type) throws {}
92-
func doneRecording() throws {}
89+
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
90+
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
91+
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
92+
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
93+
mutating func doneRecording() throws {}
94+
}
9395

94-
// === Receiving / decoding -------------------------------------------------
96+
// === Receiving / decoding -------------------------------------------------
97+
class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
98+
typealias SerializationRequirement = Codable
9599

96100
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
97-
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
101+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument { fatalError() }
98102
func decodeReturnType() throws -> Any.Type? { nil }
99103
func decodeErrorType() throws -> Any.Type? { nil }
100104
}

test/Distributed/Runtime/distributed_actor_decode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvoc
113113
// === Receiving / decoding -------------------------------------------------
114114

115115
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
116-
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
116+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument { fatalError() }
117117
func decodeReturnType() throws -> Any.Type? { nil }
118118
func decodeErrorType() throws -> Any.Type? { nil }
119119
}

test/Distributed/Runtime/distributed_actor_deinit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class FakeDistributedInvocation: DistributedTargetInvocationEncoder, Distributed
133133
func decodeGenericSubstitutions() throws -> [Any.Type] {
134134
[]
135135
}
136-
func decodeNextArgument<Argument>() throws -> Argument {
136+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument {
137137
fatalError()
138138
}
139139
func decodeReturnType() throws -> Any.Type? {

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_generic.swift

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ struct ActorAddress: Sendable, Hashable, Codable {
4545
//final class FakeActorSystem: DistributedActorSystem {
4646
struct FakeActorSystem: DistributedActorSystem {
4747
typealias ActorID = ActorAddress
48-
typealias InvocationDecoder = FakeInvocation
49-
typealias InvocationEncoder = FakeInvocation
48+
typealias InvocationDecoder = FakeInvocationDecoder
49+
typealias InvocationEncoder = FakeInvocationEncoder
5050
typealias SerializationRequirement = Codable
5151

5252
init() {}
@@ -105,16 +105,17 @@ struct FakeActorSystem: DistributedActorSystem {
105105

106106
}
107107

108-
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
108+
109+
struct FakeInvocationEncoder: DistributedTargetInvocationEncoder {
109110
typealias SerializationRequirement = Codable
110111

111-
var types: [Any.Type] = []
112+
var substitutions: [Any.Type] = []
112113
var arguments: [Any] = []
113114
var returnType: Any.Type? = nil
114115
var errorType: Any.Type? = nil
115116

116117
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {
117-
types.append(type)
118+
substitutions.append(type)
118119
}
119120
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {
120121
arguments.append(argument)
@@ -127,17 +128,45 @@ struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvo
127128
}
128129
mutating func doneRecording() throws {}
129130

130-
// === Receiving / decoding -------------------------------------------------
131+
// For testing only
132+
func makeDecoder() -> FakeInvocationDecoder {
133+
return .init(
134+
args: arguments,
135+
substitutions: substitutions,
136+
returnType: returnType,
137+
errorType: errorType
138+
)
139+
}
140+
}
141+
142+
143+
class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
144+
typealias SerializationRequirement = Codable
131145

146+
var arguments: [Any] = []
147+
var substitutions: [Any.Type] = []
148+
var returnType: Any.Type? = nil
149+
var errorType: Any.Type? = nil
150+
151+
init(
152+
args: [Any],
153+
substitutions: [Any.Type] = [],
154+
returnType: Any.Type? = nil,
155+
errorType: Any.Type? = nil
156+
) {
157+
self.arguments = args
158+
self.substitutions = substitutions
159+
self.returnType = returnType
160+
self.errorType = errorType
161+
}
162+
163+
// === Receiving / decoding -------------------------------------------------
132164
func decodeGenericSubstitutions() throws -> [Any.Type] {
133-
[]
165+
return substitutions
134166
}
135167

136168
var argumentIndex: Int = 0
137-
mutating func decodeNextArgument<Argument>(
138-
_ argumentType: Argument.Type,
139-
into pointer: UnsafeMutablePointer<Argument>
140-
) throws {
169+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument {
141170
guard argumentIndex < arguments.count else {
142171
fatalError("Attempted to decode more arguments than stored! Index: \(argumentIndex), args: \(arguments)")
143172
}
@@ -148,16 +177,18 @@ struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvo
148177
}
149178

150179
print(" > decode argument: \(argument)")
151-
pointer.pointee = argument
152180
argumentIndex += 1
181+
return argument
153182
}
154183

155-
func decodeErrorType() throws -> Any.Type? {
156-
self.errorType
184+
public func decodeErrorType() throws -> Any.Type? {
185+
print(" > decode return type: \(errorType.map { String(describing: $0) } ?? "nil")")
186+
return self.errorType
157187
}
158188

159-
func decodeReturnType() throws -> Any.Type? {
160-
self.returnType
189+
public func decodeReturnType() throws -> Any.Type? {
190+
print(" > decode return type: \(returnType.map { String(describing: $0) } ?? "nil")")
191+
return self.returnType
161192
}
162193
}
163194

test/Distributed/Runtime/distributed_actor_init_local.swift

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ var nextID: Int = 1
7070

7171
struct FakeActorSystem: DistributedActorSystem {
7272
public typealias ActorID = ActorAddress
73-
public typealias InvocationDecoder = FakeInvocation
74-
public typealias InvocationEncoder = FakeInvocation
73+
public typealias InvocationDecoder = FakeInvocationDecoder
74+
public typealias InvocationEncoder = FakeInvocationEncoder
7575
public typealias SerializationRequirement = Codable
7676

7777
init() {
@@ -121,24 +121,25 @@ struct FakeActorSystem: DistributedActorSystem {
121121

122122
}
123123

124-
public struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
125-
public typealias SerializationRequirement = Codable
124+
// === Sending / encoding -------------------------------------------------
125+
struct FakeInvocationEncoder: DistributedTargetInvocationEncoder {
126+
typealias SerializationRequirement = Codable
127+
128+
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
129+
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
130+
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
131+
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
132+
mutating func doneRecording() throws {}
133+
}
134+
135+
// === Receiving / decoding -------------------------------------------------
136+
class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
137+
typealias SerializationRequirement = Codable
126138

127-
public mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
128-
public mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
129-
public mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
130-
public mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
131-
public mutating func doneRecording() throws {}
132-
133-
// === Receiving / decoding -------------------------------------------------
134-
135-
public func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
136-
public mutating func decodeNextArgument<Argument>(
137-
_ argumentType: Argument.Type,
138-
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
139-
) throws { /* ... */ }
140-
public func decodeReturnType() throws -> Any.Type? { nil }
141-
public func decodeErrorType() throws -> Any.Type? { nil }
139+
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
140+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument { fatalError() }
141+
func decodeReturnType() throws -> Any.Type? { nil }
142+
func decodeErrorType() throws -> Any.Type? { nil }
142143
}
143144

144145
typealias DefaultDistributedActorSystem = FakeActorSystem

test/Distributed/Runtime/distributed_actor_isRemote.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
109109
typealias SerializationRequirement = Codable
110110

111111
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
112-
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
112+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument { fatalError() }
113113
func decodeReturnType() throws -> Any.Type? { nil }
114114
func decodeErrorType() throws -> Any.Type? { nil }
115115
}

test/Distributed/Runtime/distributed_actor_local.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
113113
typealias SerializationRequirement = Codable
114114

115115
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
116-
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
116+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument { fatalError() }
117117
func decodeReturnType() throws -> Any.Type? { nil }
118118
func decodeErrorType() throws -> Any.Type? { nil }
119119
}

test/Distributed/Runtime/distributed_actor_remoteCall.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
227227
}
228228

229229
var argumentIndex: Int = 0
230-
func decodeNextArgument<Argument>() throws -> Argument {
230+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument {
231231
guard argumentIndex < arguments.count else {
232232
fatalError("Attempted to decode more arguments than stored! Index: \(argumentIndex), args: \(arguments)")
233233
}

test/Distributed/Runtime/distributed_actor_remote_fieldsDontCrashDeinit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
126126
typealias SerializationRequirement = Codable
127127

128128
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
129-
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
129+
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument { fatalError() }
130130
func decodeReturnType() throws -> Any.Type? { nil }
131131
func decodeErrorType() throws -> Any.Type? { nil }
132132
}

0 commit comments

Comments
 (0)