Skip to content

Commit b69a40d

Browse files
Add the ability to specify GC options via client options
We'll use this when porting the garbage collection JS integration tests.
1 parent a7f768b commit b69a40d

5 files changed

Lines changed: 59 additions & 4 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
internal import AblyPlugin
2+
3+
internal extension ARTClientOptions {
4+
private class Box<T> {
5+
internal let boxed: T
6+
7+
internal init(boxed: T) {
8+
self.boxed = boxed
9+
}
10+
}
11+
12+
private static let garbageCollectionOptionsKey = "Objects.garbageCollectionOptions"
13+
14+
/// Can be overriden for testing purposes.
15+
var garbageCollectionOptions: InternalDefaultRealtimeObjects.GarbageCollectionOptions? {
16+
get {
17+
let optionsValue = PluginAPI.sharedInstance().pluginOptionsValue(
18+
forKey: Self.garbageCollectionOptionsKey,
19+
clientOptions: self,
20+
)
21+
22+
guard let optionsValue else {
23+
return nil
24+
}
25+
26+
guard let box = optionsValue as? Box<InternalDefaultRealtimeObjects.GarbageCollectionOptions> else {
27+
preconditionFailure("Expected GarbageCollectionOptionsBox, got \(optionsValue)")
28+
}
29+
30+
return box.boxed
31+
}
32+
33+
set {
34+
guard let newValue else {
35+
preconditionFailure("Not implemented the ability to un-set GC options")
36+
}
37+
38+
PluginAPI.sharedInstance().setPluginOptionsValue(
39+
Box<InternalDefaultRealtimeObjects.GarbageCollectionOptions>(boxed: newValue),
40+
forKey: Self.garbageCollectionOptionsKey,
41+
clientOptions: self,
42+
)
43+
}
44+
}
45+
}

Sources/AblyLiveObjects/Internal/DefaultInternalPlugin.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte
3636
internal func prepare(_ channel: AblyPlugin.RealtimeChannel, client: AblyPlugin.RealtimeClient) {
3737
let logger = pluginAPI.logger(for: channel)
3838
let callbackQueue = pluginAPI.callbackQueue(for: client)
39+
let options = pluginAPI.options(for: client)
3940

4041
logger.log("LiveObjects.DefaultInternalPlugin received prepare(_:)", level: .debug)
41-
let liveObjects = InternalDefaultRealtimeObjects(logger: logger, userCallbackQueue: callbackQueue, clock: DefaultSimpleClock())
42+
let liveObjects = InternalDefaultRealtimeObjects(
43+
logger: logger,
44+
userCallbackQueue: callbackQueue,
45+
clock: DefaultSimpleClock(),
46+
garbageCollectionOptions: options.garbageCollectionOptions ?? .init(),
47+
)
4248
pluginAPI.setPluginDataValue(liveObjects, forKey: Self.pluginDataKey, channel: channel)
4349
}
4450

Sources/AblyLiveObjects/Internal/InternalDefaultRealtimeObjects.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal final class InternalDefaultRealtimeObjects: Sendable, LiveMapObjectPool
2626
private nonisolated(unsafe) var garbageCollectionTask: Task<Void, Never>!
2727

2828
/// Parameters used to control the garbage collection of tombstoned objects and map entries, as described in RTO10.
29-
internal struct GarbageCollectionOptions {
29+
internal struct GarbageCollectionOptions: Encodable, Hashable {
3030
/// The RTO10a interval at which we will perform garbage collection.
3131
///
3232
/// The default value comes from the suggestion in RTO10a.

Tests/AblyLiveObjectsTests/Helpers/ClientHelper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Ably
2-
import AblyLiveObjects
2+
@testable import AblyLiveObjects
33

44
/// Helper for creating ably-cocoa objects, for use in integration tests.
55
enum ClientHelper {
@@ -26,6 +26,9 @@ enum ClientHelper {
2626
let logger = PrefixedLogger(prefix: "(\(logIdentifier)) ")
2727
clientOptions.logHandler = logger
2828
}
29+
if let garbageCollectionOptions = options.garbageCollectionOptions {
30+
clientOptions.garbageCollectionOptions = garbageCollectionOptions
31+
}
2932

3033
return ARTRealtime(options: clientOptions)
3134
}
@@ -69,6 +72,7 @@ enum ClientHelper {
6972
struct PartialClientOptions: Encodable, Hashable {
7073
var useBinaryProtocol: Bool?
7174
var autoConnect: Bool?
75+
var garbageCollectionOptions: InternalDefaultRealtimeObjects.GarbageCollectionOptions?
7276

7377
/// A prefix for all log messages emitted by the client. Allows clients to be distinguished in log messages for tests which use multiple clients.
7478
var logIdentifier: String?

0 commit comments

Comments
 (0)