Skip to content

Commit

Permalink
Merge pull request #148 from ably-labs/some-logging-improvements
Browse files Browse the repository at this point in the history
Some logging improvements
lawrence-forooghian authored Nov 25, 2024
2 parents 8002523 + 4adb981 commit aeffa2e
Showing 3 changed files with 54 additions and 9 deletions.
16 changes: 14 additions & 2 deletions Sources/AblyChat/DefaultRoomLifecycleContributor.swift
Original file line number Diff line number Diff line change
@@ -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<ARTErrorInfo>] = []
@@ -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)"
}
}
1 change: 1 addition & 0 deletions Sources/AblyChat/RoomLifecycleManager.swift
Original file line number Diff line number Diff line change
@@ -806,6 +806,7 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
do {
logger.log(message: "Attaching contributor \(contributor)", level: .info)
try await contributor.channel.attach()
logger.log(message: "Successfully attached contributor \(contributor)", level: .info)
} catch let contributorAttachError {
let contributorState = await contributor.channel.state
logger.log(message: "Failed to attach contributor \(contributor), which is now in state \(contributorState), error \(contributorAttachError)", level: .info)
46 changes: 39 additions & 7 deletions Tests/AblyChatTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
import Ably
import AblyChat
@testable import AblyChat
import Testing

/// Some very basic integration tests, just to check that things are kind of working.
///
/// 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"

0 comments on commit aeffa2e

Please sign in to comment.