Skip to content

Commit 8a1c513

Browse files
ktosoyim-lee
authored andcommitted
add test and availability annotations
1 parent b001d5b commit 8a1c513

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

stdlib/public/Distributed/LocalTestingDistributedActorSystem.swift

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,11 @@ import Glibc
2020
import WinSDK
2121
#endif
2222

23-
public struct LocalTestingActorAddress: Hashable, Sendable, Codable {
24-
public let address: String
25-
26-
public init(parse address: String) {
27-
self.address = address
28-
}
29-
30-
public init(from decoder: Decoder) throws {
31-
let container = try decoder.singleValueContainer()
32-
self.address = try container.decode(String.self)
33-
}
34-
35-
public func encode(to encoder: Encoder) throws {
36-
var container = encoder.singleValueContainer()
37-
try container.encode(self.address)
38-
}
39-
}
40-
23+
/// A `DistributedActorSystem` designed for local only testing.
24+
///
25+
/// It will crash on any attempt of remote communication, but can be useful
26+
/// for learning about `distributed actor` isolation, as well as early
27+
/// prototyping stages of development where a real system is not necessary yet.
4128
@available(SwiftStdlib 5.7, *)
4229
public final class LocalTestingDistributedActorSystem: DistributedActorSystem, @unchecked Sendable {
4330
public typealias ActorID = LocalTestingActorAddress
@@ -86,9 +73,6 @@ public final class LocalTestingDistributedActorSystem: DistributedActorSystem, @
8673
}
8774

8875
public func resignID(_ id: ActorID) {
89-
guard self.assignedIDsLock.withLock({ self.assignedIDs.contains(id) }) else {
90-
fatalError("Attempted to resign unknown id '\(id)'")
91-
}
9276
self.activeActorsLock.withLock {
9377
self.activeActors.removeValue(forKey: id)
9478
}
@@ -109,7 +93,7 @@ public final class LocalTestingDistributedActorSystem: DistributedActorSystem, @
10993
Act.ID == ActorID,
11094
Err: Error,
11195
Res: SerializationRequirement {
112-
fatalError("Attempted to make remote call on actor \(actor) in a local-only actor system")
96+
fatalError("Attempted to make remote call to \(target) on actor \(actor) using a local-only actor system")
11397
}
11498

11599
public func remoteCallVoid<Act, Err>(
@@ -121,7 +105,7 @@ public final class LocalTestingDistributedActorSystem: DistributedActorSystem, @
121105
where Act: DistributedActor,
122106
Act.ID == ActorID,
123107
Err: Error {
124-
fatalError("Attempted to make remote call on actor \(actor) in a local-only actor system")
108+
fatalError("Attempted to make remote call to \(target) on actor \(actor) using a local-only actor system")
125109
}
126110

127111
private struct ActorIDProvider {
@@ -140,6 +124,26 @@ public final class LocalTestingDistributedActorSystem: DistributedActorSystem, @
140124
}
141125
}
142126

127+
@available(SwiftStdlib 5.7, *)
128+
public struct LocalTestingActorAddress: Hashable, Sendable, Codable {
129+
public let address: String
130+
131+
public init(parse address: String) {
132+
self.address = address
133+
}
134+
135+
public init(from decoder: Decoder) throws {
136+
let container = try decoder.singleValueContainer()
137+
self.address = try container.decode(String.self)
138+
}
139+
140+
public func encode(to encoder: Encoder) throws {
141+
var container = encoder.singleValueContainer()
142+
try container.encode(self.address)
143+
}
144+
}
145+
146+
@available(SwiftStdlib 5.7, *)
143147
public struct LocalTestingInvocationEncoder: DistributedTargetInvocationEncoder {
144148
public typealias SerializationRequirement = Codable
145149

@@ -164,7 +168,8 @@ public struct LocalTestingInvocationEncoder: DistributedTargetInvocationEncoder
164168
}
165169
}
166170

167-
public class LocalTestingInvocationDecoder : DistributedTargetInvocationDecoder {
171+
@available(SwiftStdlib 5.7, *)
172+
public final class LocalTestingInvocationDecoder : DistributedTargetInvocationDecoder {
168173
public typealias SerializationRequirement = Codable
169174

170175
public func decodeGenericSubstitutions() throws -> [Any.Type] {
@@ -197,6 +202,7 @@ public struct LocalTestingDistributedActorSystemError: DistributedActorSystemErr
197202

198203
// === lock ----------------------------------------------------------------
199204

205+
@available(SwiftStdlib 5.7, *)
200206
fileprivate class _Lock {
201207
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
202208
private let underlying: UnsafeMutablePointer<os_unfair_lock>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-distributed -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s -o %t/a.out
3+
// RUN: %target-run %t/a.out | %FileCheck %s --color
4+
5+
// REQUIRES: executable_test
6+
// REQUIRES: concurrency
7+
// REQUIRES: distributed
8+
9+
// rdar://76038845
10+
// UNSUPPORTED: use_os_stdlib
11+
// UNSUPPORTED: back_deployment_runtime
12+
13+
import _Distributed
14+
15+
distributed actor Worker {
16+
typealias ActorSystem = LocalTestingDistributedActorSystem
17+
18+
distributed func hi() {
19+
print("hi!")
20+
}
21+
22+
nonisolated var description: Swift.String {
23+
"Worker(\(id))"
24+
}
25+
}
26+
27+
// ==== Execute ----------------------------------------------------------------
28+
@main struct Main {
29+
static func main() async throws {
30+
let system = LocalTestingDistributedActorSystem()
31+
32+
let actor = Worker(system: system)
33+
try await actor.hi() // local calls should still just work
34+
// CHECK: hi!
35+
}
36+
}

0 commit comments

Comments
 (0)