Skip to content

Commit 06a2157

Browse files
sacOO7ttypic
authored andcommitted
[ECO-5056] Updated retrieveObjectsGCGracePeriod to return gcperiod every time
CONNECTED message is received, updated tests for the same
1 parent ed77613 commit 06a2157

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

liveobjects/src/main/kotlin/io/ably/lib/objects/Helpers.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import io.ably.lib.realtime.ChannelState
44
import io.ably.lib.realtime.CompletionListener
55
import io.ably.lib.types.Callback
66
import io.ably.lib.realtime.ConnectionEvent
7-
import io.ably.lib.realtime.ConnectionState
7+
import io.ably.lib.realtime.ConnectionStateListener
88
import io.ably.lib.types.ChannelMode
99
import io.ably.lib.types.ErrorInfo
1010
import io.ably.lib.types.ProtocolMessage
@@ -51,19 +51,14 @@ internal suspend fun ObjectsAdapter.attachAsync(channelName: String) = suspendCa
5151
}
5252
}
5353

54-
internal fun ObjectsAdapter.retrieveObjectsGCGracePeriod(block : (Long?) -> Unit) {
55-
connectionManager.objectsGCGracePeriod?.let {
56-
block(it)
57-
return
58-
}
59-
// If already connected, no further `connected` event is guaranteed; return immediately.
60-
if (connection.state == ConnectionState.connected) {
61-
block(connectionManager.objectsGCGracePeriod)
62-
return
63-
}
64-
connection.once(ConnectionEvent.connected) {
54+
internal fun ObjectsAdapter.onGCGracePeriodUpdated(block : (Long?) -> Unit) : ObjectsSubscription {
55+
connectionManager.objectsGCGracePeriod?.let { block(it) }
56+
// Return new objectsGCGracePeriod whenever connection state changes to connected
57+
val listener: (_: ConnectionStateListener.ConnectionStateChange) -> Unit = {
6558
block(connectionManager.objectsGCGracePeriod)
6659
}
60+
connection.on(ConnectionEvent.connected, listener)
61+
return ObjectsSubscription { connection.off(listener) }
6762
}
6863

6964
/**

liveobjects/src/main/kotlin/io/ably/lib/objects/ObjectsPool.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ internal class ObjectsPool(
5252
private val gcScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
5353
private var gcJob: Job // Job for the garbage collection coroutine
5454

55-
@Volatile
56-
private var gcGracePeriod = ObjectsPoolDefaults.GC_GRACE_PERIOD_MS
55+
@Volatile private var gcGracePeriod = ObjectsPoolDefaults.GC_GRACE_PERIOD_MS
56+
private var gcPeriodSubscription: ObjectsSubscription
5757

5858
init {
5959
// RTO3b - Initialize pool with root object
6060
pool[ROOT_OBJECT_ID] = DefaultLiveMap.zeroValue(ROOT_OBJECT_ID, realtimeObjects)
6161
// Start garbage collection coroutine with server-provided grace period if available
62-
realtimeObjects.adapter.retrieveObjectsGCGracePeriod { period ->
62+
gcPeriodSubscription = realtimeObjects.adapter.onGCGracePeriodUpdated { period ->
6363
period?.let {
6464
gcGracePeriod = it
6565
Log.i(tag, "Using objectsGCGracePeriod from server: $gcGracePeriod ms")
@@ -164,6 +164,7 @@ internal class ObjectsPool(
164164
* Should be called when the pool is no longer needed.
165165
*/
166166
fun dispose() {
167+
gcPeriodSubscription.unsubscribe()
167168
gcJob.cancel()
168169
gcScope.cancel()
169170
pool.clear()

liveobjects/src/test/kotlin/io/ably/lib/objects/unit/HelpersTest.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,52 +60,52 @@ class HelpersTest {
6060
}
6161

6262
@Test
63-
fun testRetrieveObjectsGCGracePeriodImmediateInvokesBlock() {
63+
fun testOnGCGracePeriodImmediateInvokesBlock() {
6464
val adapter = getMockObjectsAdapter()
6565
val connManager = adapter.connectionManager
6666
connManager.setPrivateField("objectsGCGracePeriod", 123L)
6767

6868
var value: Long? = null
69-
adapter.retrieveObjectsGCGracePeriod { v -> value = v }
69+
adapter.onGCGracePeriodUpdated { v -> value = v }
7070

7171
assertEquals(123L, value)
72-
verify(exactly = 0) { adapter.connection.once(ConnectionEvent.connected, any<ConnectionStateListener>()) }
72+
verify(exactly = 1) { adapter.connection.on(ConnectionEvent.connected, any<ConnectionStateListener>()) }
7373
}
7474

7575
@Test
76-
fun testRetrieveObjectsGCGracePeriodDeferredInvokesOnConnectedWithValue() {
76+
fun testOnGCGracePeriodDeferredInvokesOnConnectedWithValue() {
7777
val adapter = getMockObjectsAdapter()
7878
val connManager = adapter.connectionManager
7979
val connection = adapter.connection
8080

8181
var value: Long? = null
82-
every { connection.once(ConnectionEvent.connected, any<ConnectionStateListener>()) } answers {
82+
every { connection.on(ConnectionEvent.connected, any<ConnectionStateListener>()) } answers {
8383
val listener = secondArg<ConnectionStateListener>()
8484
connManager.setPrivateField("objectsGCGracePeriod", 456L)
8585
listener.onConnectionStateChanged(mockk(relaxed = true))
8686
}
8787

88-
adapter.retrieveObjectsGCGracePeriod { v -> value = v }
88+
adapter.onGCGracePeriodUpdated { v -> value = v }
8989

9090
assertEquals(456L, value)
91-
verify(exactly = 1) { connection.once(ConnectionEvent.connected, any<ConnectionStateListener>()) }
91+
verify(exactly = 1) { connection.on(ConnectionEvent.connected, any<ConnectionStateListener>()) }
9292
}
9393

9494
@Test
95-
fun testRetrieveObjectsGCGracePeriodDeferredInvokesOnConnectedWithNull() {
95+
fun testOnGCGracePeriodDeferredInvokesOnConnectedWithNull() {
9696
val adapter = getMockObjectsAdapter()
9797
val connection = adapter.connection
9898

9999
var value: Long? = null
100-
every { connection.once(ConnectionEvent.connected, any<ConnectionStateListener>()) } answers {
100+
every { connection.on(ConnectionEvent.connected, any<ConnectionStateListener>()) } answers {
101101
val listener = secondArg<ConnectionStateListener>()
102102
listener.onConnectionStateChanged(mockk(relaxed = true))
103103
}
104104

105-
adapter.retrieveObjectsGCGracePeriod { v -> value = v }
105+
adapter.onGCGracePeriodUpdated { v -> value = v }
106106

107107
assertNull(value)
108-
verify(exactly = 1) { connection.once(ConnectionEvent.connected, any<ConnectionStateListener>()) }
108+
verify(exactly = 1) { connection.on(ConnectionEvent.connected, any<ConnectionStateListener>()) }
109109
}
110110

111111
@Test

0 commit comments

Comments
 (0)