1 /* 2 * Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines 6 7 import kotlinx.atomicfu.* 8 import kotlin.coroutines.* 9 import kotlin.test.* 10 11 class MultithreadedDispatcherStressTest { 12 val shared = atomic(0) 13 14 /** 15 * Tests that [newFixedThreadPoolContext] will not drop tasks when closed. 16 */ 17 @Test testClosingNotDroppingTasksnull18 fun testClosingNotDroppingTasks() { 19 repeat(7) { 20 shared.value = 0 21 val nThreads = it + 1 22 val dispatcher = newFixedThreadPoolContext(nThreads, "testMultiThreadedContext") 23 repeat(1_000) { 24 dispatcher.dispatch(EmptyCoroutineContext, Runnable { 25 shared.incrementAndGet() 26 }) 27 } 28 dispatcher.close() 29 while (shared.value < 1_000) { 30 // spin. 31 // the test will hang here if the dispatcher drops tasks. 32 } 33 } 34 } 35 } 36