Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +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 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental |
| 18 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 19 | import org.junit.Test |
| 20 | import java.util.concurrent.Executors |
| 21 | |
Roman Elizarov | 4518104 | 2017-07-20 20:37:51 +0300 | [diff] [blame] | 22 | class ExecutorsTest : TestBase() { |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 23 | fun checkThreadName(prefix: String) { |
| 24 | val name = Thread.currentThread().name |
| 25 | check(name.startsWith(prefix)) { "Expected thread name to start with '$prefix', found: '$name'" } |
| 26 | } |
| 27 | |
| 28 | @Test |
| 29 | fun testSingleThread() { |
| 30 | val context = newSingleThreadContext("TestThread") |
| 31 | runBlocking(context) { |
| 32 | checkThreadName("TestThread") |
| 33 | } |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 34 | context.close() |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @Test |
| 38 | fun testFixedThreadPool() { |
| 39 | val context = newFixedThreadPoolContext(2, "TestPool") |
| 40 | runBlocking(context) { |
| 41 | checkThreadName("TestPool") |
| 42 | } |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 43 | context.close() |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Test |
| 47 | fun testToExecutor() { |
| 48 | val executor = Executors.newSingleThreadExecutor { r -> Thread(r, "TestExecutor") } |
Roman Elizarov | 38b5ea1 | 2017-03-09 12:03:39 +0300 | [diff] [blame] | 49 | runBlocking(executor.asCoroutineDispatcher()) { |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 50 | checkThreadName("TestExecutor") |
| 51 | } |
| 52 | executor.shutdown() |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun testTwoThreads() { |
| 57 | val ctx1 = newSingleThreadContext("Ctx1") |
| 58 | val ctx2 = newSingleThreadContext("Ctx2") |
| 59 | runBlocking(ctx1) { |
| 60 | checkThreadName("Ctx1") |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 61 | withContext(ctx2) { |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 62 | checkThreadName("Ctx2") |
| 63 | } |
| 64 | checkThreadName("Ctx1") |
| 65 | } |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 66 | ctx1.close() |
| 67 | ctx2.close() |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 68 | } |
Marko Devcic | 26f4b9e | 2018-03-20 20:28:52 +0100 | [diff] [blame] | 69 | |
| 70 | @Test |
| 71 | fun testShutdownExecutorService() { |
| 72 | val executorService = Executors.newSingleThreadExecutor { r -> Thread(r, "TestExecutor") } |
| 73 | val dispatcher = executorService.asCoroutineDispatcher() |
| 74 | runBlocking (dispatcher) { |
| 75 | checkThreadName("TestExecutor") |
| 76 | } |
| 77 | dispatcher.close() |
| 78 | check(executorService.isShutdown) |
| 79 | } |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 80 | } |