blob: d3b2ff12659fd09119a4e05b7dd636702e9d52f5 (
plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.channels
import kotlinx.coroutines.*
import org.junit.Test
import kotlin.test.*
class ActorLazyTest : TestBase() {
@Test
fun testEmptyStart() = runBlocking {
expect(1)
val actor = actor<String>(start = CoroutineStart.LAZY) {
expect(5)
}
actor as Job // type assertion
assertFalse(actor.isActive)
assertFalse(actor.isCompleted)
assertFalse(actor.isClosedForSend)
expect(2)
yield() // to actor code --> nothing happens (not started!)
assertFalse(actor.isActive)
assertFalse(actor.isCompleted)
assertFalse(actor.isClosedForSend)
expect(3)
// start actor explicitly
actor.start()
expect(4)
yield() // to started actor
assertFalse(actor.isActive)
assertTrue(actor.isCompleted)
assertTrue(actor.isClosedForSend)
finish(6)
}
@Test
fun testOne() = runBlocking {
expect(1)
val actor = actor<String>(start = CoroutineStart.LAZY) {
expect(4)
assertEquals("OK", receive())
expect(5)
}
actor as Job // type assertion
assertFalse(actor.isActive)
assertFalse(actor.isCompleted)
assertFalse(actor.isClosedForSend)
expect(2)
yield() // to actor code --> nothing happens (not started!)
assertFalse(actor.isActive)
assertFalse(actor.isCompleted)
assertFalse(actor.isClosedForSend)
expect(3)
// send message to actor --> should start it
actor.send("OK")
assertFalse(actor.isActive)
assertTrue(actor.isCompleted)
assertTrue(actor.isClosedForSend)
finish(6)
}
@Test
fun testCloseFreshActor() = runTest {
val job = launch {
expect(2)
val actor = actor<Int>(start = CoroutineStart.LAZY) {
expect(3)
for (i in channel) { }
expect(4)
}
actor.close()
}
expect(1)
job.join()
finish(5)
}
@Test
fun testCancelledParent() = runTest({ it is CancellationException }) {
cancel()
expect(1)
actor<Int>(start = CoroutineStart.LAZY) {
expectUnreached()
}
finish(2)
}
}
|