From 76ab9f220eb9188920e73251131460d2e4a6fac4 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 16 Apr 2021 16:56:37 +0800 Subject: [PATCH] TestCoroutineUtils: add more cases --- .../test/util/TestCoroutineUtils.kt | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/backend/mirai-console/test/util/TestCoroutineUtils.kt b/backend/mirai-console/test/util/TestCoroutineUtils.kt index eae33eb1a..a2ce37eb9 100644 --- a/backend/mirai-console/test/util/TestCoroutineUtils.kt +++ b/backend/mirai-console/test/util/TestCoroutineUtils.kt @@ -12,8 +12,10 @@ package net.mamoe.mirai.console.util import kotlinx.coroutines.* import org.junit.jupiter.api.Test +import java.util.concurrent.atomic.AtomicInteger import kotlin.coroutines.resume import kotlin.test.assertEquals +import kotlin.time.milliseconds import kotlin.time.seconds internal class TestCoroutineUtils { @@ -54,18 +56,20 @@ internal class TestCoroutineUtils { fun `test launchTimedTask finishes multiple times`() = runBlocking { val scope = CoroutineScope(SupervisorJob()) - withTimeout(10000) { - suspendCancellableCoroutine { cont -> - val task = scope.launchTimedTask(3.seconds.toLongMilliseconds()) { - cont.resume(Unit) - } - task.setChanged() - launch { - delay(4000) - task.setChanged() - } - } + val resumedTimes = AtomicInteger(0) + val task = scope.launchTimedTask(3000.milliseconds.toLongMilliseconds()) { + resumedTimes.incrementAndGet() } + task.setChanged() + launch { + delay(4000) + task.setChanged() + } + + delay(6000) + assertEquals(1, resumedTimes.get()) + delay(15000) + assertEquals(2, resumedTimes.get()) scope.cancel() }