File: distributed_actor_init_local.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (310 lines) | stat: -rw-r--r-- 9,400 bytes parent folder | download
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library) | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed

// rdar://76038845
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime


// FIXME(distributed): Seems something remains incorrect here
// REQUIRES: rdar92952551

import Distributed

enum MyError: Error {
  case test
}

distributed actor PickATransport1 {
  init(kappa system: FakeActorSystem, other: Int) {
    self.actorSystem = system
  }
}

distributed actor PickATransport2 {
  init(other: Int, theSystem: FakeActorSystem) async {
    self.actorSystem = theSystem
  }
}

distributed actor LocalWorker {
  init(system: FakeActorSystem) {
    self.actorSystem = system
  }
}

distributed actor Bug_CallsReadyTwice {
  var x: Int
  init(system: FakeActorSystem, wantBug: Bool) async {
    self.actorSystem = system
    if wantBug {
      self.x = 1
    }
    self.x = 2
  }
}

distributed actor Throwy {
  init(system: FakeActorSystem, doThrow: Bool) throws {
    self.actorSystem = system
    if doThrow {
      throw MyError.test
    }
  }
}

distributed actor ThrowBeforeFullyInit {
  var x: Int
  init(system: FakeActorSystem, doThrow: Bool) throws {
    self.actorSystem = system
    if doThrow {
      throw MyError.test
    }
    self.x = 0
  }
}

distributed actor ThrowingAssign {
  init(_ getSystem: @Sendable () throws -> FakeActorSystem) throws {
    self.actorSystem = try getSystem()
  }
}

distributed actor MaybeSystem {
  init?(_ sys: FakeActorSystem?) {
    if let system = sys {
      self.actorSystem = system
      return
    }
    return nil
  }
}

distributed actor MaybeAfterAssign {
  var x: Int
  init?(fail: Bool) {
    actorSystem = FakeActorSystem()
    if fail {
      return nil
    }
    x = 100
  }
}

distributed actor LocalTestingDA_Int {
  typealias ActorSystem = LocalTestingDistributedActorSystem
  var int: Int
  init() {
    actorSystem = .init()
    int = 12
    // CRASH
  }
}

// ==== Fake Transport ---------------------------------------------------------

struct ActorAddress: Sendable, Hashable, Codable {
  let address: String
  init(parse address: String) {
    self.address = address
  }
}

// global to track available IDs
var nextID: Int = 1

struct FakeActorSystem: DistributedActorSystem {
  public typealias ActorID = ActorAddress
  public typealias InvocationDecoder = FakeInvocationDecoder
  public typealias InvocationEncoder = FakeInvocationEncoder
  public typealias SerializationRequirement = Codable
  public typealias ResultHandler = FakeResultHandler

  init() {
    print("Initialized new FakeActorSystem")
  }

  public func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
      where Act: DistributedActor,
            Act.ID == ActorID  {
    fatalError("not implemented:\(#function)")
  }

  func assignID<Act>(_ actorType: Act.Type) -> ActorID
      where Act: DistributedActor {
    let id = ActorAddress(parse: "\(nextID)")
    nextID += 1
    print("assign type:\(actorType), id:\(id)")
    return id
  }

  func actorReady<Act>(_ actor: Act)
      where Act: DistributedActor,
      Act.ID == ActorID {
    print("ready actor:\(actor), id:\(actor.id)")
  }

  func resignID(_ id: ActorID) {
    print("resign id:\(id)")
  }

  func makeInvocationEncoder() -> InvocationEncoder {
    .init()
  }

  func remoteCall<Act, Err, Res>(
      on actor: Act,
      target: RemoteCallTarget,
      invocation: inout InvocationEncoder,
      throwing: Err.Type,
      returning: Res.Type
  ) async throws -> Res
    where Act: DistributedActor,
          Act.ID == ActorID,
          Err: Error,
          Res: SerializationRequirement {
    throw ExecuteDistributedTargetError(message: "Not implemented")
  }

  func remoteCallVoid<Act, Err>(
      on actor: Act,
      target: RemoteCallTarget,
      invocation: inout InvocationEncoder,
      throwing: Err.Type
  ) async throws
    where Act: DistributedActor,
          Act.ID == ActorID,
          Err: Error {
    throw ExecuteDistributedTargetError(message: "Not implemented")
  }

}

// === Sending / encoding -------------------------------------------------
struct FakeInvocationEncoder: DistributedTargetInvocationEncoder {
  typealias SerializationRequirement = Codable

  mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
  mutating func recordArgument<Value: SerializationRequirement>(_ argument: RemoteCallArgument<Value>) throws {}
  mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
  mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
  mutating func doneRecording() throws {}
}

// === Receiving / decoding -------------------------------------------------
class FakeInvocationDecoder : DistributedTargetInvocationDecoder {
  typealias SerializationRequirement = Codable

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

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)")
  }
}

typealias DefaultDistributedActorSystem = FakeActorSystem

// ==== Execute ----------------------------------------------------------------

func test() async {
  let system = DefaultDistributedActorSystem()

  // NOTE: All allocated distributed actors should be saved in this array, so
  // that they will be deallocated together at the end of this test!
  // This convention helps ensure that the test is not flaky.
  var test: [(any DistributedActor)?] = []

  test.append(LocalWorker(system: system))
  // CHECK: assign type:LocalWorker, id:ActorAddress(address: "[[ID1:.*]]")
  // CHECK: ready actor:main.LocalWorker, id:ActorAddress(address: "[[ID1]]")

  test.append(PickATransport1(kappa: system, other: 0))
  // CHECK: assign type:PickATransport1, id:ActorAddress(address: "[[ID2:.*]]")
  // CHECK: ready actor:main.PickATransport1, id:ActorAddress(address: "[[ID2]]")

  test.append(try? Throwy(system: system, doThrow: false))
  // CHECK: assign type:Throwy, id:ActorAddress(address: "[[ID3:.*]]")
  // CHECK: ready actor:main.Throwy, id:ActorAddress(address: "[[ID3]]")

  test.append(try? Throwy(system: system, doThrow: true))
  // CHECK: assign type:Throwy, id:ActorAddress(address: "[[ID4:.*]]")
  // CHECK-NOT: ready
  // CHECK: resign id:ActorAddress(address: "[[ID4]]")

  test.append(try? ThrowBeforeFullyInit(system: system, doThrow: true))
  // CHECK: assign type:ThrowBeforeFullyInit, id:ActorAddress(address: "[[ID5:.*]]")
  // CHECK-NOT: ready
  // CHECK: resign id:ActorAddress(address: "[[ID5]]")

  test.append(await PickATransport2(other: 1, theSystem: system))
  // CHECK: assign type:PickATransport2, id:ActorAddress(address: "[[ID6:.*]]")
  // CHECK: ready actor:main.PickATransport2, id:ActorAddress(address: "[[ID6]]")

  test.append(await Bug_CallsReadyTwice(system: system, wantBug: true))
    // CHECK: assign type:Bug_CallsReadyTwice, id:ActorAddress(address: "[[ID7:.*]]")
    // CHECK:      ready actor:main.Bug_CallsReadyTwice, id:ActorAddress(address: "[[ID7]]")
    // CHECK-NEXT: ready actor:main.Bug_CallsReadyTwice, id:ActorAddress(address: "[[ID7]]")

  test.append(MaybeSystem(system))
  // CHECK: assign type:MaybeSystem, id:ActorAddress(address: "[[ID8:.*]]")
  // CHECK:      ready actor:main.MaybeSystem, id:ActorAddress(address: "[[ID8]]")

  test.append(MaybeAfterAssign(fail: true))
  // CHECK:      assign type:MaybeAfterAssign, id:ActorAddress(address: "[[ID9:.*]]")
  // CHECK-NOT:  ready
  // CHECK-NEXT: resign id:ActorAddress(address: "[[ID9]]")

  test.append(MaybeAfterAssign(fail: false))
  // CHECK:      assign type:MaybeAfterAssign, id:ActorAddress(address: "[[ID10:.*]]")
  // CHECK-NEXT: ready actor:main.MaybeAfterAssign, id:ActorAddress(address: "[[ID10]]")

  let localDA = LocalTestingDA_Int()
  print("localDA = \(localDA.id)")
  // CHECK: localDA = LocalTestingActorID(id: "1")

  // the following tests fail to initialize the actor's identity.
  print("-- start of no-assign tests --")
  test.append(MaybeSystem(nil))
  test.append(try? ThrowingAssign { throw MyError.test })
  print("-- end of no-assign tests --")
  // CHECK: -- start of no-assign tests --
  // CHECK-NOT: assign
  // CHECK: -- end of no-assign tests --


  // resigns that come out of the deinits:

  // CHECK-DAG: resign id:ActorAddress(address: "[[ID1]]")
  // CHECK-DAG: resign id:ActorAddress(address: "[[ID2]]")
  // CHECK-DAG: resign id:ActorAddress(address: "[[ID3]]")

  // CHECK-DAG: resign id:ActorAddress(address: "[[ID6]]")
  // CHECK-DAG: resign id:ActorAddress(address: "[[ID7]]")
  // CHECK-DAG: resign id:ActorAddress(address: "[[ID8]]")

  // CHECK-DAG: resign id:ActorAddress(address: "[[ID10]]")
}

@main struct Main {
  static func main() async {
    await test()
  }
}