• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download

<lambda>null1 package kotlinx.coroutines
2 
3 import kotlinx.coroutines.testing.*
4 import org.junit.*
5 import org.junit.Test
6 import java.util.concurrent.*
7 import kotlin.test.*
8 
9 class UnconfinedConcurrentStressTest : TestBase() {
10     private val threads = 4
11     private val executor = newFixedThreadPoolContext(threads, "UnconfinedConcurrentStressTest")
12     private val threadLocal = ThreadLocal<Int>()
13 
14     @After
15     fun tearDown() {
16         executor.close()
17     }
18 
19     @Test
20     fun testConcurrent() = runTest {
21         val iterations = 1_000 * stressTestMultiplier
22         val startBarrier = CyclicBarrier(threads + 1)
23         val finishLatch = CountDownLatch(threads)
24 
25         repeat(threads) { id ->
26             launch(executor) {
27                 startBarrier.await()
28                 repeat(iterations) {
29                     threadLocal.set(0)
30                     launch(Dispatchers.Unconfined) {
31                         assertEquals(0, threadLocal.get())
32                         launch(Dispatchers.Unconfined) {
33                             assertEquals(id, threadLocal.get())
34                         }
35 
36                         threadLocal.set(id)
37                     }
38                 }
39 
40                 finishLatch.countDown()
41             }
42         }
43 
44         startBarrier.await()
45         finishLatch.await()
46     }
47 }
48