blob: 4a06251772f146d7c5393b77bcc26e2bf2b58fe8 [file] [log] [blame]
Roman Elizarovecda27f2017-04-06 23:06:26 +03001/*
2 * Copyright 2016-2017 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package kotlinx.coroutines.experimental.channels
18
Roman Elizarovb555d912017-08-17 21:01:33 +030019import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030020import org.hamcrest.core.*
21import org.junit.*
22import org.junit.Assert.*
23import kotlin.coroutines.experimental.*
Roman Elizarovecda27f2017-04-06 23:06:26 +030024
25class ActorLazyTest : TestBase() {
26 @Test
27 fun testEmptyStart() = runBlocking<Unit> {
28 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030029 val actor = actor<String>(coroutineContext, start = CoroutineStart.LAZY) {
Roman Elizarovecda27f2017-04-06 23:06:26 +030030 expect(5)
31 }
Roman Elizarovb555d912017-08-17 21:01:33 +030032 actor as Job // type assertion
Roman Elizarovecda27f2017-04-06 23:06:26 +030033 assertThat(actor.isActive, IsEqual(false))
34 assertThat(actor.isCompleted, IsEqual(false))
Roman Elizarovb555d912017-08-17 21:01:33 +030035 assertThat(actor.isClosedForSend, IsEqual(false))
Roman Elizarovecda27f2017-04-06 23:06:26 +030036 expect(2)
37 yield() // to actor code --> nothing happens (not started!)
38 assertThat(actor.isActive, IsEqual(false))
39 assertThat(actor.isCompleted, IsEqual(false))
Roman Elizarovb555d912017-08-17 21:01:33 +030040 assertThat(actor.isClosedForSend, IsEqual(false))
Roman Elizarovecda27f2017-04-06 23:06:26 +030041 expect(3)
42 // start actor explicitly
43 actor.start()
44 expect(4)
45 yield() // to started actor
46 assertThat(actor.isActive, IsEqual(false))
47 assertThat(actor.isCompleted, IsEqual(true))
Roman Elizarovb555d912017-08-17 21:01:33 +030048 assertThat(actor.isClosedForSend, IsEqual(true))
Roman Elizarovecda27f2017-04-06 23:06:26 +030049 finish(6)
50 }
51
52 @Test
53 fun testOne() = runBlocking<Unit> {
54 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030055 val actor = actor<String>(coroutineContext, start = CoroutineStart.LAZY) {
Roman Elizarovecda27f2017-04-06 23:06:26 +030056 expect(4)
57 assertThat(receive(), IsEqual("OK"))
58 expect(5)
59 }
Roman Elizarovb555d912017-08-17 21:01:33 +030060 actor as Job // type assertion
Roman Elizarovecda27f2017-04-06 23:06:26 +030061 assertThat(actor.isActive, IsEqual(false))
62 assertThat(actor.isCompleted, IsEqual(false))
Roman Elizarovb555d912017-08-17 21:01:33 +030063 assertThat(actor.isClosedForSend, IsEqual(false))
Roman Elizarovecda27f2017-04-06 23:06:26 +030064 expect(2)
65 yield() // to actor code --> nothing happens (not started!)
66 assertThat(actor.isActive, IsEqual(false))
67 assertThat(actor.isCompleted, IsEqual(false))
Roman Elizarovb555d912017-08-17 21:01:33 +030068 assertThat(actor.isClosedForSend, IsEqual(false))
Roman Elizarovecda27f2017-04-06 23:06:26 +030069 expect(3)
70 // send message to actor --> should start it
71 actor.send("OK")
72 assertThat(actor.isActive, IsEqual(false))
73 assertThat(actor.isCompleted, IsEqual(true))
Roman Elizarovb555d912017-08-17 21:01:33 +030074 assertThat(actor.isClosedForSend, IsEqual(true))
Roman Elizarovecda27f2017-04-06 23:06:26 +030075 finish(6)
76 }
77}