• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines.guava
6 
7 import com.google.common.util.concurrent.*
8 import kotlinx.coroutines.*
9 import org.junit.*
10 import org.junit.Test
11 import kotlin.test.*
12 
13 class FutureAsDeferredUnhandledCompletionExceptionTest : TestBase() {
14 
15     // This is a separate test in order to avoid interference with uncaught exception handlers in other tests
16     private val exceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
17     private lateinit var caughtException: Throwable
18 
19     @Before
20     fun setUp() {
21         Thread.setDefaultUncaughtExceptionHandler { _, e -> caughtException = e }
22     }
23 
24     @After
25     fun tearDown() {
26         Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
27     }
28 
29     @Test
30     fun testLostExceptionOnSuccess() = runTest {
31         val future = SettableFuture.create<Int>()
32         val deferred = future.asDeferred()
33         deferred.invokeOnCompletion { throw TestException() }
34         future.set(1)
35         assertTrue { caughtException is CompletionHandlerException && caughtException.cause is TestException }
36     }
37 
38     @Test
39     fun testLostExceptionOnFailure() = runTest {
40         val future = SettableFuture.create<Int>()
41         val deferred = future.asDeferred()
42         deferred.invokeOnCompletion { throw TestException() }
43         future.setException(TestException2())
44         assertTrue { caughtException is CompletionHandlerException && caughtException.cause is TestException }
45     }
46 }
47