-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathAsFutureTest.kt
124 lines (112 loc) · 3.49 KB
/
AsFutureTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.future
import kotlinx.coroutines.*
import org.junit.*
import org.junit.Assert.*
import java.util.concurrent.*
import java.util.concurrent.CancellationException
class AsFutureTest : TestBase() {
@Test
fun testCompletedDeferredAsCompletableFuture() = runTest {
expect(1)
val deferred = async(start = CoroutineStart.UNDISPATCHED) {
expect(2) // completed right away
"OK"
}
expect(3)
val future = deferred.asCompletableFuture()
assertEquals("OK", future.await())
finish(4)
}
@Test
fun testCompletedJobAsCompletableFuture() = runTest {
val job = Job().apply { complete() }
val future = job.asCompletableFuture()
assertEquals(Unit, future.await())
}
@Test
fun testWaitForDeferredAsCompletableFuture() = runTest {
expect(1)
val deferred = async {
expect(3) // will complete later
"OK"
}
expect(2)
val future = deferred.asCompletableFuture()
assertEquals("OK", future.await()) // await yields main thread to deferred coroutine
finish(4)
}
@Test
fun testWaitForJobAsCompletableFuture() = runTest {
val job = Job()
val future = job.asCompletableFuture()
assertTrue(job.isActive)
job.complete()
assertFalse(job.isActive)
assertEquals(Unit, future.await())
}
@Test
fun testAsCompletableFutureThrowable() {
val deferred = GlobalScope.async<Unit> { throw OutOfMemoryError() }
val future = deferred.asCompletableFuture()
try {
expect(1)
future.get()
expectUnreached()
} catch (e: ExecutionException) {
assertTrue(future.isCompletedExceptionally)
assertTrue(e.cause is OutOfMemoryError)
finish(2)
}
}
@Test
fun testJobAsCompletableFutureThrowable() {
val job = Job()
CompletableDeferred<Unit>(parent = job).apply { completeExceptionally(OutOfMemoryError()) }
val future = job.asCompletableFuture()
try {
expect(1)
future.get()
expectUnreached()
} catch (e: ExecutionException) {
assertTrue(future.isCompletedExceptionally)
assertTrue(e.cause is OutOfMemoryError)
finish(2)
}
}
@Test
fun testJobAsCompletableFutureCancellation() {
val job = Job()
val future = job.asCompletableFuture()
job.cancel()
try {
expect(1)
future.get()
expectUnreached()
} catch (e: CancellationException) {
assertTrue(future.isCompletedExceptionally)
finish(2)
}
}
@Test
fun testJobCancellation() {
val job = Job()
val future = job.asCompletableFuture()
future.cancel(true)
assertTrue(job.isCancelled)
assertTrue(job.isCompleted)
assertFalse(job.isActive)
}
@Test
fun testDeferredCancellation() {
val deferred = CompletableDeferred<Int>()
val future = deferred.asCompletableFuture()
future.cancel(true)
assertTrue(deferred.isCancelled)
assertTrue(deferred.isCompleted)
assertFalse(deferred.isActive)
assertTrue(deferred.getCompletionExceptionOrNull() is CancellationException)
}
}