Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some logging improvements #148

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved
internal nonisolated let channel: DefaultRoomLifecycleContributorChannel
internal nonisolated let feature: RoomFeature
private var discontinuitySubscriptions: [Subscription<ARTErrorInfo>] = []
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Up @@ -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)
Expand Down
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
Expand All @@ -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"
Expand Down