1 /* 2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.time 6 7 import kotlinx.coroutines.* 8 import kotlinx.coroutines.selects.* 9 import org.junit.Test 10 import java.time.* 11 import java.time.temporal.* 12 import kotlin.test.* 13 14 class DurationOverflowTest : TestBase() { 15 <lambda>null16 private val durations = ChronoUnit.values().map { it.duration } 17 18 @Test <lambda>null19 fun testDelay() = runTest { 20 var counter = 0 21 for (duration in durations) { 22 expect(++counter) 23 delay(duration.negated()) // Instant bail out from negative values 24 launch(start = CoroutineStart.UNDISPATCHED) { 25 expect(++counter) 26 delay(duration) 27 }.cancelAndJoin() 28 expect(++counter) 29 } 30 31 finish(++counter) 32 } 33 34 @Test testOnTimeoutnull35 fun testOnTimeout() = runTest { 36 for (duration in durations) { 37 // Does not crash on overflows 38 select<Unit> { 39 onTimeout(duration) {} 40 onTimeout(duration.negated()) {} 41 } 42 } 43 } 44 45 @Test <lambda>null46 fun testWithTimeout() = runTest { 47 for (duration in durations) { 48 withTimeout(duration) {} 49 } 50 } 51 52 @Test <lambda>null53 fun testWithTimeoutOrNull() = runTest { 54 for (duration in durations) { 55 withTimeoutOrNull(duration) {} 56 } 57 } 58 59 @Test <lambda>null60 fun testWithTimeoutOrNullNegativeDuration() = runTest { 61 val result = withTimeoutOrNull(Duration.ofSeconds(1).negated()) { 62 1 63 } 64 65 assertNull(result) 66 } 67 68 @Test <lambda>null69 fun testZeroDurationWithTimeout() = runTest { 70 assertFailsWith<TimeoutCancellationException> { withTimeout(0L) {} } 71 assertFailsWith<TimeoutCancellationException> { withTimeout(Duration.ZERO) {} } 72 } 73 74 @Test <lambda>null75 fun testZeroDurationWithTimeoutOrNull() = runTest { 76 assertNull(withTimeoutOrNull(0L) {}) 77 assertNull(withTimeoutOrNull(Duration.ZERO) {}) 78 } 79 } 80