Vsevolod Tolstopyatov | 8788488 | 2019-04-09 18:36:22 +0300 | [diff] [blame] | 1 | /* |
| 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 Tolstopyatov | 61c64cc | 2019-04-12 16:05:58 +0300 | [diff] [blame] | 6 | package kotlinx.coroutines.flow |
Vsevolod Tolstopyatov | 8788488 | 2019-04-09 18:36:22 +0300 | [diff] [blame] | 7 | |
| 8 | import kotlinx.coroutines.* |
| 9 | import kotlinx.coroutines.channels.* |
Vsevolod Tolstopyatov | 8788488 | 2019-04-09 18:36:22 +0300 | [diff] [blame] | 10 | import kotlin.test.* |
| 11 | |
| 12 | abstract 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 Elliott | ca095be | 2022-07-25 14:26:10 +0000 | [diff] [blame] | 75 | if (it == 1) flow { |
Vsevolod Tolstopyatov | 8788488 | 2019-04-09 18:36:22 +0300 | [diff] [blame] | 76 | 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 Tolstopyatov | a3429f7 | 2021-07-16 16:02:36 +0300 | [diff] [blame] | 93 | abstract fun testFlatMapConcurrency(): TestResult |
Vsevolod Tolstopyatov | 8788488 | 2019-04-09 18:36:22 +0300 | [diff] [blame] | 94 | } |