Skip to content

Commit cbaf7fe

Browse files
committed
distributed_actor_local
1 parent eec9362 commit cbaf7fe

13 files changed

+80
-49
lines changed

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ extension DistributedActorSystem {
160160
) async throws where Act: DistributedActor,
161161
Act.ID == ActorID,
162162
ResultHandler: DistributedTargetInvocationResultHandler {
163-
fatalError("TODO: synthesize and invoke the _executedDistributedTarget")
163+
fatalError("TODO: synthesize and invoke the _executeDistributedTarget")
164164
}
165165
}
166166

167167
@available(SwiftStdlib 5.6, *)
168168
@_silgen_name("swift_distributed_execute_target")
169-
func _executedDistributedTarget(
169+
func _executeDistributedTarget(
170170
on actor: AnyObject, // DistributedActor
171171
_ targetName: UnsafePointer<UInt8>, _ targetNameLength: UInt,
172172
argumentBuffer: Builtin.RawPointer, // HeterogeneousBuffer of arguments

test/Distributed/Runtime/distributed_actor_deinit.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
127127

128128
@available(SwiftStdlib 5.6, *)
129129
func test() {
130-
let transport = FakeActorSystem()
130+
let system = FakeActorSystem()
131131

132132
// no lifecycle things make sense for a normal actor, double check we didn't emit them
133133
print("before A")
@@ -137,22 +137,22 @@ func test() {
137137
// CHECK: after A
138138

139139
_ = { () -> DA in
140-
DA(transport: transport)
140+
DA(system: system)
141141
}()
142142
// CHECK: assign type:DA, address:[[ADDRESS:.*]]
143143
// CHECK: ready actor:main.DA, address:ActorAddress(address: "[[ADDR1:addr-[0-9]]]")
144144
// CHECK: resign address:ActorAddress(address: "[[ADDR1]]")
145145

146146
_ = { () -> DA_userDefined in
147-
DA_userDefined(transport: transport)
147+
DA_userDefined(system: system)
148148
}()
149149
// CHECK: assign type:DA_userDefined, address:[[ADDRESS:.*]]
150150
// CHECK: ready actor:main.DA_userDefined, address:ActorAddress(address: "[[ADDR2:addr-[0-9]]]")
151151
// CHECK: resign address:ActorAddress(address: "[[ADDR2]]")
152152

153153
// resign must happen as the _last thing_ after user-deinit completed
154154
_ = { () -> DA_userDefined2 in
155-
DA_userDefined2(transport: transport)
155+
DA_userDefined2(system: system)
156156
}()
157157
// CHECK: assign type:DA_userDefined2, address:[[ADDRESS:.*]]
158158
// CHECK: ready actor:main.DA_userDefined2, address:ActorAddress(address: "[[ADDR3:addr-[0-9]]]")
@@ -161,7 +161,7 @@ func test() {
161161

162162
// resign must happen as the _last thing_ after user-deinit completed
163163
_ = { () -> DA_state in
164-
DA_state(transport: transport)
164+
DA_state(system: system)
165165
}()
166166
// CHECK: assign type:DA_state, address:[[ADDRESS:.*]]
167167
// CHECK: ready actor:main.DA_state, address:ActorAddress(address: "[[ADDR4:addr-[0-9]]]")

test/Distributed/Runtime/distributed_actor_dynamic_remote_func.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
7878
// ==== Execute ----------------------------------------------------------------
7979

8080
func test_local() async throws {
81-
let transport = FakeActorSystem()
81+
let system = FakeActorSystem()
8282

83-
let worker = LocalWorker(transport: transport)
83+
let worker = LocalWorker(system: system)
8484
let x = try await worker.function()
8585
print("call: \(x)")
8686
// CHECK: assign type:LocalWorker, id:[[ADDRESS:.*]]
@@ -90,7 +90,7 @@ func test_local() async throws {
9090

9191
func test_remote() async throws {
9292
let address = ActorAddress(parse: "")
93-
let transport = FakeActorSystem()
93+
let system = FakeActorSystem()
9494

9595
let worker = try LocalWorker.resolve(.init(address), using: transport)
9696
let x = try await worker.function()

test/Distributed/Runtime/distributed_actor_init_local.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,38 +107,38 @@ struct FakeActorSystem: DistributedActorSystem {
107107

108108
@available(SwiftStdlib 5.6, *)
109109
func test() async {
110-
let transport = FakeActorSystem()
110+
let system = FakeActorSystem()
111111

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

117-
test.append(LocalWorker(transport: transport))
117+
test.append(LocalWorker(system: system))
118118
// CHECK: assign type:LocalWorker, id:ActorAddress(address: "[[ID1:.*]]")
119119
// CHECK: ready actor:main.LocalWorker, id:AnyActorIdentity(ActorAddress(address: "[[ID1]]"))
120120

121121
test.append(PickATransport1(kappa: transport, other: 0))
122122
// CHECK: assign type:PickATransport1, id:ActorAddress(address: "[[ID2:.*]]")
123123
// CHECK: ready actor:main.PickATransport1, id:AnyActorIdentity(ActorAddress(address: "[[ID2]]"))
124124

125-
test.append(try? Throwy(transport: transport, doThrow: false))
125+
test.append(try? Throwy(system: system, doThrow: false))
126126
// CHECK: assign type:Throwy, id:ActorAddress(address: "[[ID3:.*]]")
127127
// CHECK: ready actor:main.Throwy, id:AnyActorIdentity(ActorAddress(address: "[[ID3]]"))
128128

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

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

137-
test.append(await PickATransport2(other: 1, theTransport: transport))
137+
test.append(await PickATransport2(other: 1, thesystem: system))
138138
// CHECK: assign type:PickATransport2, id:ActorAddress(address: "[[ID6:.*]]")
139139
// CHECK: ready actor:main.PickATransport2, id:AnyActorIdentity(ActorAddress(address: "[[ID6]]"))
140140

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

test/Distributed/Runtime/distributed_actor_isRemote.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ func __isLocalActor(_ actor: AnyObject) -> Bool {
9090
@available(SwiftStdlib 5.6, *)
9191
func test_remote() async {
9292
let address = ActorAddress(parse: "sact://127.0.0.1/example#1234")
93-
let transport = FakeActorSystem()
93+
let system = FakeActorSystem()
9494

95-
let local = SomeSpecificDistributedActor(transport: transport)
95+
let local = SomeSpecificDistributedActor(system: system)
9696
assert(__isLocalActor(local) == true, "should be local")
9797
assert(__isRemoteActor(local) == false, "should be local")
9898
print("isRemote(local) = \(__isRemoteActor(local))") // CHECK: isRemote(local) = false

test/Distributed/Runtime/distributed_actor_local.swift

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,55 @@ struct ActorAddress: Sendable, Hashable, Codable {
4343

4444
@available(SwiftStdlib 5.6, *)
4545
struct FakeActorSystem: DistributedActorSystem {
46+
typealias ActorID = ActorAddress
47+
typealias Invocation = FakeInvocation
48+
typealias SerializationRequirement = Codable
4649

4750
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
4851
where Act: DistributedActor,
4952
Act.ID == ActorID {
5053
return nil
5154
}
5255

53-
func assignID<Act>(_ actorType: Act.Type) -> AnyActorIdentity
54-
where Act: DistributedActor {
55-
.init(ActorAddress(parse: ""))
56+
func assignID<Act>(_ actorType: Act.Type) -> ActorID
57+
where Act: DistributedActor,
58+
Act.ID == ActorID {
59+
ActorAddress(parse: "")
5660
}
5761

5862
public func actorReady<Act>(_ actor: Act)
59-
where Act: DistributedActor {
63+
where Act: DistributedActor,
64+
Act.ID == ActorID {
6065
print("\(#function):\(actor)")
6166
}
6267

63-
func resignID(_ id: AnyActorIdentity) {}
68+
func resignID(_ id: ActorID) {}
69+
70+
func makeInvocation() -> Invocation {
71+
.init()
72+
}
73+
}
74+
75+
struct FakeInvocation: DistributedTargetInvocation {
76+
typealias ArgumentDecoder = FakeArgumentDecoder
77+
typealias SerializationRequirement = Codable
78+
79+
mutating func recordGenericSubstitution<T>(mangledType: T.Type) throws {}
80+
mutating func recordArgument<Argument: SerializationRequirement>(argument: Argument) throws {}
81+
mutating func recordReturnType<R: SerializationRequirement>(mangledType: R.Type) throws {}
82+
mutating func recordErrorType<E: Error>(mangledType: E.Type) throws {}
83+
mutating func doneRecording() throws {}
84+
85+
// === Receiving / decoding -------------------------------------------------
86+
87+
mutating func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
88+
mutating func argumentDecoder() -> FakeArgumentDecoder { .init() }
89+
mutating func decodeReturnType() throws -> Any.Type? { nil }
90+
mutating func decodeErrorType() throws -> Any.Type? { nil }
91+
92+
struct FakeArgumentDecoder: DistributedTargetInvocationArgumentDecoder {
93+
typealias SerializationRequirement = Codable
94+
}
6495
}
6596

6697
@available(SwiftStdlib 5.6, *)
@@ -71,32 +102,32 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
71102
@available(SwiftStdlib 5.6, *)
72103
func test_initializers() {
73104
let address = ActorAddress(parse: "")
74-
let transport = FakeActorSystem()
105+
let system = FakeActorSystem()
75106

76-
_ = SomeSpecificDistributedActor(transport: transport)
77-
_ = try! SomeSpecificDistributedActor.resolve(.init(address), using: transport)
107+
_ = SomeSpecificDistributedActor(system: system)
108+
_ = try! SomeSpecificDistributedActor.resolve(id: address, using: system)
78109
}
79110

80111
@available(SwiftStdlib 5.6, *)
81112
func test_address() {
82-
let transport = FakeActorSystem()
113+
let system = FakeActorSystem()
83114

84-
let actor = SomeSpecificDistributedActor(transport: transport)
115+
let actor = SomeSpecificDistributedActor(system: system)
85116
_ = actor.id
86117
}
87118

88119
@available(SwiftStdlib 5.6, *)
89-
func test_run(transport: FakeActorSystem) async {
90-
let actor = SomeSpecificDistributedActor(transport: transport)
120+
func test_run(system: FakeActorSystem) async {
121+
let actor = SomeSpecificDistributedActor(system: system)
91122

92123
print("before") // CHECK: before
93124
try! await actor.hello()
94125
print("after") // CHECK: after
95126
}
96127

97128
@available(SwiftStdlib 5.6, *)
98-
func test_echo(transport: FakeActorSystem) async {
99-
let actor = SomeSpecificDistributedActor(transport: transport)
129+
func test_echo(system: FakeActorSystem) async {
130+
let actor = SomeSpecificDistributedActor(system: system)
100131

101132
let echo = try! await actor.echo(int: 42)
102133
print("echo: \(echo)") // CHECK: echo: 42
@@ -105,7 +136,7 @@ func test_echo(transport: FakeActorSystem) async {
105136
@available(SwiftStdlib 5.6, *)
106137
@main struct Main {
107138
static func main() async {
108-
await test_run(transport: FakeActorSystem())
109-
await test_echo(transport: FakeActorSystem())
139+
await test_run(system: FakeActorSystem())
140+
await test_echo(system: FakeActorSystem())
110141
}
111142
}

test/Distributed/Runtime/distributed_actor_remote_fieldsDontCrashDeinit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
8686
@available(SwiftStdlib 5.6, *)
8787
func test_remote() async {
8888
let address = ActorAddress(parse: "sact://127.0.0.1/example#1234")
89-
let transport = FakeActorSystem()
89+
let system = FakeActorSystem()
9090

9191
var remote: SomeSpecificDistributedActor? =
9292
try! SomeSpecificDistributedActor.resolve(.init(address), using: transport)

test/Distributed/Runtime/distributed_actor_remote_functions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func test_remote_invoke(address: ActorAddress, transport: FakeActorSystem) async
215215
let remote = try! SomeSpecificDistributedActor.resolve(.init(address), using: transport)
216216
assert(__isRemoteActor(remote) == true, "should be remote")
217217

218-
let local = SomeSpecificDistributedActor(transport: transport)
218+
let local = SomeSpecificDistributedActor(system: system)
219219
assert(__isRemoteActor(local) == false, "should be local")
220220

221221
print("local isRemote: \(__isRemoteActor(local))")
@@ -248,8 +248,8 @@ func test_remote_invoke(address: ActorAddress, transport: FakeActorSystem) async
248248
@main struct Main {
249249
static func main() async {
250250
let address = ActorAddress(address: "")
251-
let transport = FakeActorSystem()
251+
let system = FakeActorSystem()
252252

253-
await test_remote_invoke(address: address, transport: transport)
253+
await test_remote_invoke(address: address, system: system)
254254
}
255255
}

test/Distributed/Runtime/distributed_actor_self_calls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
6565
// ==== Execute ----------------------------------------------------------------
6666

6767
func test(transport: FakeActorSystem) async {
68-
_ = Philosopher(transport: transport)
68+
_ = Philosopher(system: system)
6969
}
7070

7171
@main struct Main {

test/Distributed/Runtime/distributed_actor_whenLocal.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ struct FakeActorSystem: DistributedActorSystem {
5757
typealias DefaultDistributedActorSystem = FakeActorSystem
5858

5959
func test() async throws {
60-
let transport = FakeActorSystem()
60+
let system = FakeActorSystem()
6161

62-
let local = Capybara(transport: transport)
62+
let local = Capybara(system: system)
6363
// await local.eat() // SHOULD ERROR
6464
let valueWhenLocal: String? = await local.whenLocal { __secretlyKnownToBeLocal in
6565
__secretlyKnownToBeLocal.eat()

test/Distributed/Runtime/distributed_no_transport_boom.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
6767
@available(SwiftStdlib 5.6, *)
6868
func test_remote() async {
6969
let address = ActorAddress(parse: "")
70-
let transport = FakeActorSystem()
70+
let system = FakeActorSystem()
7171

7272
let remote = try! SomeSpecificDistributedActor.resolve(.init(address), using: transport)
7373
_ = try! await remote.hello() // let it crash!

test/Distributed/distributed_actor_initialization.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ distributed actor OK3 {
5555
distributed actor OKMulti {
5656

5757
convenience init(y: Int, transport: AnyDistributedActorSystem) { // ok
58-
self.init(transport: transport)
58+
self.init(system: system)
5959
}
6060

6161
}
6262

6363
distributed actor OKMultiDefaultValues {
6464

6565
convenience init(y: Int, transport: AnyDistributedActorSystem, x: Int = 1234) { // ok
66-
self.init(transport: transport)
66+
self.init(system: system)
6767
}
6868

6969
}

test/Distributed/distributed_actor_isolation_and_tasks.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ distributed actor Philosopher {
5858
}
5959

6060
func test_outside(transport: AnyDistributedActorSystem) async throws {
61-
_ = try await Philosopher(transport: transport).dist()
62-
_ = Philosopher(transport: transport).log // expected-error{{distributed actor-isolated property 'log' can only be referenced inside the distributed actor}}
61+
_ = try await Philosopher(system: system).dist()
62+
_ = Philosopher(system: system).log // expected-error{{distributed actor-isolated property 'log' can only be referenced inside the distributed actor}}
6363

64-
_ = Philosopher(transport: transport).id
65-
_ = Philosopher(transport: transport).actorSystem
64+
_ = Philosopher(system: system).id
65+
_ = Philosopher(system: system).actorSystem
6666
}
6767

6868
func test_outside_isolated(phil: isolated Philosopher) async throws {

0 commit comments

Comments
 (0)