-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
226 additions
and
1 deletion.
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
104 changes: 104 additions & 0 deletions
104
...s/src/commonTest/kotlin/com/arkivanov/mvikotlin/extensions/coroutines/LabelChannelTest.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,104 @@ | ||
package com.arkivanov.mvikotlin.extensions.coroutines | ||
|
||
import com.arkivanov.mvikotlin.core.rx.Disposable | ||
import com.arkivanov.mvikotlin.core.rx.Observer | ||
import com.arkivanov.mvikotlin.core.store.Store | ||
import com.arkivanov.mvikotlin.core.utils.ExperimentalMviKotlinApi | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.DelicateCoroutinesApi | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.launch | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
@OptIn(ExperimentalMviKotlinApi::class) | ||
@Suppress("TestFunctionName") | ||
class LabelChannelTest { | ||
|
||
@Test | ||
fun WHEN_label_emitted_THEN_label_collected() { | ||
val store = TestStore() | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val channel = store.labelsChannel(scope) | ||
val labels = ArrayList<Int>() | ||
|
||
store.labelObserver?.onNext(1) | ||
|
||
scope.launch { | ||
for (label in channel) { | ||
labels += label | ||
} | ||
} | ||
|
||
store.labelObserver?.onNext(2) | ||
store.labelObserver?.onNext(3) | ||
|
||
assertContentEquals(listOf(1, 2, 3), labels) | ||
} | ||
|
||
@Test | ||
fun WHEN_scope_cancelled_THEN_unsubscribed_from_store() { | ||
val store = TestStore() | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val channel = store.labelsChannel(scope) | ||
|
||
scope.launch { | ||
while (true) { | ||
channel.receive() | ||
} | ||
} | ||
|
||
scope.cancel() | ||
|
||
assertNull(store.labelObserver) | ||
} | ||
|
||
@OptIn(DelicateCoroutinesApi::class) | ||
@Test | ||
fun WHEN_scope_cancelled_THEN_channel_cancelled() { | ||
val store = TestStore() | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val channel = store.labelsChannel(scope) | ||
|
||
scope.launch { | ||
while (true) { | ||
channel.receive() | ||
} | ||
} | ||
|
||
scope.cancel() | ||
|
||
assertTrue(channel.isClosedForReceive) | ||
} | ||
|
||
private class TestStore : Store<Int, Int, Int> { | ||
override val state: Int = 0 | ||
override val isDisposed: Boolean = false | ||
|
||
var labelObserver: Observer<Int>? = null | ||
private set | ||
|
||
override fun states(observer: Observer<Int>): Disposable = error("Not required") | ||
|
||
override fun labels(observer: Observer<Int>): Disposable { | ||
labelObserver = observer | ||
|
||
return Disposable { labelObserver = null } | ||
} | ||
|
||
override fun accept(intent: Int) { | ||
// no-op | ||
} | ||
|
||
override fun init() { | ||
// no-op | ||
} | ||
|
||
override fun dispose() { | ||
// no-op | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...ines/src/commonTest/kotlin/com/arkivanov/mvikotlin/extensions/coroutines/LabelFlowTest.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,77 @@ | ||
package com.arkivanov.mvikotlin.extensions.coroutines | ||
|
||
import com.arkivanov.mvikotlin.core.rx.Disposable | ||
import com.arkivanov.mvikotlin.core.rx.Observer | ||
import com.arkivanov.mvikotlin.core.store.Store | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.launch | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertNull | ||
|
||
@Suppress("TestFunctionName") | ||
class LabelFlowTest { | ||
|
||
@Test | ||
fun WHEN_label_emitted_THEN_label_collected() { | ||
val store = TestStore() | ||
val flow = store.labels | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val items = ArrayList<Int>() | ||
|
||
scope.launch { | ||
flow.collect { items += it } | ||
} | ||
|
||
store.labelObserver?.onNext(1) | ||
store.labelObserver?.onNext(2) | ||
store.labelObserver?.onNext(3) | ||
|
||
assertContentEquals(listOf(1, 2, 3), items) | ||
} | ||
|
||
@Test | ||
fun WHEN_collection_cancelled_THEN_unsubscribed_from_store() { | ||
val store = TestStore() | ||
val flow = store.labels | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
|
||
scope.launch { | ||
flow.collect {} | ||
} | ||
|
||
scope.cancel() | ||
|
||
assertNull(store.labelObserver) | ||
} | ||
|
||
private class TestStore : Store<Int, Int, Int> { | ||
override val state: Int = 0 | ||
override val isDisposed: Boolean = false | ||
|
||
var labelObserver: Observer<Int>? = null | ||
private set | ||
|
||
override fun states(observer: Observer<Int>): Disposable = error("Not required") | ||
|
||
override fun labels(observer: Observer<Int>): Disposable { | ||
labelObserver = observer | ||
|
||
return Disposable { labelObserver = null } | ||
} | ||
|
||
override fun accept(intent: Int) { | ||
// no-op | ||
} | ||
|
||
override fun init() { | ||
// no-op | ||
} | ||
|
||
override fun dispose() { | ||
// no-op | ||
} | ||
} | ||
} |