From c997d4027cdc4bada2f85b7984f5cf1d342e1555 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Tue, 5 Nov 2024 23:34:10 +0530 Subject: [PATCH] Marked atomicCoroutineScope as private, accessed via reflection+extension feature --- .../com/ably/chat/RoomLifecycleManager.kt | 2 +- .../com/ably/chat/RoomLifecycleManagerTest.kt | 22 ++++++++++++++----- .../extensions/RoomLifecycleManager.kt | 11 ++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 chat-android/src/test/java/com/ably/helpers/extensions/RoomLifecycleManager.kt diff --git a/chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt b/chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt index adf691e9..ff66b53d 100644 --- a/chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt +++ b/chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt @@ -119,7 +119,7 @@ class RoomLifecycleManager * See [Kotlin Dispatchers](https://kt.academy/article/cc-dispatchers) for more information. * Spec: CHA-RL7 */ - internal val atomicCoroutineScope = AtomicCoroutineScope(roomScope) + private val atomicCoroutineScope = AtomicCoroutineScope(roomScope) /** * This flag indicates whether some sort of controlled operation is in progress (e.g. attaching, detaching, releasing). diff --git a/chat-android/src/test/java/com/ably/chat/RoomLifecycleManagerTest.kt b/chat-android/src/test/java/com/ably/chat/RoomLifecycleManagerTest.kt index cc19b164..a9032731 100644 --- a/chat-android/src/test/java/com/ably/chat/RoomLifecycleManagerTest.kt +++ b/chat-android/src/test/java/com/ably/chat/RoomLifecycleManagerTest.kt @@ -1,5 +1,6 @@ package com.ably.chat +import com.ably.helpers.extensions.atomicCoroutineScope import io.ably.lib.types.AblyException import io.mockk.coEvery import io.mockk.coVerify @@ -75,7 +76,7 @@ class RoomLifecycleManagerTest { coEvery { roomLifecycle.release() } coAnswers { - roomLifecycle.atomicCoroutineScope.async { + roomLifecycle.atomicCoroutineScope().async { status.setStatus(RoomLifecycle.Releasing) roomReleased.receive() status.setStatus(RoomLifecycle.Released) @@ -84,13 +85,13 @@ class RoomLifecycleManagerTest { // Release op started from separate coroutine launch { roomLifecycle.release() } - assertWaiter { !roomLifecycle.atomicCoroutineScope.finishedProcessing } - Assert.assertEquals(0, roomLifecycle.atomicCoroutineScope.queuedJobs) // no queued jobs, one job running + assertWaiter { !roomLifecycle.atomicCoroutineScope().finishedProcessing } + Assert.assertEquals(0, roomLifecycle.atomicCoroutineScope().queuedJobs) // no queued jobs, one job running assertWaiter { status.current == RoomLifecycle.Releasing } // Attach op started from separate coroutine val roomAttachOpDeferred = async(SupervisorJob()) { roomLifecycle.attach() } - assertWaiter { roomLifecycle.atomicCoroutineScope.queuedJobs == 1 } // attach op queued + assertWaiter { roomLifecycle.atomicCoroutineScope().queuedJobs == 1 } // attach op queued Assert.assertEquals(RoomLifecycle.Releasing, status.current) // Finish release op, so ATTACH op can start @@ -98,7 +99,7 @@ class RoomLifecycleManagerTest { assertWaiter { status.current == RoomLifecycle.Released } val result = kotlin.runCatching { roomAttachOpDeferred.await() } - Assert.assertTrue(roomLifecycle.atomicCoroutineScope.finishedProcessing) + Assert.assertTrue(roomLifecycle.atomicCoroutineScope().finishedProcessing) Assert.assertTrue(result.isFailure) val exception = result.exceptionOrNull() as AblyException @@ -112,10 +113,21 @@ class RoomLifecycleManagerTest { @Test fun `(CHA-RL1e) Attach op should transition room into ATTACHING state`() = runTest { + val status = spyk() + val roomStatusChanges = mutableListOf() + status.on { + roomStatusChanges.add(it) + } + val roomLifecycle = spyk(RoomLifecycleManager(roomScope, status, emptyList())) + roomLifecycle.attach() + Assert.assertEquals(RoomLifecycle.Attaching, roomStatusChanges[0].current) + Assert.assertEquals(RoomLifecycle.Attached, roomStatusChanges[1].current) + assertWaiter { roomLifecycle.atomicCoroutineScope().finishedProcessing } } @Test fun `(CHA-RL1f) Attach op should attach each contributor channel sequentially`() = runTest { + } @Test diff --git a/chat-android/src/test/java/com/ably/helpers/extensions/RoomLifecycleManager.kt b/chat-android/src/test/java/com/ably/helpers/extensions/RoomLifecycleManager.kt new file mode 100644 index 00000000..7d7fd8a7 --- /dev/null +++ b/chat-android/src/test/java/com/ably/helpers/extensions/RoomLifecycleManager.kt @@ -0,0 +1,11 @@ +package com.ably.helpers.extensions + +import com.ably.chat.AtomicCoroutineScope +import com.ably.chat.RoomLifecycleManager + +fun RoomLifecycleManager.atomicCoroutineScope(): AtomicCoroutineScope { + val loadedClass = RoomLifecycleManager::class + val valueField = loadedClass.java.getDeclaredField("atomicCoroutineScope") + valueField.isAccessible = true + return valueField.get(this) as AtomicCoroutineScope +}