• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.coroutines.*
8 import kotlin.test.*
9 
10 class JobExtensionsTest : TestBase() {
11 
12     private val job = Job()
13     private val scope = CoroutineScope(job + CoroutineExceptionHandler { _, _ ->  })
14 
15     @Test
16     fun testIsActive() = runTest {
17         expect(1)
18         scope.launch(Dispatchers.Unconfined) {
19             ensureActive()
20             coroutineContext.ensureActive()
21             coroutineContext[Job]!!.ensureActive()
22             expect(2)
23             delay(Long.MAX_VALUE)
24         }
25 
26         expect(3)
27         job.ensureActive()
28         scope.ensureActive()
29         scope.coroutineContext.ensureActive()
30         job.cancelAndJoin()
31         finish(4)
32     }
33 
34     @Test
35     fun testIsCompleted() = runTest {
36         expect(1)
37         scope.launch(Dispatchers.Unconfined) {
38             ensureActive()
39             coroutineContext.ensureActive()
40             coroutineContext[Job]!!.ensureActive()
41             expect(2)
42         }
43 
44         expect(3)
45         job.complete()
46         job.join()
47         assertFailsWith<JobCancellationException> { job.ensureActive() }
48         assertFailsWith<JobCancellationException> { scope.ensureActive() }
49         assertFailsWith<JobCancellationException> { scope.coroutineContext.ensureActive() }
50         finish(4)
51     }
52 
53 
54     @Test
55     fun testIsCancelled() = runTest {
56         expect(1)
57         scope.launch(Dispatchers.Unconfined) {
58             ensureActive()
59             coroutineContext.ensureActive()
60             coroutineContext[Job]!!.ensureActive()
61             expect(2)
62             throw TestException()
63         }
64 
65         expect(3)
66         checkException { job.ensureActive() }
67         checkException { scope.ensureActive() }
68         checkException { scope.coroutineContext.ensureActive() }
69         finish(4)
70     }
71 
72     @Test
73     fun testEnsureActiveWithEmptyContext() = runTest {
74         withEmptyContext {
75             ensureActive() // should not do anything
76         }
77     }
78 
79     private inline fun checkException(block: () -> Unit) {
80         val result = runCatching(block)
81         val exception = result.exceptionOrNull() ?: fail()
82         assertTrue(exception is JobCancellationException)
83         assertTrue(exception.cause is TestException)
84     }
85 
86     @Test
87     fun testJobExtension() = runTest {
88         assertSame(coroutineContext[Job]!!, coroutineContext.job)
89         assertSame(NonCancellable, NonCancellable.job)
90         assertSame(job, job.job)
91         assertFailsWith<IllegalStateException> { EmptyCoroutineContext.job }
92         assertFailsWith<IllegalStateException> { Dispatchers.Default.job }
93         assertFailsWith<IllegalStateException> { (Dispatchers.Default + CoroutineName("")).job }
94     }
95 }
96