Skip to content

Commit

Permalink
move tests from appleTest to supportedTargetTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushg committed Feb 17, 2022
1 parent e26b248 commit 92fb637
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package com.rickclephas.kmp.nativecoroutines

import kotlinx.coroutines.*
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.flow
import kotlin.native.concurrent.AtomicInt
import kotlin.native.concurrent.isFrozen
import kotlin.test.*

class NativeFlowTests {

@Test
fun `ensure frozen`() {
val flow = flow<RandomValue> { }
val flow = flow<RandomValue> { }
assertFalse(flow.isFrozen, "Flow shouldn't be frozen yet")
val nativeFlow = flow.asNativeFlow()
assertTrue(nativeFlow.isFrozen, "NativeFlow should be frozen")
assertTrue(flow.isFrozen, "Flow should be frozen")
}

@Test
fun `ensure completion callback is invoked`() = runBlocking {
val flow = flow<RandomValue> { }
fun `ensure completion callback is invoked`() = kotlinx.coroutines.runBlocking {
val flow = flow<RandomValue> { }
val job = Job()
val nativeFlow = flow.asNativeFlow(CoroutineScope(job))
val completionCount = AtomicInt(0)
val completionCount = kotlin.native.concurrent.AtomicInt(0)
nativeFlow({ _, _ -> }, { error, _ ->
assertNull(error, "Flow should complete without an error")
completionCount.increment()
Expand All @@ -33,12 +35,12 @@ class NativeFlowTests {
}

@Test
fun `ensure exceptions are received as errors`() = runBlocking {
fun `ensure exceptions are received as errors`() = kotlinx.coroutines.runBlocking {
val exception = RandomException()
val flow = flow<RandomValue> { throw exception }
val job = Job()
val nativeFlow = flow.asNativeFlow(CoroutineScope(job))
val completionCount = AtomicInt(0)
val completionCount = kotlin.native.concurrent.AtomicInt(0)
nativeFlow({ _, _ -> }, { error, _ ->
assertNotNull(error, "Flow should complete with an error")
val kotlinException = error.userInfo["KotlinException"]
Expand All @@ -50,26 +52,30 @@ class NativeFlowTests {
}

@Test
fun `ensure values are received`() = runBlocking {
fun `ensure values are received`() = kotlinx.coroutines.runBlocking {
val values = listOf(RandomValue(), RandomValue(), RandomValue(), RandomValue())
val flow = flow { values.forEach { emit(it) } }
val job = Job()
val nativeFlow = flow.asNativeFlow(CoroutineScope(job))
val receivedValueCount = AtomicInt(0)
val receivedValueCount = kotlin.native.concurrent.AtomicInt(0)
nativeFlow({ value, _ ->
assertSame(values[receivedValueCount.value], value, "Received incorrect value")
receivedValueCount.increment()
}, { _, _ -> })
job.children.forEach { it.join() } // Waits for the collection to complete
assertEquals(values.size, receivedValueCount.value, "Item callback should be called for every value")
assertEquals(
values.size,
receivedValueCount.value,
"Item callback should be called for every value"
)
}

@Test
fun `ensure collection is cancelled`() = runBlocking {
fun `ensure collection is cancelled`() = kotlinx.coroutines.runBlocking {
val flow = MutableSharedFlow<RandomValue>()
val job = Job()
val nativeFlow = flow.asNativeFlow(CoroutineScope(job))
val completionCount = AtomicInt(0)
val completionCount = kotlin.native.concurrent.AtomicInt(0)
val cancel = nativeFlow({ _, _ -> }, { error, _ ->
assertNotNull(error, "Flow should complete with an error")
val exception = error.userInfo["KotlinException"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package com.rickclephas.kmp.nativecoroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.cancellation.CancellationException
import kotlin.native.concurrent.AtomicInt
import kotlin.native.concurrent.isFrozen
import kotlin.test.*

Expand All @@ -31,12 +29,12 @@ class NativeSuspendTests {
}

@Test
fun `ensure correct result is received`() = runBlocking {
fun `ensure correct result is received`() = kotlinx.coroutines.runBlocking {
val value = RandomValue()
val job = Job()
val nativeSuspend = nativeSuspend(CoroutineScope(job)) { delayAndReturn(100, value) }
val receivedResultCount = AtomicInt(0)
val receivedErrorCount = AtomicInt(0)
val receivedResultCount = kotlin.native.concurrent.AtomicInt(0)
val receivedErrorCount = kotlin.native.concurrent.AtomicInt(0)
nativeSuspend({ receivedValue, _ ->
assertSame(value, receivedValue, "Received incorrect value")
receivedResultCount.increment()
Expand All @@ -49,12 +47,12 @@ class NativeSuspendTests {
}

@Test
fun `ensure exceptions are received as errors`() = runBlocking {
fun `ensure exceptions are received as errors`() = kotlinx.coroutines.runBlocking {
val exception = RandomException()
val job = Job()
val nativeSuspend = nativeSuspend(CoroutineScope(job)) { delayAndThrow(100, exception) }
val receivedResultCount = AtomicInt(0)
val receivedErrorCount = AtomicInt(0)
val receivedResultCount = kotlin.native.concurrent.AtomicInt(0)
val receivedErrorCount = kotlin.native.concurrent.AtomicInt(0)
nativeSuspend({ _, _ ->
receivedResultCount.increment()
}, { error, _ ->
Expand All @@ -69,11 +67,12 @@ class NativeSuspendTests {
}

@Test
fun `ensure function is cancelled`() = runBlocking {
fun `ensure function is cancelled`() = kotlinx.coroutines.runBlocking {
val job = Job()
val nativeSuspend = nativeSuspend(CoroutineScope(job)) { delayAndReturn(5_000, RandomValue()) }
val receivedResultCount = AtomicInt(0)
val receivedErrorCount = AtomicInt(0)
val nativeSuspend =
nativeSuspend(CoroutineScope(job)) { delayAndReturn(5_000, RandomValue()) }
val receivedResultCount = kotlin.native.concurrent.AtomicInt(0)
val receivedErrorCount = kotlin.native.concurrent.AtomicInt(0)
val cancel = nativeSuspend({ _, _ ->
receivedResultCount.increment()
}, { error, _ ->
Expand Down

0 comments on commit 92fb637

Please sign in to comment.