File: AwaitTest.kt

package info (click to toggle)
kotlinx-coroutines 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,628 kB
  • sloc: xml: 418; sh: 322; javascript: 60; makefile: 17; java: 8
file content (354 lines) | stat: -rw-r--r-- 7,793 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

import kotlin.test.*

class AwaitTest : TestBase() {

    @Test
    fun testAwaitAll() = runTest {
        expect(1)
        val d = async {
            expect(3)
            "OK"
        }

        val d2 = async {
            yield()
            expect(4)
            1L
        }

        expect(2)
        require(d2.isActive && !d2.isCompleted)

        assertEquals(listOf("OK", 1L), awaitAll(d, d2))
        expect(5)

        require(d.isCompleted && d2.isCompleted)
        require(!d.isCancelled && !d2.isCancelled)
        finish(6)
    }

    @Test
    fun testAwaitAllLazy() = runTest {
        expect(1)
        val d = async(start = CoroutineStart.LAZY) {
            expect(2);
            1
        }
        val d2 = async(start = CoroutineStart.LAZY) {
            expect(3);
            2
        }
        assertEquals(listOf(1, 2), awaitAll(d, d2))
        finish(4)
    }

    @Test
    fun testAwaitAllTyped() = runTest {
        val d1 = async { 1L }
        val d2 = async { "" }
        val d3 = async { }

        assertEquals(listOf(1L, ""), listOf(d1, d2).awaitAll())
        assertEquals(listOf(1L, Unit), listOf(d1, d3).awaitAll())
        assertEquals(listOf("", Unit), listOf(d2, d3).awaitAll())
    }

    @Test
    fun testAwaitAllExceptionally() = runTest {
        expect(1)
        val d = async {
            expect(3)
            "OK"
        }

        val d2 = async(NonCancellable) {
            yield()
            throw TestException()
        }

        val d3 = async {
            expect(4)
            delay(Long.MAX_VALUE)
            1
        }

        expect(2)
        try {
            awaitAll(d, d2, d3)
        } catch (e: TestException) {
            expect(5)
        }

        yield()
        require(d.isCompleted && d2.isCancelled && d3.isActive)
        d3.cancel()
        finish(6)
    }

    @Test
    fun testAwaitAllMultipleExceptions() = runTest {
        val d = async(NonCancellable) {
            expect(2)
            throw TestException()
        }

        val d2 = async(NonCancellable) {
            yield()
            throw TestException()
        }

        val d3 = async {
            yield()
        }

        expect(1)
        try {
            awaitAll(d, d2, d3)
        } catch (e: TestException) {
            expect(3)
        }

        finish(4)
    }

    @Test
    fun testAwaitAllCancellation() = runTest {
        val outer = async {

            expect(1)
            val inner = async {
                expect(4)
                delay(Long.MAX_VALUE)
            }

            expect(2)
            awaitAll(inner)
            expectUnreached()
        }

        yield()
        expect(3)
        yield()
        require(outer.isActive)
        outer.cancel()
        require(outer.isCancelled)
        finish(5)
    }

    @Test
    fun testAwaitAllPartiallyCompleted() = runTest {
        val d1 = async { expect(1); 1 }
        d1.await()
        val d2 = async { expect(3); 2 }
        expect(2)
        assertEquals(listOf(1, 2), awaitAll(d1, d2))
        require(d1.isCompleted && d2.isCompleted)
        finish(4)
    }

    @Test
    fun testAwaitAllPartiallyCompletedExceptionally() = runTest {
        val d1 = async(NonCancellable) {
            expect(1)
            throw TestException()
        }

        yield()

        // This job is called after exception propagation
        val d2 = async { expect(4) }

        expect(2)
        try {
            awaitAll(d1, d2)
            expectUnreached()
        } catch (e: TestException) {
            expect(3)
        }

        require(d2.isActive)
        d2.await()
        require(d1.isCompleted && d2.isCompleted)
        finish(5)
    }

    @Test
    fun testAwaitAllFullyCompleted() = runTest {
        val d1 = CompletableDeferred(Unit)
        val d2 = CompletableDeferred(Unit)
        val job = async { expect(3) }
        expect(1)
        awaitAll(d1, d2)
        expect(2)
        job.await()
        finish(4)
    }

    @Test
    fun testAwaitOnSet() = runTest {
        val d1 = CompletableDeferred(Unit)
        val d2 = CompletableDeferred(Unit)
        val job = async { expect(2) }
        expect(1)
        listOf(d1, d2, job).awaitAll()
        finish(3)
    }

    @Test
    fun testAwaitAllFullyCompletedExceptionally() = runTest {
        val d1 = CompletableDeferred<Unit>(parent = null)
            .apply { cancel(TestException()) }
        val d2 = CompletableDeferred<Unit>(parent = null)
            .apply { cancel(TestException()) }
        val job = async { expect(3) }
        expect(1)
        try {
            awaitAll(d1, d2)
        } catch (e: TestException) {
            expect(2)
        }

        job.await()
        finish(4)
    }

    @Test
    fun testAwaitAllSameJobMultipleTimes() = runTest {
        val d = async { "OK" }
        // Duplicates are allowed though kdoc doesn't guarantee that
        assertEquals(listOf("OK", "OK", "OK"), awaitAll(d, d, d))
    }

    @Test
    fun testAwaitAllSameThrowingJobMultipleTimes() = runTest {
        val d1 =
            async(NonCancellable) { throw TestException() }
        val d2 = async { } // do nothing

        try {
            expect(1)
            // Duplicates are allowed though kdoc doesn't guarantee that
            awaitAll(d1, d2, d1, d2)
            expectUnreached()
        } catch (e: TestException) {
            finish(2)
        }
    }

    @Test
    fun testAwaitAllEmpty() = runTest {
        expect(1)
        assertEquals(emptyList(), awaitAll<Unit>())
        assertEquals(emptyList(), emptyList<Deferred<Unit>>().awaitAll())
        finish(2)
    }

    // joinAll

    @Test
    fun testJoinAll() = runTest {
        val d1 = launch { expect(2) }
        val d2 = async {
            expect(3)
            "OK"
        }
        val d3 = launch { expect(4) }

        expect(1)
        joinAll(d1, d2, d3)
        finish(5)
    }

    @Test
    fun testJoinAllLazy() = runTest {
        expect(1)
        val d = async(start = CoroutineStart.LAZY) {
            expect(2)
        }
        val d2 = launch(start = CoroutineStart.LAZY) {
            expect(3)
        }
        joinAll(d, d2)
        finish(4)
    }

    @Test
    fun testJoinAllExceptionally() = runTest {
        val d1 = launch {
            expect(2)
        }
        val d2 = async(NonCancellable) {
            expect(3)
            throw TestException()
        }
        val d3 = async {
            expect(4)
        }

        expect(1)
        joinAll(d1, d2, d3)
        finish(5)
    }

    @Test
    fun testJoinAllCancellation() = runTest {
        val outer = launch {
            expect(2)
            val inner = launch {
                expect(3)
                delay(Long.MAX_VALUE)
            }

            joinAll(inner)
            expectUnreached()
        }

        expect(1)
        yield()
        require(outer.isActive)
        yield()
        outer.cancel()
        outer.join()
        finish(4)
    }

    @Test
    fun testJoinAllAlreadyCompleted() = runTest {
        val job = launch {
            expect(1)
        }

        job.join()
        expect(2)

        joinAll(job)
        finish(3)
    }

    @Test
    fun testJoinAllEmpty() = runTest {
        expect(1)
        joinAll()
        listOf<Job>().joinAll()
        finish(2)
    }

    @Test
    fun testJoinAllSameJob() = runTest {
        val job = launch { }
        joinAll(job, job, job)
    }

    @Test
    fun testJoinAllSameJobExceptionally() = runTest {
        val job =
            async(NonCancellable) { throw TestException() }
        joinAll(job, job, job)
    }
}