TestCoroutineUtils: add more cases

This commit is contained in:
Him188 2021-04-16 16:56:37 +08:00
parent 9cb75bd410
commit 76ab9f220e

View File

@ -12,8 +12,10 @@ package net.mamoe.mirai.console.util
import kotlinx.coroutines.* import kotlinx.coroutines.*
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import java.util.concurrent.atomic.AtomicInteger
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.time.milliseconds
import kotlin.time.seconds import kotlin.time.seconds
internal class TestCoroutineUtils { internal class TestCoroutineUtils {
@ -54,18 +56,20 @@ internal class TestCoroutineUtils {
fun `test launchTimedTask finishes multiple times`() = runBlocking { fun `test launchTimedTask finishes multiple times`() = runBlocking {
val scope = CoroutineScope(SupervisorJob()) val scope = CoroutineScope(SupervisorJob())
withTimeout(10000) { val resumedTimes = AtomicInteger(0)
suspendCancellableCoroutine<Unit> { cont -> val task = scope.launchTimedTask(3000.milliseconds.toLongMilliseconds()) {
val task = scope.launchTimedTask(3.seconds.toLongMilliseconds()) { resumedTimes.incrementAndGet()
cont.resume(Unit)
}
task.setChanged()
launch {
delay(4000)
task.setChanged()
}
}
} }
task.setChanged()
launch {
delay(4000)
task.setChanged()
}
delay(6000)
assertEquals(1, resumedTimes.get())
delay(15000)
assertEquals(2, resumedTimes.get())
scope.cancel() scope.cancel()
} }