mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-06-02 16:05:15 +00:00
27 lines
919 B
Kotlin
27 lines
919 B
Kotlin
package dev.inmo.micro_utils.coroutines
|
|
|
|
import kotlinx.coroutines.*
|
|
import kotlin.test.Test
|
|
import kotlin.test.assertEquals
|
|
|
|
class DoWithFirstTests {
|
|
@Test
|
|
fun testHandleOneOf() {
|
|
val scope = CoroutineScope(Dispatchers.Default)
|
|
val happenedDeferreds = mutableListOf<Int>()
|
|
val deferredWhichMustHappen = (-1).asDeferred
|
|
scope.launchSynchronously {
|
|
scope.launch {
|
|
((0 until 100).map {
|
|
DeferredAction(
|
|
scope.async { delay(10000); it },
|
|
happenedDeferreds::add
|
|
)
|
|
} + DeferredAction(deferredWhichMustHappen, happenedDeferreds::add)).invokeFirstOf(scope)
|
|
}.join()
|
|
}
|
|
assertEquals(1, happenedDeferreds.size)
|
|
assertEquals(scope.launchSynchronously { deferredWhichMustHappen.await() }, happenedDeferreds.first())
|
|
}
|
|
}
|