blob: d6a82680f3c2f1326b209ec4734f0e4c1dec9d26 [file] [log] [blame]
Roman Elizarovf16fd272017-02-07 11:26:00 +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
Roman Elizarov67891d82017-01-23 16:47:20 +030017package kotlinx.coroutines.experimental
18
Roman Elizarov67891d82017-01-23 16:47:20 +030019import org.junit.Test
20import java.util.concurrent.Executors
21
Roman Elizarov45181042017-07-20 20:37:51 +030022class ExecutorsTest : TestBase() {
Roman Elizarov67891d82017-01-23 16:47:20 +030023 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 Elizarovd9ae2bc2017-10-20 17:36:56 +080034 context.close()
Roman Elizarov67891d82017-01-23 16:47:20 +030035 }
36
37 @Test
38 fun testFixedThreadPool() {
39 val context = newFixedThreadPoolContext(2, "TestPool")
40 runBlocking(context) {
41 checkThreadName("TestPool")
42 }
Roman Elizarovd9ae2bc2017-10-20 17:36:56 +080043 context.close()
Roman Elizarov67891d82017-01-23 16:47:20 +030044 }
45
46 @Test
47 fun testToExecutor() {
48 val executor = Executors.newSingleThreadExecutor { r -> Thread(r, "TestExecutor") }
Roman Elizarov38b5ea12017-03-09 12:03:39 +030049 runBlocking(executor.asCoroutineDispatcher()) {
Roman Elizarov67891d82017-01-23 16:47:20 +030050 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 Elizarovf9e13f52017-12-21 12:23:15 +030061 withContext(ctx2) {
Roman Elizarov67891d82017-01-23 16:47:20 +030062 checkThreadName("Ctx2")
63 }
64 checkThreadName("Ctx1")
65 }
Roman Elizarovd9ae2bc2017-10-20 17:36:56 +080066 ctx1.close()
67 ctx2.close()
Roman Elizarov67891d82017-01-23 16:47:20 +030068 }
Marko Devcic26f4b9e2018-03-20 20:28:52 +010069
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 Elizarov67891d82017-01-23 16:47:20 +030080}