• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines
6 
7 import kotlin.test.*
8 
9 class SetTimeoutDispatcherTest : TestBase() {
10     @Test
<lambda>null11     fun testDispatch() = runTest {
12         launch(SetTimeoutDispatcher) {
13             expect(1)
14             launch {
15                 expect(3)
16             }
17             expect(2)
18             yield()
19             expect(4)
20         }.join()
21         finish(5)
22     }
23 
24     @Test
<lambda>null25     fun testDelay() = runTest {
26         withContext(SetTimeoutDispatcher) {
27             val job = launch(SetTimeoutDispatcher) {
28                 expect(2)
29                 delay(100)
30                 expect(4)
31             }
32             expect(1)
33             yield() // Yield uses microtask, so should be in the same context
34             expect(3)
35             job.join()
36             finish(5)
37         }
38     }
39 
40     @Test
<lambda>null41     fun testWithTimeout() = runTest {
42         withContext(SetTimeoutDispatcher) {
43             val result = withTimeoutOrNull(10) {
44                 expect(1)
45                 delay(100)
46                 expectUnreached()
47                 42
48             }
49             assertNull(result)
50             finish(2)
51         }
52     }
53 }
54