|
| 1 | +package io.ably.lib.objects.unit.objects |
| 2 | + |
| 3 | +import io.ably.lib.objects.ObjectData |
| 4 | +import io.ably.lib.objects.ObjectsState |
| 5 | +import io.ably.lib.objects.ROOT_OBJECT_ID |
| 6 | +import io.ably.lib.objects.assertWaiter |
| 7 | +import io.ably.lib.objects.type.livecounter.DefaultLiveCounter |
| 8 | +import io.ably.lib.objects.type.livemap.DefaultLiveMap |
| 9 | +import io.ably.lib.objects.type.livemap.LiveMapEntry |
| 10 | +import io.ably.lib.objects.unit.* |
| 11 | +import io.ably.lib.objects.unit.BufferedObjectOperations |
| 12 | +import io.ably.lib.objects.unit.ObjectsManager |
| 13 | +import io.ably.lib.objects.unit.SyncObjectsDataPool |
| 14 | +import io.ably.lib.objects.unit.getDefaultLiveObjectsWithMockedDeps |
| 15 | +import io.ably.lib.realtime.ChannelState |
| 16 | +import io.mockk.mockk |
| 17 | +import io.mockk.verify |
| 18 | +import kotlinx.coroutines.test.runTest |
| 19 | +import org.junit.Test |
| 20 | +import kotlin.test.assertEquals |
| 21 | + |
| 22 | +class DefaultLiveObjectsTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + fun `(RTO4, RTO4a) When channel ATTACHED with HAS_OBJECTS flag true should start sync sequence`() = runTest { |
| 26 | + val defaultLiveObjects = getDefaultLiveObjectsWithMockedDeps() |
| 27 | + |
| 28 | + // RTO4a - If the HAS_OBJECTS flag is 1, the server will shortly perform an OBJECT_SYNC sequence |
| 29 | + defaultLiveObjects.handleStateChange(ChannelState.attached, true) |
| 30 | + // It is expected that the client will start a new sync sequence |
| 31 | + verify(exactly = 1) { |
| 32 | + defaultLiveObjects.ObjectsManager.startNewSync(null) |
| 33 | + } |
| 34 | + verify(exactly = 0) { |
| 35 | + defaultLiveObjects.ObjectsManager.endSync(any<Boolean>()) |
| 36 | + } |
| 37 | + assertWaiter { defaultLiveObjects.state == ObjectsState.SYNCING } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `(RTO4, RTO4b) When channel ATTACHED with HAS_OBJECTS flag false should complete sync immediately`() = runTest { |
| 42 | + val defaultLiveObjects = getDefaultLiveObjectsWithMockedDeps() |
| 43 | + |
| 44 | + // Set up some objects in objectPool that should be cleared |
| 45 | + val rootObject = defaultLiveObjects.objectsPool.get(ROOT_OBJECT_ID) as DefaultLiveMap |
| 46 | + rootObject.data["key1"] = LiveMapEntry(data = ObjectData("testValue1")) |
| 47 | + defaultLiveObjects.objectsPool.set("dummyObjectId", DefaultLiveCounter("dummyObjectId", mockk(relaxed = true))) |
| 48 | + |
| 49 | + // RTO4b - If the HAS_OBJECTS flag is 0, the sync sequence must be considered complete immediately |
| 50 | + defaultLiveObjects.handleStateChange(ChannelState.attached, false) |
| 51 | + |
| 52 | + verify(exactly = 1) { |
| 53 | + defaultLiveObjects.objectsPool.resetToInitialPool(true) |
| 54 | + } |
| 55 | + verify(exactly = 1) { |
| 56 | + defaultLiveObjects.ObjectsManager.endSync(any<Boolean>()) |
| 57 | + } |
| 58 | + |
| 59 | + // Verify expected outcomes |
| 60 | + assertWaiter { defaultLiveObjects.state == ObjectsState.SYNCED } // RTO4b4 |
| 61 | + assertEquals(0, defaultLiveObjects.ObjectsManager.SyncObjectsDataPool.size) // RTO4b3 |
| 62 | + assertEquals(0, defaultLiveObjects.ObjectsManager.BufferedObjectOperations.size) // RTO4b5 |
| 63 | + assertEquals(1, defaultLiveObjects.objectsPool.size()) // RTO4b1 - Only root remains |
| 64 | + assertEquals(rootObject, defaultLiveObjects.objectsPool.get(ROOT_OBJECT_ID)) // points to previously created root object |
| 65 | + assertEquals(0, rootObject.data.size) // RTO4b2 - root object must be empty |
| 66 | + } |
| 67 | +} |
0 commit comments