Skip to content

Commit

Permalink
Marked atomicCoroutineScope as private, accessed via reflection+exten…
Browse files Browse the repository at this point in the history
…sion feature
  • Loading branch information
sacOO7 committed Nov 5, 2024
1 parent 30ad922 commit c997d40
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -84,21 +85,21 @@ 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
roomReleased.send(true)
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
Expand All @@ -112,10 +113,21 @@ class RoomLifecycleManagerTest {

@Test
fun `(CHA-RL1e) Attach op should transition room into ATTACHING state`() = runTest {
val status = spyk<DefaultStatus>()
val roomStatusChanges = mutableListOf<RoomStatusChange>()
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c997d40

Please sign in to comment.