-
Notifications
You must be signed in to change notification settings - Fork 1
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
[ECO-4982] Implement Messages
discontinuities (CHA-M7)
#108
Conversation
WalkthroughThe changes in this pull request involve extensive modifications across several files in the AblyChat framework. Key updates include the introduction of the Changes
Assessment against linked issues
Possibly related issues
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Outside diff range and nitpick comments (7)
Tests/AblyChatTests/Mocks/MockFeatureChannel.swift (1)
13-23
: Enhance mock capabilities for better testability.While the current implementation is functional, it could be improved to provide better testing capabilities.
Consider these enhancements:
final actor MockFeatureChannel: FeatureChannel { let channel: RealtimeChannelProtocol private var discontinuitySubscriptions: [Subscription<ARTErrorInfo>] = [] + private var emittedDiscontinuities: [ARTErrorInfo] = [] + private var emissionCount = 0 // ... existing code ... func emitDiscontinuity(_ discontinuity: ARTErrorInfo) { + emittedDiscontinuities.append(discontinuity) for subscription in discontinuitySubscriptions { subscription.emit(discontinuity) + emissionCount += 1 } } + + // Helper methods for tests + func getEmittedDiscontinuities() -> [ARTErrorInfo] { + emittedDiscontinuities + } + + func getEmissionCount() -> Int { + emissionCount + } }These additions would allow tests to verify:
- Which discontinuities were emitted
- How many times emissions occurred
- Track the history of discontinuity events
Sources/AblyChat/RoomFeature.swift (2)
Line range hint
16-24
: Consider safer handling of unimplemented featuresThe use of
fatalError
for unimplemented features could lead to runtime crashes if these features are accidentally accessed. Consider implementing a safer approach, such as returning aResult
type or throwing an error.Here's a suggested improvement:
private var channelNameSuffix: String { switch self { case .messages: // (CHA-M1) Chat messages for a Room are sent on a corresponding realtime channel <roomId>::$chat::$chatMessages. For example, if your room id is my-room then the messages channel will be my-room::$chat::$chatMessages. "chatMessages" case .typing, .reactions, .presence, .occupancy: - // We'll add these, with reference to the relevant spec points, as we implement these features - fatalError("Don't know channel name suffix for room feature \(self)") + throw FeatureError.notImplemented(feature: self) } }With a new error type:
enum FeatureError: Error { case notImplemented(feature: RoomFeature) }
37-44
: Consider making the implementation more flexible and robustWhile the implementation is functional, there are a few potential improvements:
- The
contributor
property could use a protocol type instead of the concreteDefaultRoomLifecycleContributor
to allow for better testing and flexibility.- The
subscribeToDiscontinuities
method could benefit from error handling around the contributor call.Here's a suggested improvement:
internal struct DefaultFeatureChannel: FeatureChannel { internal var channel: RealtimeChannelProtocol - internal var contributor: DefaultRoomLifecycleContributor + internal var contributor: any RoomLifecycleContributor internal func subscribeToDiscontinuities() async -> Subscription<ARTErrorInfo> { + do { await contributor.subscribeToDiscontinuities() + } catch { + // Handle or propagate the error appropriately + throw FeatureError.discontinuitySubscriptionFailed(error) + } } }Sources/AblyChat/DefaultRoomLifecycleContributor.swift (1)
15-19
: Consider adding error handling for emission failuresThe
emitDiscontinuity
method should handle potential failures during emission to prevent a single failed emission from affecting other subscriptions.internal func emitDiscontinuity(_ error: ARTErrorInfo) { for subscription in discontinuitySubscriptions { - subscription.emit(error) + do { + subscription.emit(error) + } catch { + // Log the error but continue with remaining subscriptions + print("Failed to emit discontinuity: \(error)") + } } }Tests/AblyChatTests/DefaultMessagesTests.swift (1)
83-89
: Consider enhancing test coverageWhile the current test effectively covers the basic discontinuity propagation, consider these enhancements:
- Use a specific error type instead of "arbitrary" unknown error
- Test multiple discontinuities in sequence
- Verify behavior when subscription is cancelled
Example enhancement:
-let featureChannelDiscontinuity = ARTErrorInfo.createUnknownError() // arbitrary +// Use specific error types for different scenarios +let discontinuities = [ + ARTErrorInfo.create(withCode: 91001, status: 410, message: "Channel has been detached"), + ARTErrorInfo.create(withCode: 91002, status: 410, message: "Message gap detected") +] + +let messagesDiscontinuitySubscription = await messages.subscribeToDiscontinuities() + +// Emit multiple discontinuities +for expectedDiscontinuity in discontinuities { + await featureChannel.emitDiscontinuity(expectedDiscontinuity) + let actualDiscontinuity = try #require(await messagesDiscontinuitySubscription.first { _ in true }) + #expect(actualDiscontinuity === expectedDiscontinuity) +} + +// Test cancellation +messagesDiscontinuitySubscription.cancel() +await featureChannel.emitDiscontinuity(ARTErrorInfo.createUnknownError()) +let noMoreDiscontinuities = await messagesDiscontinuitySubscription.first { _ in true } == nil +#expect(noMoreDiscontinuities)Sources/AblyChat/Room.swift (2)
84-86
: LGTM! Good architectural improvement.The new approach of creating feature channels first and then deriving channels and contributors improves separation of concerns and makes the relationship between features and their channels more explicit.
This design pattern will make it easier to add new features in the future, as each feature can be encapsulated within its own feature channel.
94-94
: Consider safer feature channel access.The force unwrap (
!
) offeatureChannels[.messages]
could lead to a runtime crash if the messages feature is not properly initialized. Consider using guard let or if let for safer access.- featureChannel: featureChannels[.messages]!, + guard let messagesFeatureChannel = featureChannels[.messages] else { + preconditionFailure("Messages feature channel must be available") + } + featureChannel: messagesFeatureChannel,
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (6)
Sources/AblyChat/DefaultMessages.swift
(3 hunks)Sources/AblyChat/DefaultRoomLifecycleContributor.swift
(2 hunks)Sources/AblyChat/Room.swift
(1 hunks)Sources/AblyChat/RoomFeature.swift
(2 hunks)Tests/AblyChatTests/DefaultMessagesTests.swift
(4 hunks)Tests/AblyChatTests/Mocks/MockFeatureChannel.swift
(1 hunks)
🔇 Additional comments (10)
Tests/AblyChatTests/Mocks/MockFeatureChannel.swift (1)
1-4
: LGTM! Good choice of actor for thread-safe mock implementation.
The use of a final actor
is appropriate here as it provides thread-safety for managing subscriptions and prevents inheritance, which is ideal for test mocks.
Sources/AblyChat/RoomFeature.swift (2)
27-35
: Well-designed protocol with clear separation of concerns
The FeatureChannel
protocol effectively combines channel access with discontinuity handling while maintaining thread safety through Sendable
conformance. The documentation clearly explains its purpose and design rationale.
33-35
: Verify CHA-M7 specification compliance
The FeatureChannel
protocol implements discontinuity handling, but we should verify that it fully satisfies the CHA-M7 specification requirements.
✅ Verification successful
CHA-M7 specification compliance verified
The implementation of discontinuity handling in FeatureChannel
and the corresponding tests in DefaultMessagesTests.swift
align with the CHA-M7 specification requirements.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for any additional CHA-M7 related implementations or tests
rg "CHA-M7" -A 5
Length of output: 1263
Sources/AblyChat/DefaultRoomLifecycleContributor.swift (3)
3-3
: LGTM: Protocol conformance aligns with requirements
The addition of EmitsDiscontinuities
protocol conformance properly implements the message discontinuities handling requirement (CHA-M7).
21-25
: Review unbounded buffering policy
The unbounded
buffering policy could lead to memory issues if many discontinuities occur before they're processed. Consider:
- Using a bounded buffer with a reasonable size limit
- Implementing a TTL (Time-To-Live) for buffered items
- Adding documentation about the memory implications
#!/bin/bash
# Check for other buffering policy usage patterns
rg "bufferingPolicy:\s*\." -A 2
6-6
: Consider implementing subscription cleanup
The discontinuitySubscriptions
array can grow indefinitely as subscriptions are only added. While this is tracked in issue #36, consider implementing a weak reference pattern or subscription lifecycle management to prevent potential memory leaks.
Tests/AblyChatTests/DefaultMessagesTests.swift (2)
14-15
: LGTM: Consistent updates to use featureChannel
The changes consistently update the test initialization pattern to use MockFeatureChannel
, maintaining the original test intentions while adapting to the new architecture.
Also applies to: 32-33, 57-58
72-90
: LGTM: Well-structured test for CHA-M7 specification
The test effectively verifies the propagation of discontinuities from featureChannel
to DefaultMessages
, following a clear Given-When-Then structure.
Sources/AblyChat/Room.swift (1)
101-106
: LGTM! Well-structured for future extensibility.
The feature channel creation is well-implemented and cleanly structured. The current implementation only handles the messages feature, which aligns with the PR objectives. The structure is ready for adding more features (presence, reactions, typing, occupancy) in the future.
Let's verify that we're not missing any required features in the current implementation:
Sources/AblyChat/DefaultMessages.swift (1)
106-108
: Method subscribeToDiscontinuities
correctly forwards the call
The subscribeToDiscontinuities
method properly awaits and returns the result from featureChannel.subscribeToDiscontinuities()
. This implementation aligns with the requirements and ensures that discontinuity events are handled appropriately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, left minor suggestion.
Note: This PR is based on top of #107; please review that one first.
Based on same spec as 8daa191.
Resolves #47.
Summary by CodeRabbit
Release Notes
New Features
FeatureChannel
protocol andDefaultFeatureChannel
struct for enhanced channel functionality.DefaultMessages
class to utilizefeatureChannel
, improving message handling and subscription management.subscribeToDiscontinuities
method inDefaultRoomLifecycleContributor
for better discontinuity event handling.Bug Fixes
Tests
DefaultMessages
to reflect changes in initialization and added scenarios for testing discontinuities.MockFeatureChannel
for simulating channel behavior in tests.