Skip to content

Commit c0d71dc

Browse files
committed
MPP: More common tests, using kotlin.test.assertTrue & runTest dsl
1 parent e3f2884 commit c0d71dc

18 files changed

+445
-437
lines changed

common/kotlinx-coroutines-core-common/src/test/kotlin/kotlinx/coroutines/experimental/CommonAsyncLazyTest.kt

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class CommonAsyncLazyTest : TestBase() {
2929
42
3030
}
3131
expect(2)
32-
check(!d.isActive && !d.isCompleted)
33-
check(d.await() == 42)
34-
check(!d.isActive && d.isCompleted && !d.isCompletedExceptionally)
32+
assertTrue(!d.isActive && !d.isCompleted)
33+
assertTrue(d.await() == 42)
34+
assertTrue(!d.isActive && d.isCompleted && !d.isCompletedExceptionally)
3535
expect(4)
36-
check(d.await() == 42) // second await -- same result
36+
assertTrue(d.await() == 42) // second await -- same result
3737
finish(5)
3838
}
3939

@@ -47,11 +47,11 @@ class CommonAsyncLazyTest : TestBase() {
4747
42
4848
}
4949
expect(2)
50-
check(!d.isActive && !d.isCompleted)
51-
check(d.await() == 42)
52-
check(!d.isActive && d.isCompleted && !d.isCompletedExceptionally)
50+
assertTrue(!d.isActive && !d.isCompleted)
51+
assertTrue(d.await() == 42)
52+
assertTrue(!d.isActive && d.isCompleted && !d.isCompletedExceptionally)
5353
expect(5)
54-
check(d.await() == 42) // second await -- same result
54+
assertTrue(d.await() == 42) // second await -- same result
5555
finish(6)
5656
}
5757

@@ -63,22 +63,22 @@ class CommonAsyncLazyTest : TestBase() {
6363
42
6464
}
6565
expect(2)
66-
check(!d.isActive && !d.isCompleted)
66+
assertTrue(!d.isActive && !d.isCompleted)
6767
launch(coroutineContext) { // see how it looks from another coroutine
6868
expect(4)
69-
check(!d.isActive && !d.isCompleted)
69+
assertTrue(!d.isActive && !d.isCompleted)
7070
yield() // yield back to main
7171
expect(6)
72-
check(d.isActive && !d.isCompleted) // implicitly started by main's await
72+
assertTrue(d.isActive && !d.isCompleted) // implicitly started by main's await
7373
yield() // yield to d
7474
}
7575
expect(3)
76-
check(!d.isActive && !d.isCompleted)
76+
assertTrue(!d.isActive && !d.isCompleted)
7777
yield() // yield to second child (lazy async is not computing yet)
7878
expect(5)
79-
check(!d.isActive && !d.isCompleted)
80-
check(d.await() == 42) // starts computing
81-
check(!d.isActive && d.isCompleted && !d.isCompletedExceptionally)
79+
assertTrue(!d.isActive && !d.isCompleted)
80+
assertTrue(d.await() == 42) // starts computing
81+
assertTrue(!d.isActive && d.isCompleted && !d.isCompletedExceptionally)
8282
finish(8)
8383
}
8484

@@ -92,7 +92,7 @@ class CommonAsyncLazyTest : TestBase() {
9292
throw TestException()
9393
}
9494
expect(2)
95-
check(!d.isActive && !d.isCompleted)
95+
assertTrue(!d.isActive && !d.isCompleted)
9696
d.await() // will throw IOException
9797
}
9898

@@ -108,7 +108,7 @@ class CommonAsyncLazyTest : TestBase() {
108108
throw TestException()
109109
}
110110
expect(2)
111-
check(!d.isActive && !d.isCompleted)
111+
assertTrue(!d.isActive && !d.isCompleted)
112112
d.await() // will throw IOException
113113
}
114114

@@ -120,11 +120,11 @@ class CommonAsyncLazyTest : TestBase() {
120120
throw TestException()
121121
}
122122
expect(2)
123-
check(!d.isActive && !d.isCompleted)
123+
assertTrue(!d.isActive && !d.isCompleted)
124124
try {
125125
d.await() // will throw IOException
126126
} catch (e: TestException) {
127-
check(!d.isActive && d.isCompleted && d.isCompletedExceptionally && !d.isCancelled)
127+
assertTrue(!d.isActive && d.isCompleted && d.isCompletedExceptionally && !d.isCancelled)
128128
expect(4)
129129
}
130130
finish(5)
@@ -138,15 +138,15 @@ class CommonAsyncLazyTest : TestBase() {
138138
42
139139
}
140140
expect(2)
141-
check(!d.isActive && !d.isCompleted)
142-
check(d.start())
143-
check(d.isActive && !d.isCompleted)
141+
assertTrue(!d.isActive && !d.isCompleted)
142+
assertTrue(d.start())
143+
assertTrue(d.isActive && !d.isCompleted)
144144
expect(3)
145-
check(!d.start())
145+
assertTrue(!d.start())
146146
yield() // yield to started coroutine
147-
check(!d.isActive && d.isCompleted && !d.isCompletedExceptionally) // and it finishes
147+
assertTrue(!d.isActive && d.isCompleted && !d.isCompletedExceptionally) // and it finishes
148148
expect(5)
149-
check(d.await() == 42) // await sees result
149+
assertTrue(d.await() == 42) // await sees result
150150
finish(6)
151151
}
152152

@@ -160,13 +160,13 @@ class CommonAsyncLazyTest : TestBase() {
160160
42
161161
}
162162
expect(2)
163-
check(!d.isActive && !d.isCompleted)
164-
check(d.cancel())
165-
check(!d.isActive && d.isCompleted && d.isCompletedExceptionally && d.isCancelled)
166-
check(!d.cancel())
167-
check(!d.start())
163+
assertTrue(!d.isActive && !d.isCompleted)
164+
assertTrue(d.cancel())
165+
assertTrue(!d.isActive && d.isCompleted && d.isCompletedExceptionally && d.isCancelled)
166+
assertTrue(!d.cancel())
167+
assertTrue(!d.start())
168168
finish(3)
169-
check(d.await() == 42) // await shall throw CancellationException
169+
assertTrue(d.await() == 42) // await shall throw CancellationException
170170
expectUnreached()
171171
}
172172

@@ -182,19 +182,19 @@ class CommonAsyncLazyTest : TestBase() {
182182
42
183183
}
184184
expect(2)
185-
check(!d.isActive && !d.isCompleted && !d.isCancelled)
186-
check(d.start())
187-
check(d.isActive && !d.isCompleted && !d.isCancelled)
185+
assertTrue(!d.isActive && !d.isCompleted && !d.isCancelled)
186+
assertTrue(d.start())
187+
assertTrue(d.isActive && !d.isCompleted && !d.isCancelled)
188188
expect(3)
189189
yield() // yield to d
190190
expect(5)
191-
check(d.isActive && !d.isCompleted && !d.isCancelled)
192-
check(d.cancel())
193-
check(!d.isActive && !d.isCompletedExceptionally && d.isCancelled) // cancelling !
194-
check(!d.cancel())
195-
check(!d.isActive && !d.isCompletedExceptionally && d.isCancelled) // still cancelling
191+
assertTrue(d.isActive && !d.isCompleted && !d.isCancelled)
192+
assertTrue(d.cancel())
193+
assertTrue(!d.isActive && !d.isCompletedExceptionally && d.isCancelled) // cancelling !
194+
assertTrue(!d.cancel())
195+
assertTrue(!d.isActive && !d.isCompletedExceptionally && d.isCancelled) // still cancelling
196196
finish(6)
197-
check(d.await() == 42) // await shall throw CancellationException
197+
assertTrue(d.await() == 42) // await shall throw CancellationException
198198
expectUnreached()
199199
}
200200

common/kotlinx-coroutines-core-common/src/test/kotlin/kotlinx/coroutines/experimental/CommonAsyncTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class CommonAsyncTest : TestBase() {
2929
42
3030
}
3131
expect(2)
32-
check(d.isActive)
33-
check(d.await() == 42)
34-
check(!d.isActive)
32+
assertTrue(d.isActive)
33+
assertTrue(d.await() == 42)
34+
assertTrue(!d.isActive)
3535
expect(4)
36-
check(d.await() == 42) // second await -- same result
36+
assertTrue(d.await() == 42) // second await -- same result
3737
finish(5)
3838
}
3939

@@ -45,8 +45,8 @@ class CommonAsyncTest : TestBase() {
4545
42
4646
}
4747
expect(3)
48-
check(!d.isActive)
49-
check(d.await() == 42)
48+
assertTrue(!d.isActive)
49+
assertTrue(d.await() == 42)
5050
finish(4)
5151
}
5252

@@ -90,13 +90,13 @@ class CommonAsyncTest : TestBase() {
9090
expect(2)
9191
launch(coroutineContext) {
9292
expect(6)
93-
check(d.await() == 42)
93+
assertTrue(d.await() == 42)
9494
expect(11)
9595
}
9696
expect(3)
9797
launch(coroutineContext) {
9898
expect(7)
99-
check(d.await() == 42)
99+
assertTrue(d.await() == 42)
100100
expect(12)
101101
}
102102
expect(4)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package kotlinx.coroutines.experimental
2+
3+
import kotlin.test.*
4+
5+
class CommonAtomicCancellationTest : TestBase() {
6+
@Test
7+
fun testCancellableLaunch() = runBlocking {
8+
expect(1)
9+
val job = launch(coroutineContext) {
10+
expectUnreached() // will get cancelled before start
11+
}
12+
expect(2)
13+
job.cancel()
14+
finish(3)
15+
}
16+
17+
@Test
18+
fun testAtomicLaunch() = runBlocking {
19+
expect(1)
20+
val job = launch(coroutineContext, start = CoroutineStart.ATOMIC) {
21+
finish(4) // will execute even after it was cancelled
22+
}
23+
expect(2)
24+
job.cancel()
25+
expect(3)
26+
}
27+
28+
@Test
29+
fun testDeferredAwaitCancellable() = runBlocking {
30+
expect(1)
31+
val deferred = async(coroutineContext) { // deferred, not yet complete
32+
expect(4)
33+
"OK"
34+
}
35+
assertEquals(false, deferred.isCompleted)
36+
var job: Job? = null
37+
launch(coroutineContext) { // will cancel job as soon as deferred completes
38+
expect(5)
39+
assertEquals(true, deferred.isCompleted)
40+
job!!.cancel()
41+
}
42+
job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
43+
expect(2)
44+
try {
45+
deferred.await() // suspends
46+
expectUnreached() // will not execute -- cancelled while dispatched
47+
} finally {
48+
finish(7) // but will execute finally blocks
49+
}
50+
}
51+
expect(3) // continues to execute when job suspends
52+
yield() // to deferred & canceller
53+
expect(6)
54+
}
55+
56+
@Test
57+
fun testJobJoinCancellable() = runBlocking {
58+
expect(1)
59+
val jobToJoin = launch(coroutineContext) { // not yet complete
60+
expect(4)
61+
}
62+
assertEquals(false, jobToJoin.isCompleted)
63+
var job: Job? = null
64+
launch(coroutineContext) { // will cancel job as soon as jobToJoin completes
65+
expect(5)
66+
assertEquals(true, jobToJoin.isCompleted)
67+
job!!.cancel()
68+
}
69+
job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
70+
expect(2)
71+
try {
72+
jobToJoin.join() // suspends
73+
expectUnreached() // will not execute -- cancelled while dispatched
74+
} finally {
75+
finish(7) // but will execute finally blocks
76+
}
77+
}
78+
expect(3) // continues to execute when job suspends
79+
yield() // to jobToJoin & canceller
80+
expect(6)
81+
}
82+
}

common/kotlinx-coroutines-core-common/src/test/kotlin/kotlinx/coroutines/experimental/CommonCompletableDeferredTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class CommonCompletableDeferredTest : TestBase() {
157157
}
158158

159159
@Test
160-
fun testAwait() = runBlocking {
160+
fun testAwait() = runTest {
161161
expect(1)
162162
val c = CompletableDeferred<String>()
163163
launch(coroutineContext, CoroutineStart.UNDISPATCHED) {

common/kotlinx-coroutines-core-common/src/test/kotlin/kotlinx/coroutines/experimental/CommonCoroutineExceptionHandlerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import kotlin.test.*
44

55
class CommonCoroutineExceptionHandlerTest : TestBase() {
66
@Test
7-
fun testCoroutineExceptionHandlerCreator() = runBlocking {
7+
fun testCoroutineExceptionHandlerCreator() = runTest {
88
expect(1)
99
var coroutineException: Throwable? = null
1010
val handler = CoroutineExceptionHandler { _, ex ->
@@ -17,7 +17,7 @@ class CommonCoroutineExceptionHandlerTest : TestBase() {
1717
expect(2)
1818
job.join()
1919
finish(4)
20-
check(coroutineException is TestException)
20+
assertTrue(coroutineException is TestException)
2121
}
2222

2323
private class TestException: RuntimeException()

0 commit comments

Comments
 (0)