From 96418eb38691b634bb176435b72b49971dc07c27 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Fri, 4 Jan 2019 13:53:45 -0500 Subject: Prepare new test for unique only --- .../com/pitchedapps/frost/utils/CoroutineTest.kt | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'app/src/test/kotlin') diff --git a/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt index 72eb6076..32a781df 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt @@ -16,8 +16,10 @@ */ package com.pitchedapps.frost.utils +import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.async import kotlinx.coroutines.channels.BroadcastChannel @@ -31,6 +33,7 @@ import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import java.util.concurrent.Executors import kotlin.coroutines.EmptyCoroutineContext +import kotlin.test.Ignore import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse @@ -206,4 +209,40 @@ class CoroutineTest { ) } } + + /** + * When using [uniqueOnly] for channels with limited capacity, + * the duplicates should not count towards the actual capacity + */ + @Ignore("Not yet working as unique only buffered removes the capacity limitation of the channel") + @Test + fun uniqueOnlyBuffer() { + val completable = CompletableDeferred() + val channel = Channel(3) + runBlocking { + + val deferred = async { + listen(channel.uniqueOnly(GlobalScope)) { + // Throttle consumer + delay(50) + return@listen false + } + } + + listOf(0, 1, 1, 1, 1, 1, 2, 2, 2).forEach { + delay(10) + channel.offer(it) + } + + channel.close() + + val data = deferred.await() + + assertEquals( + listOf(0, 1, 2), + data, + "Unique receiver should not have two consecutive events that are equal" + ) + } + } } -- cgit v1.2.3