From 83581c71b8d42e2d8be95a1ede4444d94071ec1d Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Mon, 25 Nov 2024 15:38:07 -0300 Subject: [PATCH 1/3] Add a log statement --- Sources/AblyChat/RoomLifecycleManager.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/AblyChat/RoomLifecycleManager.swift b/Sources/AblyChat/RoomLifecycleManager.swift index 67cdcff4..51a92545 100644 --- a/Sources/AblyChat/RoomLifecycleManager.swift +++ b/Sources/AblyChat/RoomLifecycleManager.swift @@ -806,6 +806,7 @@ internal actor DefaultRoomLifecycleManager Date: Mon, 25 Nov 2024 15:34:21 -0300 Subject: [PATCH 2/3] Add CustomDebugStringConvertible to DefaultRoomLifecycleContributor Useful for debugging, to see the room feature and the address of the underlying ably-cocoa channel (for matching with ably-cocoa logs). --- .../DefaultRoomLifecycleContributor.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Sources/AblyChat/DefaultRoomLifecycleContributor.swift b/Sources/AblyChat/DefaultRoomLifecycleContributor.swift index 5254a739..fd991ed6 100644 --- a/Sources/AblyChat/DefaultRoomLifecycleContributor.swift +++ b/Sources/AblyChat/DefaultRoomLifecycleContributor.swift @@ -1,6 +1,6 @@ import Ably -internal actor DefaultRoomLifecycleContributor: RoomLifecycleContributor, EmitsDiscontinuities { +internal actor DefaultRoomLifecycleContributor: RoomLifecycleContributor, EmitsDiscontinuities, CustomDebugStringConvertible { internal nonisolated let channel: DefaultRoomLifecycleContributorChannel internal nonisolated let feature: RoomFeature private var discontinuitySubscriptions: [Subscription] = [] @@ -24,9 +24,15 @@ internal actor DefaultRoomLifecycleContributor: RoomLifecycleContributor, EmitsD discontinuitySubscriptions.append(subscription) return subscription } + + // MARK: - CustomDebugStringConvertible + + internal nonisolated var debugDescription: String { + "(\(id): \(feature), \(channel))" + } } -internal final class DefaultRoomLifecycleContributorChannel: RoomLifecycleContributorChannel { +internal final class DefaultRoomLifecycleContributorChannel: RoomLifecycleContributorChannel, CustomDebugStringConvertible { private let underlyingChannel: any RealtimeChannelProtocol internal init(underlyingChannel: any RealtimeChannelProtocol) { @@ -55,4 +61,10 @@ internal final class DefaultRoomLifecycleContributorChannel: RoomLifecycleContri underlyingChannel.on { subscription.emit($0) } return subscription } + + // MARK: - CustomDebugStringConvertible + + internal var debugDescription: String { + "\(underlyingChannel)" + } } From 4adb98161de111c27debf1c66b314a6a9b86dabc Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Mon, 25 Nov 2024 15:35:03 -0300 Subject: [PATCH 3/3] Add optional logging to integration tests Useful for debugging. --- Tests/AblyChatTests/IntegrationTests.swift | 46 ++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/Tests/AblyChatTests/IntegrationTests.swift b/Tests/AblyChatTests/IntegrationTests.swift index 5167cd79..abbfa2af 100644 --- a/Tests/AblyChatTests/IntegrationTests.swift +++ b/Tests/AblyChatTests/IntegrationTests.swift @@ -1,5 +1,5 @@ import Ably -import AblyChat +@testable import AblyChat import Testing /// Some very basic integration tests, just to check that things are kind of working. @@ -7,17 +7,49 @@ import Testing /// It would be nice to give this a time limit, but unfortunately the `timeLimit` trait is only available on iOS 16 etc and above. CodeRabbit suggested writing a timeout function myself and wrapping the contents of the test in it, but I didn’t have time to try understanding its suggested code, so it can wait. @Suite struct IntegrationTests { - private static func createSandboxRealtime(apiKey: String) -> ARTRealtime { + private class AblyCocoaLogger: ARTLog { + private let label: String + + init(label: String) { + self.label = label + } + + override func log(_ message: String, with level: ARTLogLevel) { + super.log("\(label): \(message)", with: level) + } + } + + private final class ChatLogger: LogHandler { + private let label: String + private let defaultLogHandler = DefaultLogHandler() + + init(label: String) { + self.label = label + } + + func log(message: String, level: LogLevel, context: LogContext?) { + defaultLogHandler.log(message: "\(label): \(message)", level: level, context: context) + } + } + + private static func createSandboxRealtime(apiKey: String, loggingLabel: String) -> ARTRealtime { let realtimeOptions = ARTClientOptions(key: apiKey) realtimeOptions.environment = "sandbox" realtimeOptions.clientId = UUID().uuidString + if TestLogger.loggingEnabled { + realtimeOptions.logLevel = .verbose + realtimeOptions.logHandler = AblyCocoaLogger(label: loggingLabel) + } + return ARTRealtime(options: realtimeOptions) } - private static func createSandboxChatClient(apiKey: String) -> DefaultChatClient { - let realtime = createSandboxRealtime(apiKey: apiKey) - return DefaultChatClient(realtime: realtime, clientOptions: nil) + private static func createSandboxChatClient(apiKey: String, loggingLabel: String) -> DefaultChatClient { + let realtime = createSandboxRealtime(apiKey: apiKey, loggingLabel: loggingLabel) + let clientOptions = TestLogger.loggingEnabled ? ClientOptions(logHandler: ChatLogger(label: loggingLabel), logLevel: .trace) : nil + + return DefaultChatClient(realtime: realtime, clientOptions: clientOptions) } @Test @@ -27,8 +59,8 @@ struct IntegrationTests { let apiKey = try await Sandbox.createAPIKey() // (1) Create a couple of chat clients — one for sending and one for receiving - let txClient = Self.createSandboxChatClient(apiKey: apiKey) - let rxClient = Self.createSandboxChatClient(apiKey: apiKey) + let txClient = Self.createSandboxChatClient(apiKey: apiKey, loggingLabel: "tx") + let rxClient = Self.createSandboxChatClient(apiKey: apiKey, loggingLabel: "rx") // (2) Fetch a room let roomID = "basketball"