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