Skip to content

Commit 58ad0cd

Browse files
committed
Make exception after cancellation test more robust
Ensure that the RX error handler actually gets invoked
1 parent 3cc9b94 commit 58ad0cd

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

reactive/kotlinx-coroutines-rx2/test/ObservableTest.kt

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
package kotlinx.coroutines.rx2
66

77
import io.reactivex.*
8-
import io.reactivex.plugins.*
8+
import io.reactivex.exceptions.*
99
import kotlinx.coroutines.*
1010
import kotlinx.coroutines.CancellationException
1111
import org.junit.*
1212
import org.junit.Test
1313
import java.util.concurrent.*
14+
import java.util.concurrent.atomic.*
1415
import kotlin.test.*
1516

1617
class ObservableTest : TestBase() {
@@ -140,24 +141,26 @@ class ObservableTest : TestBase() {
140141

141142
@Test
142143
fun testExceptionAfterCancellation() {
143-
// Test that no exceptions were reported to the global EH (it will fail the test if so)
144-
val handler = { e: Throwable ->
145-
assertFalse(e is CancellationException)
144+
// Test that no exceptions were reported to the coroutine EH (it will fail the test if so)
145+
val coroutineExceptionHandler = CoroutineExceptionHandler { _, _ -> expectUnreached() }
146+
val rxExceptionHandlerInvocations = AtomicInteger()
147+
val rxExceptionHandler: (Throwable) -> Unit = { error ->
148+
assertTrue(error is UndeliverableException)
149+
assertTrue(error.cause is TestException)
150+
rxExceptionHandlerInvocations.getAndIncrement()
146151
}
147-
withExceptionHandler(handler) {
148-
RxJavaPlugins.setErrorHandler {
149-
require(it !is CancellationException)
150-
}
152+
withExceptionHandler(rxExceptionHandler) {
151153
Observable
152154
.interval(1, TimeUnit.MILLISECONDS)
153155
.take(1000)
154156
.switchMapSingle {
155-
rxSingle {
157+
rxSingle(coroutineExceptionHandler) {
156158
timeBomb().await()
157159
}
158160
}
159161
.blockingSubscribe({}, {})
160162
}
163+
assertTrue(rxExceptionHandlerInvocations.get() > 0)
161164
}
162165

163166
private fun timeBomb() = Single.timer(1, TimeUnit.MILLISECONDS).doOnSuccess { throw TestException() }

reactive/kotlinx-coroutines-rx3/test/ObservableTest.kt

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
package kotlinx.coroutines.rx3
66

77
import io.reactivex.rxjava3.core.*
8-
import io.reactivex.rxjava3.plugins.*
8+
import io.reactivex.rxjava3.exceptions.*
99
import kotlinx.coroutines.*
1010
import kotlinx.coroutines.CancellationException
1111
import org.junit.*
1212
import org.junit.Test
1313
import java.util.concurrent.*
14+
import java.util.concurrent.atomic.*
1415
import kotlin.test.*
1516

1617
class ObservableTest : TestBase() {
@@ -140,24 +141,26 @@ class ObservableTest : TestBase() {
140141

141142
@Test
142143
fun testExceptionAfterCancellation() {
143-
// Test that no exceptions were reported to the global EH (it will fail the test if so)
144-
val handler = { e: Throwable ->
145-
assertFalse(e is CancellationException)
144+
// Test that no exceptions were reported to the coroutine EH (it will fail the test if so)
145+
val coroutineExceptionHandler = CoroutineExceptionHandler { _, _ -> expectUnreached() }
146+
val rxExceptionHandlerInvocations = AtomicInteger()
147+
val rxExceptionHandler: (Throwable) -> Unit = { error ->
148+
assertTrue(error is UndeliverableException)
149+
assertTrue(error.cause is TestException)
150+
rxExceptionHandlerInvocations.getAndIncrement()
146151
}
147-
withExceptionHandler(handler) {
148-
RxJavaPlugins.setErrorHandler {
149-
require(it !is CancellationException)
150-
}
152+
withExceptionHandler(rxExceptionHandler) {
151153
Observable
152154
.interval(1, TimeUnit.MILLISECONDS)
153155
.take(1000)
154156
.switchMapSingle {
155-
rxSingle {
157+
rxSingle(coroutineExceptionHandler) {
156158
timeBomb().await()
157159
}
158160
}
159161
.blockingSubscribe({}, {})
160162
}
163+
assertTrue(rxExceptionHandlerInvocations.get() > 0)
161164
}
162165

163166
private fun timeBomb() = Single.timer(1, TimeUnit.MILLISECONDS).doOnSuccess { throw TestException() }

0 commit comments

Comments
 (0)