-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Chat.destroy() method, TimerManager class (#66)
* Minor code cleanup * Chat.destroy() and PlatformTimer refactor * Add test for TimerManager * Formatting * Add tests * Bump pubnub-kotlin version * Update pubnub-chat-impl/src/iosMain/kotlin/com/pubnub/chat/internal/timer/PlatformTimer.ios.kt Co-authored-by: jguz-pubnub <[email protected]> * Update pubnub-chat-impl/src/iosMain/kotlin/com/pubnub/chat/internal/timer/PlatformTimer.ios.kt Co-authored-by: jguz-pubnub <[email protected]> * Fix accepted suggestions --------- Co-authored-by: jguz-pubnub <[email protected]>
- Loading branch information
1 parent
fbd4b7b
commit 82462a3
Showing
18 changed files
with
234 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/internal/TimerManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.pubnub.internal | ||
|
||
import com.pubnub.chat.internal.timer.createTimerManager | ||
import kotlinx.atomicfu.atomic | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.coroutines.withContext | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class TimerManagerTest { | ||
@Test | ||
fun cancelOneOff() = runTest { | ||
val timerManager = createTimerManager() | ||
val completable1 = CompletableDeferred<Unit>() | ||
val completable2 = CompletableDeferred<Unit>() | ||
|
||
// this should run | ||
timerManager.runWithDelay(1.seconds) { | ||
completable1.complete(Unit) | ||
} | ||
|
||
// this shouldn't run | ||
timerManager.runWithDelay(2.seconds) { | ||
completable2.complete(Unit) | ||
} | ||
|
||
completable1.await() | ||
timerManager.destroy() | ||
|
||
// give completable2 a chance to complete (in case destroy() doesn't work) | ||
withContext(Dispatchers.Default) { | ||
delay(2.seconds) | ||
} | ||
|
||
assertFalse { completable2.isCompleted } | ||
} | ||
|
||
@Test | ||
fun cancelPeriodic() = runTest { | ||
val timerManager = createTimerManager() | ||
val counter = atomic(0) | ||
val counter2 = atomic(0) | ||
|
||
// this should run 1 time | ||
timerManager.runPeriodically(1.seconds) { | ||
counter.incrementAndGet() | ||
} | ||
|
||
// this should run 3 times | ||
// let's also try to start it from a background thread to test if cancellation works in that case | ||
withContext(Dispatchers.Default) { | ||
timerManager.runPeriodically(0.5.seconds) { | ||
counter2.incrementAndGet() | ||
} | ||
} | ||
|
||
withContext(Dispatchers.Default) { | ||
delay(1.75.seconds) | ||
} | ||
timerManager.destroy() | ||
|
||
withContext(Dispatchers.Default) { | ||
delay(1.seconds) | ||
} | ||
|
||
assertEquals(1, counter.value) | ||
assertEquals(3, counter2.value) | ||
} | ||
} |
Oops, something went wrong.