blob: 8f126d557c483e4b12db565dc8d4c6727b8b491b [file] [log] [blame]
Roman Elizarovc0d71dc2017-12-21 22:12:43 +03001
Roman Elizarova7db8ec2017-12-21 22:45:12 +03002/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03003 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarova7db8ec2017-12-21 22:45:12 +03004 */
5
Roman Elizarovc0d71dc2017-12-21 22:12:43 +03006@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
7
8package kotlinx.coroutines.experimental
9
10import kotlin.test.*
11
Roman Elizarovaa461cf2018-04-11 13:20:29 +030012class WithTimeoutTest : TestBase() {
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030013 /**
14 * Tests a case of no timeout and no suspension inside.
15 */
16 @Test
17 fun testBasicNoSuspend() = runTest {
18 expect(1)
19 val result = withTimeout(10_000) {
20 expect(2)
21 "OK"
22 }
23 assertEquals("OK", result)
24 finish(3)
25 }
26
27 /**
28 * Tests a case of no timeout and one suspension inside.
29 */
30 @Test
31 fun testBasicSuspend() = runTest {
32 expect(1)
33 val result = withTimeout(10_000) {
34 expect(2)
35 yield()
36 expect(3)
37 "OK"
38 }
39 assertEquals("OK", result)
40 finish(4)
41 }
42
43 /**
44 * Tests proper dispatching of `withTimeout` blocks
45 */
46 @Test
47 fun testDispatch() = runTest {
48 expect(1)
Roman Elizarov6e3ffb12018-09-14 13:46:58 +030049 launch {
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030050 expect(4)
51 yield() // back to main
52 expect(7)
53 }
54 expect(2)
55 // test that it does not yield to the above job when started
56 val result = withTimeout(1000) {
57 expect(3)
58 yield() // yield only now
59 expect(5)
60 "OK"
61 }
62 assertEquals("OK", result)
63 expect(6)
64 yield() // back to launch
65 finish(8)
66 }
67
68
69 /**
70 * Tests that a 100% CPU-consuming loop will react on timeout if it has yields.
71 */
72 @Test
73 fun testYieldBlockingWithTimeout() = runTest(
74 expected = { it is CancellationException }
75 ) {
76 withTimeout(100) {
77 while (true) {
78 yield()
79 }
80 }
81 }
82
83 /**
84 * Tests that [withTimeout] waits for children coroutines to complete.
85 */
86 @Test
87 fun testWithTimeoutChildWait() = runTest {
88 expect(1)
89 withTimeout(100) {
90 expect(2)
91 // launch child with timeout
Roman Elizarov6e3ffb12018-09-14 13:46:58 +030092 launch {
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030093 expect(4)
94 }
95 expect(3)
96 // now will wait for child before returning
97 }
98 finish(5)
99 }
Konrad KamiƄskiea4cd452018-02-01 17:18:14 +0100100
101 @Test
102 fun testBadClass() = runTest {
103 val bad = BadClass()
104 val result = withTimeout(100) {
105 bad
106 }
107 assertSame(bad, result)
108 }
109
110 class BadClass {
111 override fun equals(other: Any?): Boolean = error("Should not be called")
112 override fun hashCode(): Int = error("Should not be called")
113 override fun toString(): String = error("Should not be called")
114 }
Roman Elizarovaa461cf2018-04-11 13:20:29 +0300115
116 @Test
117 fun testExceptionOnTimeout() = runTest {
118 expect(1)
119 try {
120 withTimeout(100) {
121 expect(2)
122 delay(1000)
123 expectUnreached()
124 "OK"
125 }
126 } catch (e: CancellationException) {
Roman Elizarov27b8f452018-09-20 21:23:41 +0300127 assertEquals("Timed out waiting for 100 ms", e.message)
Roman Elizarovaa461cf2018-04-11 13:20:29 +0300128 finish(3)
129 }
130 }
131
132 @Test
133 fun testSuppressExceptionWithResult() = runTest(
134 expected = { it is CancellationException }
135 ) {
136 expect(1)
Vsevolod Tolstopyatov87f2faa2018-04-30 22:53:02 +0300137 withTimeout(100) {
Roman Elizarovaa461cf2018-04-11 13:20:29 +0300138 expect(2)
139 try {
140 delay(1000)
141 } catch (e: CancellationException) {
142 finish(3)
143 }
144 "OK"
145 }
146 expectUnreached()
147 }
148
149 @Test
Roman Elizarov541a9b62018-09-25 23:23:34 +0300150 fun testSuppressExceptionWithAnotherException() = runTest{
Roman Elizarovaa461cf2018-04-11 13:20:29 +0300151 expect(1)
Roman Elizarov541a9b62018-09-25 23:23:34 +0300152 try {
153 withTimeout(100) {
154 expect(2)
155 try {
156 delay(1000)
157 } catch (e: CancellationException) {
158 expect(3)
159 throw TestException()
160 }
161 expectUnreached()
162 "OK"
Roman Elizarovaa461cf2018-04-11 13:20:29 +0300163 }
164 expectUnreached()
Roman Elizarov541a9b62018-09-25 23:23:34 +0300165 } catch (e: TestException) {
166 finish(4)
Roman Elizarovaa461cf2018-04-11 13:20:29 +0300167 }
Roman Elizarovaa461cf2018-04-11 13:20:29 +0300168 }
169
170 private class TestException : Exception()
Vsevolod Tolstopyatov4cb5d192018-04-11 17:42:16 +0300171
172 @Test
173 fun testNegativeTimeout() = runTest {
174 expect(1)
175 try {
176 withTimeout(-1) {
177 expectUnreached()
178 "OK"
179 }
180 } catch (e: CancellationException) {
181 assertEquals("Timed out immediately", e.message)
182 finish(2)
183 }
184 }
Roman Elizarovc0d71dc2017-12-21 22:12:43 +0300185}
186