Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package kotlinx.coroutines.experimental.channels |
| 18 | |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 20 | import org.hamcrest.core.* |
| 21 | import org.junit.* |
| 22 | import org.junit.Assert.* |
| 23 | import kotlin.coroutines.experimental.* |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 24 | |
| 25 | class ActorLazyTest : TestBase() { |
| 26 | @Test |
| 27 | fun testEmptyStart() = runBlocking<Unit> { |
| 28 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 29 | val actor = actor<String>(coroutineContext, start = CoroutineStart.LAZY) { |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 30 | expect(5) |
| 31 | } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 32 | actor as Job // type assertion |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 33 | assertThat(actor.isActive, IsEqual(false)) |
| 34 | assertThat(actor.isCompleted, IsEqual(false)) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 35 | assertThat(actor.isClosedForSend, IsEqual(false)) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 36 | expect(2) |
| 37 | yield() // to actor code --> nothing happens (not started!) |
| 38 | assertThat(actor.isActive, IsEqual(false)) |
| 39 | assertThat(actor.isCompleted, IsEqual(false)) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 40 | assertThat(actor.isClosedForSend, IsEqual(false)) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 41 | 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 Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 48 | assertThat(actor.isClosedForSend, IsEqual(true)) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 49 | finish(6) |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | fun testOne() = runBlocking<Unit> { |
| 54 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 55 | val actor = actor<String>(coroutineContext, start = CoroutineStart.LAZY) { |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 56 | expect(4) |
| 57 | assertThat(receive(), IsEqual("OK")) |
| 58 | expect(5) |
| 59 | } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 60 | actor as Job // type assertion |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 61 | assertThat(actor.isActive, IsEqual(false)) |
| 62 | assertThat(actor.isCompleted, IsEqual(false)) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 63 | assertThat(actor.isClosedForSend, IsEqual(false)) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 64 | expect(2) |
| 65 | yield() // to actor code --> nothing happens (not started!) |
| 66 | assertThat(actor.isActive, IsEqual(false)) |
| 67 | assertThat(actor.isCompleted, IsEqual(false)) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 68 | assertThat(actor.isClosedForSend, IsEqual(false)) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 69 | 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 Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 74 | assertThat(actor.isClosedForSend, IsEqual(true)) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 75 | finish(6) |
| 76 | } |
| 77 | } |