Skip to content

Commit 6e3ffb1

Browse files
committed
Structured concurrency in tests (removed explicit coroutineContext)
1 parent fa9586b commit 6e3ffb1

File tree

60 files changed

+341
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+341
-389
lines changed

common/kotlinx-coroutines-core-common/test/AsyncLazyTest.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
package kotlinx.coroutines.experimental
88

9-
import kotlin.coroutines.experimental.*
109
import kotlin.test.*
1110

1211
class AsyncLazyTest : TestBase() {
1312

1413
@Test
1514
fun testSimple() = runTest {
1615
expect(1)
17-
val d = async(coroutineContext, CoroutineStart.LAZY) {
16+
val d = async(start = CoroutineStart.LAZY) {
1817
expect(3)
1918
42
2019
}
@@ -30,7 +29,7 @@ class AsyncLazyTest : TestBase() {
3029
@Test
3130
fun testLazyDeferAndYield() = runTest {
3231
expect(1)
33-
val d = async(coroutineContext, CoroutineStart.LAZY) {
32+
val d = async(start = CoroutineStart.LAZY) {
3433
expect(3)
3534
yield() // this has not effect, because parent coroutine is waiting
3635
expect(4)
@@ -48,13 +47,13 @@ class AsyncLazyTest : TestBase() {
4847
@Test
4948
fun testLazyDeferAndYield2() = runTest {
5049
expect(1)
51-
val d = async(coroutineContext, CoroutineStart.LAZY) {
50+
val d = async(start = CoroutineStart.LAZY) {
5251
expect(7)
5352
42
5453
}
5554
expect(2)
5655
assertTrue(!d.isActive && !d.isCompleted)
57-
launch(coroutineContext) { // see how it looks from another coroutine
56+
launch { // see how it looks from another coroutine
5857
expect(4)
5958
assertTrue(!d.isActive && !d.isCompleted)
6059
yield() // yield back to main
@@ -77,7 +76,7 @@ class AsyncLazyTest : TestBase() {
7776
expected = { it is TestException }
7877
) {
7978
expect(1)
80-
val d = async(coroutineContext, CoroutineStart.LAZY) {
79+
val d = async(start = CoroutineStart.LAZY) {
8180
finish(3)
8281
throw TestException()
8382
}
@@ -91,7 +90,7 @@ class AsyncLazyTest : TestBase() {
9190
expected = { it is TestException }
9291
) {
9392
expect(1)
94-
val d = async(coroutineContext, CoroutineStart.LAZY) {
93+
val d = async(start = CoroutineStart.LAZY) {
9594
expect(3)
9695
yield() // this has not effect, because parent coroutine is waiting
9796
finish(4)
@@ -105,7 +104,7 @@ class AsyncLazyTest : TestBase() {
105104
@Test
106105
fun testCatchException() = runTest {
107106
expect(1)
108-
val d = async(coroutineContext, CoroutineStart.LAZY) {
107+
val d = async(start = CoroutineStart.LAZY) {
109108
expect(3)
110109
throw TestException()
111110
}
@@ -123,7 +122,7 @@ class AsyncLazyTest : TestBase() {
123122
@Test
124123
fun testStart() = runTest {
125124
expect(1)
126-
val d = async(coroutineContext, CoroutineStart.LAZY) {
125+
val d = async(start = CoroutineStart.LAZY) {
127126
expect(4)
128127
42
129128
}
@@ -145,7 +144,7 @@ class AsyncLazyTest : TestBase() {
145144
expected = { it is JobCancellationException }
146145
) {
147146
expect(1)
148-
val d = async(coroutineContext, CoroutineStart.LAZY) {
147+
val d = async(start = CoroutineStart.LAZY) {
149148
expectUnreached()
150149
42
151150
}
@@ -165,7 +164,7 @@ class AsyncLazyTest : TestBase() {
165164
expected = { it is CancellationException }
166165
) {
167166
expect(1)
168-
val d = async(coroutineContext, CoroutineStart.LAZY) {
167+
val d = async(start = CoroutineStart.LAZY) {
169168
expect(4)
170169
yield() // yield to main, that is going to cancel us
171170
expectUnreached()

common/kotlinx-coroutines-core-common/test/AsyncTest.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AsyncTest : TestBase() {
1313
@Test
1414
fun testSimple() = runTest {
1515
expect(1)
16-
val d = async(coroutineContext) {
16+
val d = async {
1717
expect(3)
1818
42
1919
}
@@ -29,7 +29,7 @@ class AsyncTest : TestBase() {
2929
@Test
3030
fun testUndispatched() = runTest {
3131
expect(1)
32-
val d = async(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
32+
val d = async(start = CoroutineStart.UNDISPATCHED) {
3333
expect(2)
3434
42
3535
}
@@ -42,7 +42,7 @@ class AsyncTest : TestBase() {
4242
@Test
4343
fun testSimpleException() = runTest(expected = { it is TestException }) {
4444
expect(1)
45-
val d = async(coroutineContext) {
45+
val d = async {
4646
finish(3)
4747
throw TestException()
4848
}
@@ -53,7 +53,7 @@ class AsyncTest : TestBase() {
5353
@Test
5454
fun testCancellationWithCause() = runTest(expected = { it is AssertionError }) {
5555
expect(1)
56-
val d = async(coroutineContext, CoroutineStart.ATOMIC) {
56+
val d = async(start = CoroutineStart.ATOMIC) {
5757
finish(3)
5858
yield()
5959
}
@@ -97,8 +97,8 @@ class AsyncTest : TestBase() {
9797

9898
@Test
9999
fun testParallelDecompositionCaughtExceptionWithInheritedParent() = runTest {
100-
val deferred = async(coroutineContext) {
101-
val decomposed = async(coroutineContext) {
100+
val deferred = async {
101+
val decomposed = async {
102102
throw AssertionError()
103103
1
104104
}
@@ -115,8 +115,8 @@ class AsyncTest : TestBase() {
115115

116116
@Test
117117
fun testParallelDecompositionUncaughtExceptionWithInheritedParent() = runTest(expected = { it is AssertionError }) {
118-
val deferred = async(coroutineContext) {
119-
val decomposed = async(coroutineContext) {
118+
val deferred = async {
119+
val decomposed = async {
120120
throw AssertionError()
121121
1
122122
}
@@ -163,7 +163,7 @@ class AsyncTest : TestBase() {
163163
@Test
164164
fun testDeferAndYieldException() = runTest(expected = { it is TestException }) {
165165
expect(1)
166-
val d = async(coroutineContext) {
166+
val d = async {
167167
expect(3)
168168
yield() // no effect, parent waiting
169169
finish(4)
@@ -176,20 +176,20 @@ class AsyncTest : TestBase() {
176176
@Test
177177
fun testDeferWithTwoWaiters() = runTest {
178178
expect(1)
179-
val d = async(coroutineContext) {
179+
val d = async {
180180
expect(5)
181181
yield()
182182
expect(9)
183183
42
184184
}
185185
expect(2)
186-
launch(coroutineContext) {
186+
launch {
187187
expect(6)
188188
assertEquals(d.await(), 42)
189189
expect(11)
190190
}
191191
expect(3)
192-
launch(coroutineContext) {
192+
launch {
193193
expect(7)
194194
assertEquals(d.await(), 42)
195195
expect(12)
@@ -212,7 +212,7 @@ class AsyncTest : TestBase() {
212212
@Test
213213
fun testDeferBadClass() = runTest {
214214
val bad = BadClass()
215-
val d = async(coroutineContext) {
215+
val d = async {
216216
expect(1)
217217
bad
218218
}

common/kotlinx-coroutines-core-common/test/AtomicCancellationCommonTest.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ package kotlinx.coroutines.experimental
66

77
import kotlinx.coroutines.experimental.selects.*
88
import kotlinx.coroutines.experimental.sync.*
9-
import kotlin.coroutines.experimental.*
109
import kotlin.test.*
1110

1211
class AtomicCancellationCommonTest : TestBase() {
1312
@Test
1413
fun testCancellableLaunch() = runTest {
1514
expect(1)
16-
val job = launch(coroutineContext) {
15+
val job = launch {
1716
expectUnreached() // will get cancelled before start
1817
}
1918
expect(2)
@@ -24,7 +23,7 @@ class AtomicCancellationCommonTest : TestBase() {
2423
@Test
2524
fun testAtomicLaunch() = runTest {
2625
expect(1)
27-
val job = launch(coroutineContext, start = CoroutineStart.ATOMIC) {
26+
val job = launch(start = CoroutineStart.ATOMIC) {
2827
finish(4) // will execute even after it was cancelled
2928
}
3029
expect(2)
@@ -35,18 +34,18 @@ class AtomicCancellationCommonTest : TestBase() {
3534
@Test
3635
fun testDeferredAwaitCancellable() = runTest {
3736
expect(1)
38-
val deferred = async(coroutineContext) { // deferred, not yet complete
37+
val deferred = async { // deferred, not yet complete
3938
expect(4)
4039
"OK"
4140
}
4241
assertEquals(false, deferred.isCompleted)
4342
var job: Job? = null
44-
launch(coroutineContext) { // will cancel job as soon as deferred completes
43+
launch { // will cancel job as soon as deferred completes
4544
expect(5)
4645
assertEquals(true, deferred.isCompleted)
4746
job!!.cancel()
4847
}
49-
job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
48+
job = launch(start = CoroutineStart.UNDISPATCHED) {
5049
expect(2)
5150
try {
5251
deferred.await() // suspends
@@ -63,17 +62,17 @@ class AtomicCancellationCommonTest : TestBase() {
6362
@Test
6463
fun testJobJoinCancellable() = runTest {
6564
expect(1)
66-
val jobToJoin = launch(coroutineContext) { // not yet complete
65+
val jobToJoin = launch { // not yet complete
6766
expect(4)
6867
}
6968
assertEquals(false, jobToJoin.isCompleted)
7069
var job: Job? = null
71-
launch(coroutineContext) { // will cancel job as soon as jobToJoin completes
70+
launch { // will cancel job as soon as jobToJoin completes
7271
expect(5)
7372
assertEquals(true, jobToJoin.isCompleted)
7473
job!!.cancel()
7574
}
76-
job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
75+
job = launch(start = CoroutineStart.UNDISPATCHED) {
7776
expect(2)
7877
try {
7978
jobToJoin.join() // suspends
@@ -91,7 +90,7 @@ class AtomicCancellationCommonTest : TestBase() {
9190
fun testLockAtomicCancel() = runTest {
9291
expect(1)
9392
val mutex = Mutex(true) // locked mutex
94-
val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
93+
val job = launch(start = CoroutineStart.UNDISPATCHED) {
9594
expect(2)
9695
mutex.lock() // suspends
9796
expect(4) // should execute despite cancellation
@@ -107,7 +106,7 @@ class AtomicCancellationCommonTest : TestBase() {
107106
fun testSelectLockAtomicCancel() = runTest {
108107
expect(1)
109108
val mutex = Mutex(true) // locked mutex
110-
val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
109+
val job = launch(start = CoroutineStart.UNDISPATCHED) {
111110
expect(2)
112111
val result = select<String> { // suspends
113112
mutex.onLock {

0 commit comments

Comments
 (0)