<lambda>null1package kotlinx.coroutines.guava 2 3 import kotlinx.coroutines.testing.* 4 import com.google.common.util.concurrent.* 5 import kotlinx.coroutines.* 6 import org.junit.* 7 import org.junit.Test 8 import kotlin.test.* 9 10 class FutureAsDeferredUnhandledCompletionExceptionTest : TestBase() { 11 12 // This is a separate test in order to avoid interference with uncaught exception handlers in other tests 13 private val exceptionHandler = Thread.getDefaultUncaughtExceptionHandler() 14 private lateinit var caughtException: Throwable 15 16 @Before 17 fun setUp() { 18 Thread.setDefaultUncaughtExceptionHandler { _, e -> caughtException = e } 19 } 20 21 @After 22 fun tearDown() { 23 Thread.setDefaultUncaughtExceptionHandler(exceptionHandler) 24 } 25 26 @Test 27 fun testLostExceptionOnSuccess() = runTest { 28 val future = SettableFuture.create<Int>() 29 val deferred = future.asDeferred() 30 deferred.invokeOnCompletion { throw TestException() } 31 future.set(1) 32 assertTrue { caughtException is CompletionHandlerException && caughtException.cause is TestException } 33 } 34 35 @Test 36 fun testLostExceptionOnFailure() = runTest { 37 val future = SettableFuture.create<Int>() 38 val deferred = future.asDeferred() 39 deferred.invokeOnCompletion { throw TestException() } 40 future.setException(TestException2()) 41 assertTrue { caughtException is CompletionHandlerException && caughtException.cause is TestException } 42 } 43 } 44