1 package kotlinx.coroutines 2 3 import kotlinx.coroutines.testing.* 4 import kotlinx.coroutines.channels.* 5 import kotlinx.coroutines.selects.* 6 import kotlin.test.* 7 8 class BuilderContractsTest : TestBase() { 9 10 @Test <lambda>null11 fun testContracts() = runTest { 12 // Coroutine scope 13 val cs: Int 14 coroutineScope { 15 cs = 42 16 } 17 consume(cs) 18 19 // Supervisor scope 20 val svs: Int 21 supervisorScope { 22 svs = 21 23 } 24 consume(svs) 25 26 // with context scope 27 val wctx: Int 28 withContext(Dispatchers.Unconfined) { 29 wctx = 239 30 } 31 consume(wctx) 32 33 val wt: Int 34 withTimeout(Long.MAX_VALUE) { 35 wt = 123 36 } 37 consume(wt) 38 39 val s: Int 40 select<Unit> { 41 s = 42 42 Job().apply { complete() }.onJoin {} 43 } 44 consume(s) 45 46 47 val ch: Int 48 val i = Channel<Int>() 49 i.consume { 50 ch = 321 51 } 52 consume(ch) 53 } 54 consumenull55 private fun consume(a: Int) { 56 /* 57 * Verify the value is actually set correctly 58 * (non-zero, VerificationError is not triggered, can be read) 59 */ 60 assertNotEquals(0, a) 61 assertEquals(a.hashCode(), a) 62 } 63 } 64