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