|
| 1 | +package ox.flow |
| 2 | + |
| 3 | +import org.scalatest.flatspec.AnyFlatSpec |
| 4 | +import org.scalatest.matchers.should.Matchers |
| 5 | +import ox.* |
| 6 | +import ox.channels.ChannelClosedException |
| 7 | + |
| 8 | +class FlowOpsRecoverTest extends AnyFlatSpec with Matchers: |
| 9 | + |
| 10 | + behavior of "Flow.recover" |
| 11 | + |
| 12 | + it should "pass through elements when upstream flow succeeds" in: |
| 13 | + // given |
| 14 | + val flow = Flow.fromValues(1, 2, 3) |
| 15 | + val recoveryFunction: PartialFunction[Throwable, Int] = { case _: IllegalArgumentException => |
| 16 | + 42 |
| 17 | + } |
| 18 | + |
| 19 | + // when |
| 20 | + val result = flow.recover(recoveryFunction).runToList() |
| 21 | + |
| 22 | + // then |
| 23 | + result shouldBe List(1, 2, 3) |
| 24 | + |
| 25 | + it should "emit recovery value when upstream flow fails with handled exception" in: |
| 26 | + // given |
| 27 | + val exception = new IllegalArgumentException("test error") |
| 28 | + val flow = Flow.fromValues(1, 2).concat(Flow.failed(exception)) |
| 29 | + val recoveryFunction: PartialFunction[Throwable, Int] = { case _: IllegalArgumentException => |
| 30 | + 42 |
| 31 | + } |
| 32 | + |
| 33 | + // when |
| 34 | + val result = flow.recover(recoveryFunction).runToList() |
| 35 | + |
| 36 | + // then |
| 37 | + result shouldBe List(1, 2, 42) |
| 38 | + |
| 39 | + it should "not emit recovery value when downstream flow fails with handled exception" in: |
| 40 | + // given |
| 41 | + val exception = new IllegalArgumentException("test error") |
| 42 | + val recoveryFunction: PartialFunction[Throwable, Int] = { case _: IllegalArgumentException => |
| 43 | + 42 |
| 44 | + } |
| 45 | + val flow = Flow.fromValues(1, 2).recover(recoveryFunction).concat(Flow.failed(exception)) |
| 46 | + |
| 47 | + // when & then |
| 48 | + the[IllegalArgumentException] thrownBy { |
| 49 | + flow.runToList() |
| 50 | + } should have message "test error" |
| 51 | + |
| 52 | + it should "propagate unhandled exceptions" in: |
| 53 | + // given |
| 54 | + val exception = new RuntimeException("unhandled error") |
| 55 | + val flow = Flow.fromValues(1, 2).concat(Flow.failed(exception)) |
| 56 | + val recoveryFunction: PartialFunction[Throwable, Int] = { case _: IllegalArgumentException => |
| 57 | + 42 |
| 58 | + } |
| 59 | + |
| 60 | + // when & then |
| 61 | + val caught = the[ChannelClosedException.Error] thrownBy { |
| 62 | + flow.recover(recoveryFunction).runToList() |
| 63 | + } |
| 64 | + caught.getCause shouldBe an[RuntimeException] |
| 65 | + caught.getCause.getMessage shouldBe "unhandled error" |
| 66 | + |
| 67 | + it should "handle multiple exception types" in: |
| 68 | + // given |
| 69 | + val exception = new IllegalStateException("state error") |
| 70 | + val flow = Flow.fromValues(1, 2).concat(Flow.failed(exception)) |
| 71 | + val recoveryFunction: PartialFunction[Throwable, Int] = { |
| 72 | + case _: IllegalArgumentException => 42 |
| 73 | + case _: IllegalStateException => 99 |
| 74 | + case _: NullPointerException => 0 |
| 75 | + } |
| 76 | + |
| 77 | + // when |
| 78 | + val result = flow.recover(recoveryFunction).runToList() |
| 79 | + |
| 80 | + // then |
| 81 | + result shouldBe List(1, 2, 99) |
| 82 | + |
| 83 | + it should "work with different recovery value type" in: |
| 84 | + // given |
| 85 | + val exception = new IllegalArgumentException("test error") |
| 86 | + val flow = Flow.fromValues("a", "b").concat(Flow.failed(exception)) |
| 87 | + val recoveryFunction: PartialFunction[Throwable, String] = { case _: IllegalArgumentException => |
| 88 | + "recovered" |
| 89 | + } |
| 90 | + |
| 91 | + // when |
| 92 | + val result = flow.recover(recoveryFunction).runToList() |
| 93 | + |
| 94 | + // then |
| 95 | + result shouldBe List("a", "b", "recovered") |
| 96 | + |
| 97 | + it should "handle exception thrown during flow processing" in: |
| 98 | + // given |
| 99 | + val flow = Flow.fromValues(1, 2, 3).map(x => if x == 3 then throw new IllegalArgumentException("map error") else x) |
| 100 | + val recoveryFunction: PartialFunction[Throwable, Int] = { case _: IllegalArgumentException => |
| 101 | + -1 |
| 102 | + } |
| 103 | + |
| 104 | + // when |
| 105 | + val result = flow.recover(recoveryFunction).runToList() |
| 106 | + |
| 107 | + // then |
| 108 | + result shouldBe List(1, 2, -1) |
| 109 | + |
| 110 | + it should "work with empty flow" in: |
| 111 | + // given |
| 112 | + val flow = Flow.empty[Int] |
| 113 | + val recoveryFunction: PartialFunction[Throwable, Int] = { case _: IllegalArgumentException => |
| 114 | + 42 |
| 115 | + } |
| 116 | + |
| 117 | + // when |
| 118 | + val result = flow.recover(recoveryFunction).runToList() |
| 119 | + |
| 120 | + // then |
| 121 | + result shouldBe List.empty |
| 122 | + |
| 123 | + it should "propagate exception when partial function throws" in: |
| 124 | + // given |
| 125 | + val originalException = new IllegalArgumentException("original error") |
| 126 | + val flow = Flow.fromValues(1, 2).concat(Flow.failed(originalException)) |
| 127 | + val recoveryFunction: PartialFunction[Throwable, Int] = { case _: IllegalArgumentException => |
| 128 | + throw new RuntimeException("recovery failed") |
| 129 | + } |
| 130 | + |
| 131 | + // when & then |
| 132 | + val caught = the[ChannelClosedException.Error] thrownBy { |
| 133 | + flow.recover(recoveryFunction).runToList() |
| 134 | + } |
| 135 | + caught.getCause shouldBe an[RuntimeException] |
| 136 | + caught.getCause.getMessage shouldBe "recovery failed" |
| 137 | +end FlowOpsRecoverTest |
0 commit comments