Skip to content

Commit 7023ff6

Browse files
committed
ECO-5447] Added calculateUpdateFromDataDiff method to CounterManager
- Added extra assertion for deleting of "valuesMap"
1 parent 97f55f7 commit 7023ff6

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

live-objects/src/main/kotlin/io/ably/lib/objects/type/livecounter/DefaultLiveCounter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ internal class DefaultLiveCounter private constructor(
8080
}
8181

8282
override fun clearData(): LiveCounterUpdate {
83-
return LiveCounterUpdate(data.get()).apply { data.set(0.0) }
83+
return liveCounterManager.calculateUpdateFromDataDiff(data.get(), 0.0).apply { data.set(0.0) }
8484
}
8585

8686
override fun notifyUpdated(update: LiveObjectUpdate) {

live-objects/src/main/kotlin/io/ably/lib/objects/type/livecounter/LiveCounterManager.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal class LiveCounterManager(private val liveCounter: DefaultLiveCounter):
3333
}
3434
}
3535

36-
return LiveCounterUpdate(liveCounter.data.get() - previousData)
36+
return calculateUpdateFromDataDiff(previousData, liveCounter.data.get())
3737
}
3838

3939
/**
@@ -85,6 +85,10 @@ internal class LiveCounterManager(private val liveCounter: DefaultLiveCounter):
8585
return LiveCounterUpdate(amount)
8686
}
8787

88+
internal fun calculateUpdateFromDataDiff(prevData: Double, newData: Double): LiveCounterUpdate {
89+
return LiveCounterUpdate(newData - prevData)
90+
}
91+
8892
/**
8993
* @spec RTLC10 - Merges initial data from create operation
9094
*/

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class DefaultLiveObjectsTest : IntegrationTest() {
190190
assertWaiter { rootMap.size() == 5L } // Wait for the removal to complete
191191
assertNull(rootMap.get("referencedCounter")) // Should be null after removal
192192
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
193+
assertEquals(-20.0, counterUpdates[0]) // The update should indicate counter was removed with value 20
194194

195195
// Remove the "referencedMap" from the root map
196196
val referencedMap = rootMap.get("referencedMap") as LiveMap
@@ -212,5 +212,28 @@ class DefaultLiveObjectsTest : IntegrationTest() {
212212
assertEquals(1, updatedMap.size) // Should have one change
213213
assertEquals("counterKey", updatedMap.keys.first()) // The change should be for the "counterKey"
214214
assertEquals(LiveMapUpdate.Change.REMOVED, updatedMap.values.first()) // Should indicate removal
215+
216+
// Remove the "valuesMap" from the root map
217+
val valuesMap = rootMap.get("valuesMap") as LiveMap
218+
assertNotNull(valuesMap)
219+
// Subscribe to map updates to verify removal
220+
val valuesMapUpdates = mutableListOf<Map<String, LiveMapUpdate.Change>>()
221+
valuesMap.subscribe { event ->
222+
valuesMapUpdates.add(event.update)
223+
}
224+
225+
// Simulate the deletion of the valuesMap object
226+
channel.objects.simulateObjectDelete(valuesMap as DefaultLiveMap)
227+
228+
assertWaiter { rootMap.size() == 3L } // Wait for the removal to complete
229+
assertNull(rootMap.get("valuesMap")) // Should be null after removal
230+
assertEquals(1, valuesMapUpdates.size) // Should have received one update for deletion
231+
232+
val updatedValuesMap = valuesMapUpdates.first()
233+
assertEquals(13, updatedValuesMap.size) // Should have 13 changes (one for each entry in valuesMap)
234+
// Verify that all entries in valuesMap were marked as REMOVED
235+
updatedValuesMap.values.forEach { change ->
236+
assertEquals(LiveMapUpdate.Change.REMOVED, change)
237+
}
215238
}
216239
}

0 commit comments

Comments
 (0)