blob: 9904d3d12c4d90363f9505c70cf305f857a3f5d4 [file] [log] [blame]
Vsevolod Tolstopyatovcd006432018-04-26 16:03:40 +03001
2@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
3
4package kotlinx.coroutines.experimental
5
6import kotlinx.coroutines.experimental.timeunit.*
7import kotlin.coroutines.experimental.*
8import kotlin.test.*
9
10class DelayTest : TestBase() {
11
12 @Test
13 fun testCancellation() = runTest(expected = {it is JobCancellationException}) {
14 runAndCancel(3600, TimeUnit.SECONDS)
15 }
16
17 @Test
18 fun testMaxLongValue()= runTest(expected = {it is JobCancellationException}) {
19 runAndCancel(Long.MAX_VALUE)
20 }
21
22 @Test
23 fun testMaxIntValue()= runTest(expected = {it is JobCancellationException}) {
24 runAndCancel(Int.MAX_VALUE.toLong())
25 }
26
27 @Test
28 fun testOverflowOnUnitConversion()= runTest(expected = {it is JobCancellationException}) {
29 runAndCancel(Long.MAX_VALUE, TimeUnit.SECONDS)
30 }
31
32 @Test
33 fun testRegularDelay() = runTest {
34 val deferred = async(coroutineContext) {
35 expect(2)
36 delay(1)
37 expect(3)
38 }
39
40 expect(1)
41 yield()
42 deferred.await()
43 finish(4)
44 }
45
46 private suspend fun runAndCancel(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) {
47 expect(1)
48 val deferred = async(coroutineContext) {
49 expect(2)
50 delay(time, unit)
51 expectUnreached()
52 }
53
54 yield()
55 expect(3)
56 require(deferred.isActive)
57 deferred.cancel()
58 finish(4)
59 deferred.await()
60 }
61}