1 /* 2 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 package kotlinx.coroutines 5 6 import kotlinx.coroutines.channels.* 7 import kotlin.test.* 8 9 10 abstract class AbstractDispatcherConcurrencyTest : TestBase() { 11 12 public abstract val dispatcher: CoroutineDispatcher 13 14 @Test <lambda>null15 fun testLaunchAndJoin() = runMtTest { 16 expect(1) 17 var capturedMutableState = 0 18 val job = GlobalScope.launch(dispatcher) { 19 ++capturedMutableState 20 expect(2) 21 } 22 runBlocking { job.join() } 23 assertEquals(1, capturedMutableState) 24 finish(3) 25 } 26 27 @Test <lambda>null28 fun testDispatcherHasOwnThreads() = runMtTest { 29 val channel = Channel<Int>() 30 GlobalScope.launch(dispatcher) { 31 channel.send(42) 32 } 33 34 var result = ChannelResult.failure<Int>() 35 while (!result.isSuccess) { 36 result = channel.tryReceive() 37 // Block the thread, wait 38 } 39 // Delivery was successful, let's check it 40 assertEquals(42, result.getOrThrow()) 41 } 42 43 @Test <lambda>null44 fun testDelayInDispatcher() = runMtTest { 45 expect(1) 46 val job = GlobalScope.launch(dispatcher) { 47 expect(2) 48 delay(100) 49 expect(3) 50 } 51 runBlocking { job.join() } 52 finish(4) 53 } 54 } 55