-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathBuilderContractsTest.kt
66 lines (55 loc) · 1.36 KB
/
BuilderContractsTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.selects.*
import kotlin.test.*
class BuilderContractsTest : TestBase() {
@Test
fun testContracts() = runTest {
// Coroutine scope
val cs: Int
coroutineScope {
cs = 42
}
consume(cs)
// Supervisor scope
val svs: Int
supervisorScope {
svs = 21
}
consume(svs)
// with context scope
val wctx: Int
withContext(Dispatchers.Unconfined) {
wctx = 239
}
consume(wctx)
val wt: Int
kotlinx.coroutines.time.withTimeout(Long.MAX_VALUE) {
wt = 123
}
consume(wt)
val s: Int
select<Unit> {
s = 42
Job().apply { complete() }.onJoin {}
}
consume(s)
val ch: Int
val i = Channel<Int>()
i.consume {
ch = 321
}
consume(ch)
}
private fun consume(a: Int) {
/*
* Verify the value is actually set correctly
* (non-zero, VerificationError is not triggered, can be read)
*/
assertNotEquals(0, a)
assertEquals(a.hashCode(), a)
}
}