Skip to content

Commit 2dee529

Browse files
committed
[ECO-5426] Refactored initializeHandlerForIncomingObjectMessages to handle exceptions
1. Added try catch to avoid crashing collector 2. Fixed defaultLiveObjects flaky tests
1 parent 7030cef commit 2dee529

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

live-objects/src/main/kotlin/io/ably/lib/objects/DefaultLiveObjects.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@ internal class DefaultLiveObjects(private val channelName: String, internal val
129129
)
130130
}
131131

132-
when (protocolMessage.action) {
133-
ProtocolMessage.Action.`object` -> objectsManager.handleObjectMessages(objects)
134-
ProtocolMessage.Action.object_sync -> objectsManager.handleObjectSyncMessages(
135-
objects,
136-
protocolMessage.channelSerial
137-
)
138-
139-
else -> Log.w(tag, "Ignoring protocol message with unhandled action: ${protocolMessage.action}")
132+
try {
133+
when (protocolMessage.action) {
134+
ProtocolMessage.Action.`object` -> objectsManager.handleObjectMessages(objects)
135+
ProtocolMessage.Action.object_sync -> objectsManager.handleObjectSyncMessages(
136+
objects,
137+
protocolMessage.channelSerial
138+
)
139+
else -> Log.w(tag, "Ignoring protocol message with unhandled action: ${protocolMessage.action}")
140+
}
141+
} catch (exception: Exception) {
142+
// Skip current message if an error occurs, don't rethrow to avoid crashing the collector
143+
Log.e(tag, "Error handling objects message with protocolMsg id ${protocolMessage.id}", exception)
140144
}
141145
}
142146
}

live-objects/src/test/kotlin/io/ably/lib/objects/unit/objects/DefaultLiveObjectsTest.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ class DefaultLiveObjectsTest {
3434

3535
// RTO4a - If the HAS_OBJECTS flag is 1, the server will shortly perform an OBJECT_SYNC sequence
3636
defaultLiveObjects.handleStateChange(ChannelState.attached, true)
37+
38+
assertWaiter { defaultLiveObjects.state == ObjectsState.SYNCING }
39+
3740
// It is expected that the client will start a new sync sequence
3841
verify(exactly = 1) {
3942
defaultLiveObjects.ObjectsManager.startNewSync(null)
4043
}
4144
verify(exactly = 0) {
4245
defaultLiveObjects.ObjectsManager.endSync(any<Boolean>())
4346
}
44-
assertWaiter { defaultLiveObjects.state == ObjectsState.SYNCING }
4547
}
4648

4749
@Test
@@ -57,15 +59,16 @@ class DefaultLiveObjectsTest {
5759
// RTO4b - If the HAS_OBJECTS flag is 0, the sync sequence must be considered complete immediately
5860
defaultLiveObjects.handleStateChange(ChannelState.attached, false)
5961

62+
// Verify expected outcomes
63+
assertWaiter { defaultLiveObjects.state == ObjectsState.SYNCED } // RTO4b4
64+
6065
verify(exactly = 1) {
6166
defaultLiveObjects.objectsPool.resetToInitialPool(true)
6267
}
6368
verify(exactly = 1) {
6469
defaultLiveObjects.ObjectsManager.endSync(any<Boolean>())
6570
}
6671

67-
// Verify expected outcomes
68-
assertWaiter { defaultLiveObjects.state == ObjectsState.SYNCED } // RTO4b4
6972
assertEquals(0, defaultLiveObjects.ObjectsManager.SyncObjectsDataPool.size) // RTO4b3
7073
assertEquals(0, defaultLiveObjects.ObjectsManager.BufferedObjectOperations.size) // RTO4b5
7174
assertEquals(1, defaultLiveObjects.objectsPool.size()) // RTO4b1 - Only root remains

0 commit comments

Comments
 (0)