@@ -6,11 +6,15 @@ import io.ably.lib.objects.*
66import io.ably.lib.objects.Binary
77import io.ably.lib.objects.integration.helpers.State
88import io.ably.lib.objects.integration.helpers.fixtures.initializeRootMap
9+ import io.ably.lib.objects.integration.helpers.simulateObjectDelete
910import io.ably.lib.objects.integration.setup.IntegrationTest
1011import io.ably.lib.objects.size
1112import io.ably.lib.objects.state.ObjectsStateEvent
1213import io.ably.lib.objects.type.counter.LiveCounter
14+ import io.ably.lib.objects.type.livecounter.DefaultLiveCounter
15+ import io.ably.lib.objects.type.livemap.DefaultLiveMap
1316import io.ably.lib.objects.type.map.LiveMap
17+ import io.ably.lib.objects.type.map.LiveMapUpdate
1418import kotlinx.coroutines.test.runTest
1519import org.junit.Test
1620import kotlin.test.assertEquals
@@ -155,13 +159,14 @@ class DefaultLiveObjectsTest : IntegrationTest() {
155159 }
156160
157161 /* *
158- * Spec: RTLO4e - Tests the removal of objects from the root map.
159162 * Server runs periodic garbage collection (GC) to remove orphaned objects and will send
160163 * OBJECT_DELETE events for objects that are no longer referenced.
161- * `OBJECT_DELETE` event is not covered in the test and we only check if map entries are removed
164+ * So, we simulate the deletion of an object by sending an object delete ProtocolMessage.
165+ * This does not actually delete the object from the server, only simulates the deletion locally.
166+ * Spec: RTLO4e
162167 */
163168 @Test
164- fun testObjectRemovalFromRoot () = runTest {
169+ fun testObjectDelete () = runTest {
165170 val channelName = generateChannelName()
166171 // Initialize the root map on the channel with initial data
167172 restObjects.initializeRootMap(channelName)
@@ -171,19 +176,41 @@ class DefaultLiveObjectsTest : IntegrationTest() {
171176 assertEquals(6L , rootMap.size()) // Should have 6 entries initially
172177
173178 // Remove the "referencedCounter" from the root map
174- assertNotNull(rootMap.get(" referencedCounter" )) // Access to ensure it exists before removal
179+ val refCounter = rootMap.get(" referencedCounter" ) as LiveCounter
180+ assertNotNull(refCounter)
181+ // Subscribe to counter updates to verify removal
182+ val counterUpdates = mutableListOf<Double >()
183+ refCounter.subscribe { event ->
184+ counterUpdates.add(event.update.amount)
185+ }
175186
176- restObjects.removeMapValue(channelName, " root" , " referencedCounter" )
187+ // Simulate the deletion of the referencedCounter object
188+ channel.objects.simulateObjectDelete(channelName, refCounter as DefaultLiveCounter )
177189
178190 assertWaiter { rootMap.size() == 5L } // Wait for the removal to complete
179191 assertNull(rootMap.get(" referencedCounter" )) // Should be null after removal
192+ assertEquals(1 , counterUpdates.size) // Should have received one update for deletion
193+ assertEquals(20.0 , counterUpdates[0 ]) // The update should indicate the counter was removed
180194
181195 // Remove the "referencedMap" from the root map
182- assertNotNull(rootMap.get(" referencedMap" )) // Access to ensure it exists before removal
196+ val referencedMap = rootMap.get(" referencedMap" ) as LiveMap
197+ assertNotNull(referencedMap)
198+ // Subscribe to map updates to verify removal
199+ val mapUpdates = mutableListOf<Map <String , LiveMapUpdate .Change >>()
200+ referencedMap.subscribe { event ->
201+ mapUpdates.add(event.update)
202+ }
183203
184- restObjects.removeMapValue(channelName, " root" , " referencedMap" )
204+ // Simulate the deletion of the referencedMap object
205+ channel.objects.simulateObjectDelete(channelName, referencedMap as DefaultLiveMap )
185206
186207 assertWaiter { rootMap.size() == 4L } // Wait for the removal to complete
187208 assertNull(rootMap.get(" referencedMap" )) // Should be null after removal
209+ assertEquals(1 , mapUpdates.size) // Should have received one update for deletion
210+
211+ val updatedMap = mapUpdates.first()
212+ assertEquals(1 , updatedMap.size) // Should have one change
213+ assertEquals(" counterKey" , updatedMap.keys.first()) // The change should be for the "counterKey"
214+ assertEquals(LiveMapUpdate .Change .REMOVED , updatedMap.values.first()) // Should indicate removal
188215 }
189216}
0 commit comments