blob: f09db120a8c2b0cf576416ccc9ec560df5f8f64e [file] [log] [blame]
Vsevolod Tolstopyatov87884882019-04-09 18:36:22 +03001/*
2 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5
Vsevolod Tolstopyatov61c64cc2019-04-12 16:05:58 +03006package kotlinx.coroutines.flow
Vsevolod Tolstopyatov87884882019-04-09 18:36:22 +03007
8import kotlinx.coroutines.*
9import kotlinx.coroutines.channels.*
Vsevolod Tolstopyatov87884882019-04-09 18:36:22 +030010import kotlin.test.*
11
12abstract class FlatMapMergeBaseTest : FlatMapBaseTest() {
13 @Test
14 fun testFailureCancellation() = runTest {
15 val flow = flow {
16 expect(2)
17 emit(1)
18 expect(3)
19 emit(2)
20 expect(4)
21 }.flatMap {
22 if (it == 1) flow {
23 hang { expect(6) }
24 } else flow<Int> {
25 expect(5)
26 throw TestException()
27 }
28 }
29
30 expect(1)
31 assertFailsWith<TestException> { flow.singleOrNull() }
32 finish(7)
33 }
34
35 @Test
36 fun testConcurrentFailure() = runTest {
37 val latch = Channel<Unit>()
38 val flow = flow {
39 expect(2)
40 emit(1)
41 expect(3)
42 emit(2)
43 }.flatMap {
44 if (it == 1) flow<Int> {
45 expect(5)
46 latch.send(Unit)
47 hang {
48 expect(7)
49 throw TestException2()
50
51 }
52 } else {
53 expect(4)
54 latch.receive()
55 expect(6)
56 throw TestException()
57 }
58 }
59
60 expect(1)
61 assertFailsWith<TestException>(flow)
62 finish(8)
63 }
64
65 @Test
66 fun testFailureInMapOperationCancellation() = runTest {
67 val latch = Channel<Unit>()
68 val flow = flow {
69 expect(2)
70 emit(1)
71 expect(3)
72 emit(2)
73 expectUnreached()
74 }.flatMap {
Steve Elliottca095be2022-07-25 14:26:10 +000075 if (it == 1) flow {
Vsevolod Tolstopyatov87884882019-04-09 18:36:22 +030076 expect(5)
77 latch.send(Unit)
78 hang { expect(7) }
79 } else {
80 expect(4)
81 latch.receive()
82 expect(6)
83 throw TestException()
84 }
85 }
86
87 expect(1)
88 assertFailsWith<TestException> { flow.count() }
89 finish(8)
90 }
91
92 @Test
Vsevolod Tolstopyatova3429f72021-07-16 16:02:36 +030093 abstract fun testFlatMapConcurrency(): TestResult
Vsevolod Tolstopyatov87884882019-04-09 18:36:22 +030094}