1
1
/*
2
- * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2
+ * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
3
*/
4
4
5
5
package kotlinx.coroutines
6
6
7
- import org.junit.Test
8
- import java.util.concurrent.Executors
7
+ import org.junit.*
8
+ import org.junit.Assert.*
9
+ import java.util.concurrent.*
10
+ import kotlin.coroutines.*
9
11
10
12
class ExecutorsTest : TestBase () {
11
13
private fun checkThreadName (prefix : String ) {
@@ -32,14 +34,49 @@ class ExecutorsTest : TestBase() {
32
34
}
33
35
34
36
@Test
35
- fun testToExecutor () {
37
+ fun testExecutorToDispatcher () {
36
38
val executor = Executors .newSingleThreadExecutor { r -> Thread (r, " TestExecutor" ) }
37
39
runBlocking(executor.asCoroutineDispatcher()) {
38
40
checkThreadName(" TestExecutor" )
39
41
}
40
42
executor.shutdown()
41
43
}
42
44
45
+ @Test
46
+ fun testConvertedDispatcherToExecutor () {
47
+ val executor: ExecutorService = Executors .newSingleThreadExecutor { r -> Thread (r, " TestExecutor" ) }
48
+ val dispatcher: CoroutineDispatcher = executor.asCoroutineDispatcher()
49
+ assertSame(executor, dispatcher.asExecutor())
50
+ executor.shutdown()
51
+ }
52
+
53
+ @Test
54
+ fun testDefaultDispatcherToExecutor () {
55
+ val latch = CountDownLatch (2 )
56
+ Dispatchers .Default .asExecutor().execute {
57
+ checkThreadName(" DefaultDispatcher" )
58
+ latch.countDown()
59
+ }
60
+ latch.countDown()
61
+ }
62
+
63
+ @Test
64
+ fun testCustomDispatcherToExecutor () {
65
+ expect(1 )
66
+ val dispatcher = object : CoroutineDispatcher () {
67
+ override fun dispatch (context : CoroutineContext , block : Runnable ) {
68
+ expect(2 )
69
+ block.run ()
70
+ }
71
+ }
72
+ val executor = dispatcher.asExecutor()
73
+ assertSame(dispatcher, executor.asCoroutineDispatcher())
74
+ executor.execute {
75
+ expect(3 )
76
+ }
77
+ finish(4 )
78
+ }
79
+
43
80
@Test
44
81
fun testTwoThreads () {
45
82
val ctx1 = newSingleThreadContext(" Ctx1" )
0 commit comments