1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
import Distributed
distributed actor DA: CustomStringConvertible {
typealias ActorSystem = FakeActorSystem
nonisolated var description: String {
"DA(\(self.id))"
}
}
// ==== Fake Transport ---------------------------------------------------------
struct ActorAddress: Hashable, Sendable, Codable {
let address: String
init(parse address : String) {
self.address = address
}
// Explicit implementations to make our TestEncoder/Decoder simpler
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.address = try container.decode(String.self)
print("decode ActorAddress -> \(self)")
}
func encode(to encoder: Encoder) throws {
print("encode \(self)")
var container = encoder.singleValueContainer()
try container.encode(self.address)
}
}
final class FakeActorSystem: DistributedActorSystem {
typealias ActorID = ActorAddress
typealias InvocationDecoder = FakeInvocation
typealias InvocationEncoder = FakeInvocation
typealias SerializationRequirement = Codable
typealias ResultHandler = FakeResultHandler
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
where Act: DistributedActor,
Act.ID == ActorID {
print("resolve type:\(actorType), address:\(id)")
return nil
}
func assignID<Act>(_ actorType: Act.Type) -> ActorID
where Act: DistributedActor, Act.ID == ActorID {
let address = ActorAddress(parse: "xxx")
print("assign type:\(actorType), address:\(address)")
return address
}
func actorReady<Act>(_ actor: Act)
where Act: DistributedActor,
Act.ID == ActorID {
print("ready actor:\(actor), address:\(actor.id)")
}
func resignID(_ id: ActorID) {
print("resign address:\(id)")
}
func makeInvocationEncoder() -> InvocationEncoder {
.init()
}
func remoteCall<Act, Err, Res>(
on actor: Act,
target: RemoteCallTarget,
invocation invocationEncoder: inout InvocationEncoder,
throwing: Err.Type,
returning: Res.Type
) async throws -> Res
where Act: DistributedActor,
Act.ID == ActorID,
Err: Error,
Res: SerializationRequirement {
fatalError("not implemented: \(#function)")
}
func remoteCallVoid<Act, Err>(
on actor: Act,
target: RemoteCallTarget,
invocation invocationEncoder: inout InvocationEncoder,
throwing: Err.Type
) async throws
where Act: DistributedActor,
Act.ID == ActorID,
Err: Error {
fatalError("not implemented: \(#function)")
}
}
final class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
typealias SerializationRequirement = Codable
func recordGenericSubstitution<T>(_ type: T.Type) throws {}
func recordArgument<Value: SerializationRequirement>(_ argument: RemoteCallArgument<Value>) throws {}
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
func recordErrorType<E: Error>(_ type: E.Type) throws {}
func doneRecording() throws {}
// === Receiving / decoding -------------------------------------------------
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument { fatalError() }
func decodeReturnType() throws -> Any.Type? { nil }
func decodeErrorType() throws -> Any.Type? { nil }
}
@available(SwiftStdlib 5.5, *)
typealias DefaultDistributedActorSystem = FakeActorSystem
// ==== Test Coding ------------------------------------------------------------
class TestEncoder: Encoder {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
var data: String? = nil
init(system: FakeActorSystem) {
self.codingPath = []
self.userInfo = [.actorSystemKey: system]
}
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> {
fatalError("Not implemented: \(#function)")
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
fatalError("Not implemented: \(#function)")
}
func singleValueContainer() -> SingleValueEncodingContainer {
TestSingleValueEncodingContainer(parent: self)
}
class TestSingleValueEncodingContainer: SingleValueEncodingContainer {
let parent: TestEncoder
init(parent: TestEncoder) {
self.parent = parent
}
var codingPath: [CodingKey] = []
func encodeNil() throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: Bool) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: String) throws {
}
func encode(_ value: Double) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: Float) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: Int) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: Int8) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: Int16) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: Int32) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: Int64) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: UInt) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: UInt8) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: UInt16) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: UInt32) throws { fatalError("Not implemented: \(#function)") }
func encode(_ value: UInt64) throws { fatalError("Not implemented: \(#function)") }
func encode<T: Encodable>(_ value: T) throws {
print("encode: \(value)")
if let address = value as? ActorAddress {
self.parent.data = address.address
}
}
}
func encode<Act: DistributedActor>(_ actor: Act) throws -> String where Act.ID: Codable {
try actor.encode(to: self)
return self.data!
}
}
@available(SwiftStdlib 5.5, *)
public struct FakeResultHandler: DistributedTargetInvocationResultHandler {
public typealias SerializationRequirement = Codable
public func onReturn<Success: SerializationRequirement>(value: Success) async throws {
fatalError("Not implemented: \(#function)")
}
public func onReturnVoid() async throws {
fatalError("Not implemented: \(#function)")
}
public func onThrow<Err: Error>(error: Err) async throws {
fatalError("Not implemented: \(#function)")
}
}
class TestDecoder: Decoder {
let encoder: TestEncoder
let data: String
init(encoder: TestEncoder, system: FakeActorSystem, data: String) {
self.encoder = encoder
self.userInfo = [.actorSystemKey: system]
self.data = data
}
var codingPath: [CodingKey] = []
var userInfo: [CodingUserInfoKey : Any]
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
fatalError("Not implemented: \(#function)")
}
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
fatalError("Not implemented: \(#function)")
}
func singleValueContainer() throws -> SingleValueDecodingContainer {
TestSingleValueDecodingContainer(parent: self)
}
class TestSingleValueDecodingContainer: SingleValueDecodingContainer {
let parent: TestDecoder
init(parent: TestDecoder) {
self.parent = parent
}
var codingPath: [CodingKey] = []
func decodeNil() -> Bool { fatalError("Not implemented: \(#function)") }
func decode(_ type: Bool.Type) throws -> Bool { fatalError("Not implemented: \(#function)") }
func decode(_ type: String.Type) throws -> String {
print("decode String -> \(self.parent.data)")
return self.parent.data
}
func decode(_ type: Double.Type) throws -> Double { fatalError("Not implemented: \(#function)") }
func decode(_ type: Float.Type) throws -> Float { fatalError("Not implemented: \(#function)") }
func decode(_ type: Int.Type) throws -> Int { fatalError("Not implemented: \(#function)") }
func decode(_ type: Int8.Type) throws -> Int8 { fatalError("Not implemented: \(#function)") }
func decode(_ type: Int16.Type) throws -> Int16 { fatalError("Not implemented: \(#function)") }
func decode(_ type: Int32.Type) throws -> Int32 { fatalError("Not implemented: \(#function)") }
func decode(_ type: Int64.Type) throws -> Int64 { fatalError("Not implemented: \(#function)") }
func decode(_ type: UInt.Type) throws -> UInt { fatalError("Not implemented: \(#function)") }
func decode(_ type: UInt8.Type) throws -> UInt8 { fatalError("Not implemented: \(#function)") }
func decode(_ type: UInt16.Type) throws -> UInt16 { fatalError("Not implemented: \(#function)") }
func decode(_ type: UInt32.Type) throws -> UInt32 { fatalError("Not implemented: \(#function)") }
func decode(_ type: UInt64.Type) throws -> UInt64 { fatalError("Not implemented: \(#function)") }
func decode<T>(_ type: T.Type) throws -> T where T : Decodable { fatalError("Not implemented: \(#function)") }
}
}
// ==== Execute ----------------------------------------------------------------
func test() {
let system = DefaultDistributedActorSystem()
// CHECK: assign type:DA, address:ActorAddress(address: "xxx")
// CHECK: ready actor:DA(ActorAddress(address: "xxx"))
let da = DA(actorSystem: system)
// CHECK: encode: ActorAddress(address: "xxx")
let encoder = TestEncoder(system: system)
let data = try! encoder.encode(da)
// CHECK: decode String -> xxx
// CHECK: decode ActorAddress -> ActorAddress(address: "xxx")
let da2 = try! DA(from: TestDecoder(encoder: encoder, system: system, data: data))
// CHECK: decoded da2: DA(ActorAddress(address: "xxx"))
print("decoded da2: \(da2)")
}
@main struct Main {
static func main() async {
test()
}
}
|