|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.processor; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | + |
| 21 | +import org.apache.camel.ContextTestSupport; |
| 22 | +import org.apache.camel.Exchange; |
| 23 | +import org.apache.camel.builder.RouteBuilder; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; |
| 30 | + |
| 31 | +/** |
| 32 | + * The doTry calls a route whose error handler handles a failure (dead letter channel or a handled onException). The |
| 33 | + * doFinally block must run all its steps. The caller does not continue routing after the doTry, as before |
| 34 | + * (CAMEL-19441). |
| 35 | + */ |
| 36 | +class DoFinallyAfterHandledCalledRouteTest extends ContextTestSupport { |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean isUseRouteBuilder() { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + private Exchange send(String errorHandler, boolean calleeFails) throws Exception { |
| 44 | + context.addRoutes(new RouteBuilder() { |
| 45 | + @Override |
| 46 | + public void configure() { |
| 47 | + switch (errorHandler) { |
| 48 | + case "dlc" -> errorHandler(deadLetterChannel("mock:dead").redeliveryDelay(0)); |
| 49 | + case "dlcAsync" -> errorHandler(deadLetterChannel("mock:dead").maximumRedeliveries(1) |
| 50 | + .redeliveryDelay(10).asyncDelayedRedelivery()); |
| 51 | + default -> onException(IllegalArgumentException.class).handled(true).to("mock:dead"); |
| 52 | + } |
| 53 | + |
| 54 | + from("direct:start") |
| 55 | + .process(e -> e.setProperty("startThread", Thread.currentThread().getName())) |
| 56 | + .doTry() |
| 57 | + .to("direct:callee") |
| 58 | + .to("mock:afterCallee") |
| 59 | + .doCatch(Exception.class) |
| 60 | + .to("mock:catch") |
| 61 | + .doFinally() |
| 62 | + .to("mock:f1") |
| 63 | + .to("mock:f2") |
| 64 | + .setHeader("f3", constant("yes")) |
| 65 | + .process(e -> e.setProperty("finallyThread", Thread.currentThread().getName())) |
| 66 | + .to("mock:f4") |
| 67 | + .end() |
| 68 | + .to("mock:end"); |
| 69 | + |
| 70 | + if (calleeFails) { |
| 71 | + from("direct:callee").setBody(constant("in callee")) |
| 72 | + .throwException(new IllegalArgumentException("Forced")); |
| 73 | + } else { |
| 74 | + from("direct:callee").setBody(constant("in callee")); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + context.start(); |
| 79 | + |
| 80 | + return assertTimeoutPreemptively(Duration.ofSeconds(10), |
| 81 | + () -> template.send("direct:start", e -> e.getMessage().setBody("Hello")), "the send did not complete"); |
| 82 | + } |
| 83 | + |
| 84 | + private void assertAllFinallySteps(Exchange out) { |
| 85 | + assertEquals(1, getMockEndpoint("mock:f1").getReceivedCounter(), "mock:f1"); |
| 86 | + assertEquals(1, getMockEndpoint("mock:f2").getReceivedCounter(), "mock:f2"); |
| 87 | + assertEquals(1, getMockEndpoint("mock:f4").getReceivedCounter(), "mock:f4"); |
| 88 | + assertEquals("yes", out.getMessage().getHeader("f3"), "the header set by the doFinally"); |
| 89 | + } |
| 90 | + |
| 91 | + private void assertHandledByCalledRoute(Exchange out) { |
| 92 | + assertNull(out.getException(), "the called route handled the failure"); |
| 93 | + assertEquals(1, getMockEndpoint("mock:dead").getReceivedCounter(), "mock:dead"); |
| 94 | + assertEquals(0, getMockEndpoint("mock:afterCallee").getReceivedCounter(), "mock:afterCallee"); |
| 95 | + assertEquals(0, getMockEndpoint("mock:catch").getReceivedCounter(), "there is no exception to catch"); |
| 96 | + // the caller does not continue routing after the doTry (CAMEL-19441) |
| 97 | + assertEquals(0, getMockEndpoint("mock:end").getReceivedCounter(), "mock:end"); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void deadLetterChannel() throws Exception { |
| 102 | + Exchange out = send("dlc", true); |
| 103 | + assertHandledByCalledRoute(out); |
| 104 | + assertAllFinallySteps(out); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void deadLetterChannelAsyncDelayedRedelivery() throws Exception { |
| 109 | + Exchange out = send("dlcAsync", true); |
| 110 | + assertHandledByCalledRoute(out); |
| 111 | + assertAllFinallySteps(out); |
| 112 | + // the redelivery, and so the doFinally block, ran on another thread than the one that started the route |
| 113 | + assertNotEquals(out.getProperty("startThread"), out.getProperty("finallyThread")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void handledOnException() throws Exception { |
| 118 | + Exchange out = send("handled", true); |
| 119 | + assertHandledByCalledRoute(out); |
| 120 | + assertAllFinallySteps(out); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void controlCalledRouteDoesNotFail() throws Exception { |
| 125 | + Exchange out = send("dlc", false); |
| 126 | + assertNull(out.getException()); |
| 127 | + assertEquals(0, getMockEndpoint("mock:dead").getReceivedCounter(), "mock:dead"); |
| 128 | + assertEquals(1, getMockEndpoint("mock:afterCallee").getReceivedCounter(), "mock:afterCallee"); |
| 129 | + assertAllFinallySteps(out); |
| 130 | + assertEquals(1, getMockEndpoint("mock:end").getReceivedCounter(), "mock:end"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void routeCalledFromTheDoFinallyHandlesItsOwnFailure() throws Exception { |
| 135 | + context.addRoutes(new RouteBuilder() { |
| 136 | + @Override |
| 137 | + public void configure() { |
| 138 | + errorHandler(deadLetterChannel("mock:dead").redeliveryDelay(0)); |
| 139 | + |
| 140 | + from("direct:start") |
| 141 | + .doTry() |
| 142 | + .to("direct:callee") |
| 143 | + .doFinally() |
| 144 | + .to("mock:f1") |
| 145 | + .to("direct:callee2") |
| 146 | + .to("mock:f2") |
| 147 | + .end() |
| 148 | + .to("mock:end"); |
| 149 | + |
| 150 | + from("direct:callee").throwException(new IllegalArgumentException("Forced")); |
| 151 | + from("direct:callee2").throwException(new IllegalStateException("Forced in doFinally")); |
| 152 | + } |
| 153 | + }); |
| 154 | + context.start(); |
| 155 | + |
| 156 | + Exchange out = assertTimeoutPreemptively(Duration.ofSeconds(10), |
| 157 | + () -> template.send("direct:start", e -> e.getMessage().setBody("Hello")), "the send did not complete"); |
| 158 | + // the dead letter channel of the route called from the doFinally handles that failure too, as when the doTry |
| 159 | + // did not fail before |
| 160 | + assertNull(out.getException(), "the called routes handled their failures"); |
| 161 | + assertEquals(2, getMockEndpoint("mock:dead").getReceivedCounter(), "mock:dead"); |
| 162 | + assertEquals(1, getMockEndpoint("mock:f1").getReceivedCounter(), "mock:f1"); |
| 163 | + // a handled failure of a called route stops the caller, also in the doFinally block |
| 164 | + assertEquals(0, getMockEndpoint("mock:f2").getReceivedCounter(), "mock:f2"); |
| 165 | + assertEquals(0, getMockEndpoint("mock:end").getReceivedCounter(), "mock:end"); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + void routeCalledFromTheDoFinallyContinuesItsOwnFailure() throws Exception { |
| 170 | + context.addRoutes(new RouteBuilder() { |
| 171 | + @Override |
| 172 | + public void configure() { |
| 173 | + errorHandler(deadLetterChannel("mock:dead").redeliveryDelay(0)); |
| 174 | + onException(NumberFormatException.class).continued(true); |
| 175 | + |
| 176 | + from("direct:start") |
| 177 | + .doTry() |
| 178 | + .to("direct:callee") |
| 179 | + .doFinally() |
| 180 | + .to("mock:f1") |
| 181 | + .to("direct:callee2") |
| 182 | + .to("mock:f2") |
| 183 | + .end() |
| 184 | + .to("mock:end"); |
| 185 | + |
| 186 | + from("direct:callee").throwException(new IllegalArgumentException("Forced")); |
| 187 | + from("direct:callee2").throwException(new NumberFormatException("Forced in doFinally")) |
| 188 | + .to("mock:callee2End"); |
| 189 | + } |
| 190 | + }); |
| 191 | + context.start(); |
| 192 | + |
| 193 | + Exchange out = assertTimeoutPreemptively(Duration.ofSeconds(10), |
| 194 | + () -> template.send("direct:start", e -> e.getMessage().setBody("Hello")), "the send did not complete"); |
| 195 | + assertNull(out.getException(), "the called routes handled or continued their failures"); |
| 196 | + assertEquals(1, getMockEndpoint("mock:dead").getReceivedCounter(), "mock:dead"); |
| 197 | + // the onException of the route called from the doFinally continues its failure |
| 198 | + assertEquals(1, getMockEndpoint("mock:callee2End").getReceivedCounter(), "mock:callee2End"); |
| 199 | + assertEquals(1, getMockEndpoint("mock:f1").getReceivedCounter(), "mock:f1"); |
| 200 | + assertEquals(1, getMockEndpoint("mock:f2").getReceivedCounter(), "mock:f2"); |
| 201 | + // the caller does not continue routing after the doTry (CAMEL-19441) |
| 202 | + assertEquals(0, getMockEndpoint("mock:end").getReceivedCounter(), "mock:end"); |
| 203 | + } |
| 204 | +} |
0 commit comments