Skip to content

Commit

Permalink
Make TimeoutException not a CancellationException
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhalanskyjb committed Dec 3, 2022
1 parent 7267280 commit 0d818f4
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 24 deletions.
1 change: 1 addition & 0 deletions kotlinx-coroutines-core/common/src/JobSupport.kt
Expand Up @@ -8,6 +8,7 @@ package kotlinx.coroutines
import kotlinx.atomicfu.*
import kotlinx.coroutines.internal.*
import kotlinx.coroutines.selects.*
import kotlinx.coroutines.time.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
import kotlin.js.*
Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/common/src/time/Timeout.kt
Expand Up @@ -106,7 +106,7 @@ private class TimeoutCoroutine<U, in T: U>(
public class TimeoutException internal constructor(
message: String,
@JvmField @Transient internal val coroutine: Job?
) : CancellationException(message), CopyableThrowable<TimeoutException> {
): IllegalStateException(message), CopyableThrowable<TimeoutException> {
/**
* Creates a timeout exception with the given message.
* This constructor is needed for exception stack-traces recovery.
Expand Down
Expand Up @@ -74,7 +74,7 @@ class WithTimeoutDurationTest : TestBase() {
*/
@Test
fun testYieldBlockingWithTimeout() = runTest(
expected = { it is CancellationException }
expected = { it is TimeoutException }
) {
kotlinx.coroutines.time.withTimeout(100.milliseconds) {
while (true) {
Expand Down Expand Up @@ -126,15 +126,15 @@ class WithTimeoutDurationTest : TestBase() {
expectUnreached()
"OK"
}
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
assertEquals("Timed out waiting for 100 ms", e.message)
finish(3)
}
}

@Test
fun testSuppressExceptionWithResult() = runTest(
expected = { it is CancellationException }
expected = { it is TimeoutException }
) {
expect(1)
kotlinx.coroutines.time.withTimeout(100.milliseconds) {
Expand All @@ -157,7 +157,7 @@ class WithTimeoutDurationTest : TestBase() {
expect(2)
try {
delay(1000.milliseconds)
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(3)
throw TestException()
}
Expand Down
Expand Up @@ -103,7 +103,7 @@ class WithTimeoutOrNullDurationTest : TestBase() {

@Test
fun testInnerTimeout() = runTest(
expected = { it is CancellationException }
expected = { it is TimeoutException }
) {
withTimeoutOrNull(1000.milliseconds) {
kotlinx.coroutines.time.withTimeout(10.milliseconds) {
Expand Down Expand Up @@ -184,7 +184,7 @@ class WithTimeoutOrNullDurationTest : TestBase() {
expect(2)
try {
delay(1000.milliseconds)
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(3)
}
"OK"
Expand All @@ -201,7 +201,7 @@ class WithTimeoutOrNullDurationTest : TestBase() {
expect(2)
try {
delay(1000.milliseconds)
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(3)
throw TestException()
}
Expand Down
6 changes: 3 additions & 3 deletions kotlinx-coroutines-core/common/test/WithTimeoutOrNullTest.kt
Expand Up @@ -100,7 +100,7 @@ class WithTimeoutOrNullTest : TestBase() {

@Test
fun testInnerTimeout() = runTest(
expected = { it is CancellationException }
expected = { it is TimeoutException }
) {
withTimeoutOrNull(1000) {
kotlinx.coroutines.time.withTimeout(10) {
Expand Down Expand Up @@ -175,7 +175,7 @@ class WithTimeoutOrNullTest : TestBase() {
expect(2)
try {
delay(1000)
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(3)
}
"OK"
Expand All @@ -192,7 +192,7 @@ class WithTimeoutOrNullTest : TestBase() {
expect(2)
try {
delay(1000)
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(3)
throw TestException()
}
Expand Down
10 changes: 5 additions & 5 deletions kotlinx-coroutines-core/common/test/WithTimeoutTest.kt
Expand Up @@ -72,7 +72,7 @@ class WithTimeoutTest : TestBase() {
*/
@Test
fun testYieldBlockingWithTimeout() = runTest(
expected = { it is CancellationException }
expected = { it is TimeoutException }
) {
kotlinx.coroutines.time.withTimeout(100) {
while (true) {
Expand Down Expand Up @@ -118,22 +118,22 @@ class WithTimeoutTest : TestBase() {
expectUnreached()
"OK"
}
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
assertEquals("Timed out waiting for 100 ms", e.message)
finish(3)
}
}

@Test
fun testSuppressExceptionWithResult() = runTest(
expected = { it is CancellationException }
expected = { it is TimeoutException }
) {
expect(1)
kotlinx.coroutines.time.withTimeout(100) {
expect(2)
try {
delay(1000)
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
finish(3)
}
"OK"
Expand All @@ -149,7 +149,7 @@ class WithTimeoutTest : TestBase() {
expect(2)
try {
delay(1000)
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(3)
throw TestException()
}
Expand Down
Expand Up @@ -4,6 +4,7 @@

package kotlinx.coroutines

import kotlinx.coroutines.time.*
import kotlin.test.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
Expand Down Expand Up @@ -67,7 +68,7 @@ class WithTimeoutOrNullThreadDispatchTest : TestBase() {
expect(3)
delay(1000)
expectUnreached()
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(4)
assertEquals(thread, Thread.currentThread())
throw e // rethrow
Expand All @@ -79,4 +80,4 @@ class WithTimeoutOrNullThreadDispatchTest : TestBase() {
}
finish(6)
}
}
}
Expand Up @@ -4,6 +4,7 @@

package kotlinx.coroutines

import kotlinx.coroutines.time.*
import kotlin.test.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
Expand Down Expand Up @@ -68,13 +69,13 @@ class WithTimeoutThreadDispatchTest : TestBase() {
expect(3)
delay(1000)
expectUnreached()
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(4)
assertEquals(thread, Thread.currentThread())
throw e // rethrow
}
}
} catch (e: CancellationException) {
} catch (e: TimeoutException) {
expect(5)
assertEquals(thread, Thread.currentThread())
}
Expand Down
Expand Up @@ -19,6 +19,7 @@ class StackTraceRecoveryWithTimeoutTest : TestBase() {
try {
outerWithTimeout()
} catch (e: TimeoutException) {
println(e.stackTraceToString())
verifyStackTrace("timeout/${name.methodName}", e)
}
}
Expand Down
13 changes: 9 additions & 4 deletions ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt
Expand Up @@ -61,9 +61,14 @@ class HandlerDispatcherTest : TestBase() {
mainLooper.pause()
assertFalse { mainLooper.scheduler.areAnyRunnable() }
val job = launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
kotlinx.coroutines.time.withTimeout(1) {
expect(1)
hang { expect(3) }
try {
kotlinx.coroutines.time.withTimeout(1) {
expect(1)
hang { expect(3) }
}
} catch (e: kotlinx.coroutines.time.TimeoutException) {
expect(4)
throw CancellationException("successfully timed out", e)
}
expectUnreached()
}
Expand All @@ -72,7 +77,7 @@ class HandlerDispatcherTest : TestBase() {
// Schedule cancellation
mainLooper.runToEndOfTasks()
job.join()
finish(4)
finish(5)
}

@Test
Expand Down

0 comments on commit 0d818f4

Please sign in to comment.