Skip to content

Commit 0d818f4

Browse files
committed
Make TimeoutException not a CancellationException
1 parent 7267280 commit 0d818f4

10 files changed

+33
-24
lines changed

kotlinx-coroutines-core/common/src/JobSupport.kt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package kotlinx.coroutines
88
import kotlinx.atomicfu.*
99
import kotlinx.coroutines.internal.*
1010
import kotlinx.coroutines.selects.*
11+
import kotlinx.coroutines.time.*
1112
import kotlin.coroutines.*
1213
import kotlin.coroutines.intrinsics.*
1314
import kotlin.js.*

kotlinx-coroutines-core/common/src/time/Timeout.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private class TimeoutCoroutine<U, in T: U>(
106106
public class TimeoutException internal constructor(
107107
message: String,
108108
@JvmField @Transient internal val coroutine: Job?
109-
) : CancellationException(message), CopyableThrowable<TimeoutException> {
109+
): IllegalStateException(message), CopyableThrowable<TimeoutException> {
110110
/**
111111
* Creates a timeout exception with the given message.
112112
* This constructor is needed for exception stack-traces recovery.

kotlinx-coroutines-core/common/test/WithTimeoutDurationTest.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class WithTimeoutDurationTest : TestBase() {
7474
*/
7575
@Test
7676
fun testYieldBlockingWithTimeout() = runTest(
77-
expected = { it is CancellationException }
77+
expected = { it is TimeoutException }
7878
) {
7979
kotlinx.coroutines.time.withTimeout(100.milliseconds) {
8080
while (true) {
@@ -126,15 +126,15 @@ class WithTimeoutDurationTest : TestBase() {
126126
expectUnreached()
127127
"OK"
128128
}
129-
} catch (e: CancellationException) {
129+
} catch (e: TimeoutException) {
130130
assertEquals("Timed out waiting for 100 ms", e.message)
131131
finish(3)
132132
}
133133
}
134134

135135
@Test
136136
fun testSuppressExceptionWithResult() = runTest(
137-
expected = { it is CancellationException }
137+
expected = { it is TimeoutException }
138138
) {
139139
expect(1)
140140
kotlinx.coroutines.time.withTimeout(100.milliseconds) {
@@ -157,7 +157,7 @@ class WithTimeoutDurationTest : TestBase() {
157157
expect(2)
158158
try {
159159
delay(1000.milliseconds)
160-
} catch (e: CancellationException) {
160+
} catch (e: TimeoutException) {
161161
expect(3)
162162
throw TestException()
163163
}

kotlinx-coroutines-core/common/test/WithTimeoutOrNullDurationTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class WithTimeoutOrNullDurationTest : TestBase() {
103103

104104
@Test
105105
fun testInnerTimeout() = runTest(
106-
expected = { it is CancellationException }
106+
expected = { it is TimeoutException }
107107
) {
108108
withTimeoutOrNull(1000.milliseconds) {
109109
kotlinx.coroutines.time.withTimeout(10.milliseconds) {
@@ -184,7 +184,7 @@ class WithTimeoutOrNullDurationTest : TestBase() {
184184
expect(2)
185185
try {
186186
delay(1000.milliseconds)
187-
} catch (e: CancellationException) {
187+
} catch (e: TimeoutException) {
188188
expect(3)
189189
}
190190
"OK"
@@ -201,7 +201,7 @@ class WithTimeoutOrNullDurationTest : TestBase() {
201201
expect(2)
202202
try {
203203
delay(1000.milliseconds)
204-
} catch (e: CancellationException) {
204+
} catch (e: TimeoutException) {
205205
expect(3)
206206
throw TestException()
207207
}

kotlinx-coroutines-core/common/test/WithTimeoutOrNullTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class WithTimeoutOrNullTest : TestBase() {
100100

101101
@Test
102102
fun testInnerTimeout() = runTest(
103-
expected = { it is CancellationException }
103+
expected = { it is TimeoutException }
104104
) {
105105
withTimeoutOrNull(1000) {
106106
kotlinx.coroutines.time.withTimeout(10) {
@@ -175,7 +175,7 @@ class WithTimeoutOrNullTest : TestBase() {
175175
expect(2)
176176
try {
177177
delay(1000)
178-
} catch (e: CancellationException) {
178+
} catch (e: TimeoutException) {
179179
expect(3)
180180
}
181181
"OK"
@@ -192,7 +192,7 @@ class WithTimeoutOrNullTest : TestBase() {
192192
expect(2)
193193
try {
194194
delay(1000)
195-
} catch (e: CancellationException) {
195+
} catch (e: TimeoutException) {
196196
expect(3)
197197
throw TestException()
198198
}

kotlinx-coroutines-core/common/test/WithTimeoutTest.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class WithTimeoutTest : TestBase() {
7272
*/
7373
@Test
7474
fun testYieldBlockingWithTimeout() = runTest(
75-
expected = { it is CancellationException }
75+
expected = { it is TimeoutException }
7676
) {
7777
kotlinx.coroutines.time.withTimeout(100) {
7878
while (true) {
@@ -118,22 +118,22 @@ class WithTimeoutTest : TestBase() {
118118
expectUnreached()
119119
"OK"
120120
}
121-
} catch (e: CancellationException) {
121+
} catch (e: TimeoutException) {
122122
assertEquals("Timed out waiting for 100 ms", e.message)
123123
finish(3)
124124
}
125125
}
126126

127127
@Test
128128
fun testSuppressExceptionWithResult() = runTest(
129-
expected = { it is CancellationException }
129+
expected = { it is TimeoutException }
130130
) {
131131
expect(1)
132132
kotlinx.coroutines.time.withTimeout(100) {
133133
expect(2)
134134
try {
135135
delay(1000)
136-
} catch (e: CancellationException) {
136+
} catch (e: TimeoutException) {
137137
finish(3)
138138
}
139139
"OK"
@@ -149,7 +149,7 @@ class WithTimeoutTest : TestBase() {
149149
expect(2)
150150
try {
151151
delay(1000)
152-
} catch (e: CancellationException) {
152+
} catch (e: TimeoutException) {
153153
expect(3)
154154
throw TestException()
155155
}

kotlinx-coroutines-core/jvm/test/WithTimeoutOrNullThreadDispatchTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.coroutines
66

7+
import kotlinx.coroutines.time.*
78
import kotlin.test.*
89
import java.util.concurrent.ExecutorService
910
import java.util.concurrent.Executors
@@ -67,7 +68,7 @@ class WithTimeoutOrNullThreadDispatchTest : TestBase() {
6768
expect(3)
6869
delay(1000)
6970
expectUnreached()
70-
} catch (e: CancellationException) {
71+
} catch (e: TimeoutException) {
7172
expect(4)
7273
assertEquals(thread, Thread.currentThread())
7374
throw e // rethrow
@@ -79,4 +80,4 @@ class WithTimeoutOrNullThreadDispatchTest : TestBase() {
7980
}
8081
finish(6)
8182
}
82-
}
83+
}

kotlinx-coroutines-core/jvm/test/WithTimeoutThreadDispatchTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.coroutines
66

7+
import kotlinx.coroutines.time.*
78
import kotlin.test.*
89
import java.util.concurrent.ExecutorService
910
import java.util.concurrent.Executors
@@ -68,13 +69,13 @@ class WithTimeoutThreadDispatchTest : TestBase() {
6869
expect(3)
6970
delay(1000)
7071
expectUnreached()
71-
} catch (e: CancellationException) {
72+
} catch (e: TimeoutException) {
7273
expect(4)
7374
assertEquals(thread, Thread.currentThread())
7475
throw e // rethrow
7576
}
7677
}
77-
} catch (e: CancellationException) {
78+
} catch (e: TimeoutException) {
7879
expect(5)
7980
assertEquals(thread, Thread.currentThread())
8081
}

kotlinx-coroutines-core/jvm/test/exceptions/StackTraceRecoveryWithTimeoutTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class StackTraceRecoveryWithTimeoutTest : TestBase() {
1919
try {
2020
outerWithTimeout()
2121
} catch (e: TimeoutException) {
22+
println(e.stackTraceToString())
2223
verifyStackTrace("timeout/${name.methodName}", e)
2324
}
2425
}

ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt

+9-4
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ class HandlerDispatcherTest : TestBase() {
6161
mainLooper.pause()
6262
assertFalse { mainLooper.scheduler.areAnyRunnable() }
6363
val job = launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
64-
kotlinx.coroutines.time.withTimeout(1) {
65-
expect(1)
66-
hang { expect(3) }
64+
try {
65+
kotlinx.coroutines.time.withTimeout(1) {
66+
expect(1)
67+
hang { expect(3) }
68+
}
69+
} catch (e: kotlinx.coroutines.time.TimeoutException) {
70+
expect(4)
71+
throw CancellationException("successfully timed out", e)
6772
}
6873
expectUnreached()
6974
}
@@ -72,7 +77,7 @@ class HandlerDispatcherTest : TestBase() {
7277
// Schedule cancellation
7378
mainLooper.runToEndOfTasks()
7479
job.join()
75-
finish(4)
80+
finish(5)
7681
}
7782

7883
@Test

0 commit comments

Comments
 (0)