From 5e0841305dc8eb4ba59df0ee6bf75c5a9d60bc16 Mon Sep 17 00:00:00 2001 From: Dmitry Khalanskiy Date: Tue, 28 Jan 2025 13:00:41 +0100 Subject: [PATCH] Fix tests where coroutine outlive the test body --- .../common/test/ParentCancellationTest.kt | 1 + .../BlockingCoroutineDispatcherTerminationStressTest.kt | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/kotlinx-coroutines-core/common/test/ParentCancellationTest.kt b/kotlinx-coroutines-core/common/test/ParentCancellationTest.kt index 43f77a5c3d..30a53209d7 100644 --- a/kotlinx-coroutines-core/common/test/ParentCancellationTest.kt +++ b/kotlinx-coroutines-core/common/test/ParentCancellationTest.kt @@ -158,6 +158,7 @@ class ParentCancellationTest : TestBase() { } if (expectParentActive) { assertTrue(parent.isActive) + parent.cancelAndJoin() } else { parent.join() assertFalse(parent.isActive) diff --git a/kotlinx-coroutines-core/jvm/test/scheduling/BlockingCoroutineDispatcherTerminationStressTest.kt b/kotlinx-coroutines-core/jvm/test/scheduling/BlockingCoroutineDispatcherTerminationStressTest.kt index f9e93abe15..ccb219186b 100644 --- a/kotlinx-coroutines-core/jvm/test/scheduling/BlockingCoroutineDispatcherTerminationStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/scheduling/BlockingCoroutineDispatcherTerminationStressTest.kt @@ -19,14 +19,18 @@ class BlockingCoroutineDispatcherTerminationStressTest : TestBase() { baseDispatcher.close() } + /** + * Tests that when threads are created to accommodate the new tasks, but then don't receive any tasks for the + * duration of their terminate-on-idling timeout, liveness does not suffer. + */ @Test - fun testTermination() { + fun testTermination() = runTest { val rnd = Random() val deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(TEST_SECONDS) while (System.currentTimeMillis() < deadline) { Thread.sleep(rnd.nextInt(30).toLong()) repeat(rnd.nextInt(5) + 1) { - GlobalScope.launch(ioDispatcher) { + launch(ioDispatcher) { Thread.sleep(rnd.nextInt(5).toLong()) } }