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