Skip to content

Commit cb7f37b

Browse files
committed
Treat Duration.ZERO as 0L in jdk8 extensions
Fixes #1349
1 parent fe41869 commit cb7f37b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

integration/kotlinx-coroutines-jdk8/src/time/Time.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public suspend fun <T> withTimeoutOrNull(duration: Duration, block: suspend Coro
4646
* - Non-suspending fast-paths (e.g. `withTimeout(1 nanosecond) { 42 }` should not throw)
4747
*/
4848
private fun Duration.coerceToMillis(): Long {
49-
if (isNegative) return 0
49+
if (this <= Duration.ZERO) return 0
5050
if (this <= ChronoUnit.MILLIS.duration) return 1
5151

5252
// Maximum scalar values of Duration.ofMillis(Long.MAX_VALUE)

integration/kotlinx-coroutines-jdk8/test/time/DurationOverflowTest.kt

+11
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@ class DurationOverflowTest : TestBase() {
6565
assertNull(result)
6666
}
6767

68+
@Test
69+
fun testZeroDurationWithTimeout() = runTest {
70+
assertFailsWith<TimeoutCancellationException> { withTimeout(0L) {} }
71+
assertFailsWith<TimeoutCancellationException> { withTimeout(Duration.ZERO) {} }
72+
}
73+
74+
@Test
75+
fun testZeroDurationWithTimeoutOrNull() = runTest {
76+
assertNull(withTimeoutOrNull(0L) {})
77+
assertNull(withTimeoutOrNull(Duration.ZERO) {})
78+
}
6879
}

0 commit comments

Comments
 (0)