Repository: camel Updated Branches: refs/heads/master a38ff2587 -> 5b14e46c3
CAMEL-11747: Change SupervisingRouteControllerRestartTest to not use Jetty in test Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/eeb828cc Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/eeb828cc Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/eeb828cc Branch: refs/heads/master Commit: eeb828cc2a96874ecb1d28f3a5a06490e71801c4 Parents: a38ff25 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Sep 6 10:45:44 2017 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Sep 6 10:45:44 2017 +0200 ---------------------------------------------------------------------- .../SupervisingRouteControllerRestartTest.java | 53 ++++++---------- .../camel/spring/boot/dummy/DummyComponent.java | 38 ++++++++++++ .../camel/spring/boot/dummy/DummyConsumer.java | 54 +++++++++++++++++ .../camel/spring/boot/dummy/DummyEndpoint.java | 63 ++++++++++++++++++++ 4 files changed, 174 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/eeb828cc/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerRestartTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerRestartTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerRestartTest.java index 3383be4..3d8b815 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerRestartTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerRestartTest.java @@ -16,14 +16,11 @@ */ package org.apache.camel.spring.boot; -import java.net.BindException; -import java.net.ServerSocket; - import org.apache.camel.CamelContext; import org.apache.camel.ServiceStatus; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.SupervisingRouteController; -import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.spring.boot.dummy.DummyComponent; import org.apache.camel.util.backoff.BackOffTimer; import org.junit.Assert; import org.junit.Test; @@ -35,9 +32,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; - @DirtiesContext @RunWith(SpringRunner.class) @SpringBootTest( @@ -59,6 +53,7 @@ import static org.assertj.core.api.Assertions.fail; } ) public class SupervisingRouteControllerRestartTest { + @Autowired private CamelContext context; @@ -74,43 +69,32 @@ public class SupervisingRouteControllerRestartTest { Assert.assertEquals(ServiceStatus.Started, context.getRouteStatus("foo")); Assert.assertEquals(ServiceStatus.Started, context.getRouteStatus("bar")); - Assert.assertEquals(ServiceStatus.Started, context.getRouteStatus("jetty")); + Assert.assertEquals(ServiceStatus.Started, context.getRouteStatus("dummy")); // Wait a little Thread.sleep(250); - controller.stopRoute("jetty"); + controller.stopRoute("dummy"); - Assert.assertNull(context.getRoute("jetty").getRouteContext().getRouteController()); - - // bind the port so starting the route jetty should fail - ServerSocket socket = new ServerSocket(TestConfiguration.PORT); + Assert.assertNull(context.getRoute("dummy").getRouteContext().getRouteController()); + // dummy will fail on first restart try { - controller.startRoute("jetty"); + controller.startRoute("dummy"); } catch (Exception e) { - assertThat(e).isInstanceOf(BindException.class); + Assert.assertEquals("Forced error on restart", e.getMessage()); } - // Wait for at lest one restart attempt. - Thread.sleep(2000); - - Assert.assertTrue(controller.getBackOffContext("jetty").isPresent()); - Assert.assertEquals(BackOffTimer.Task.Status.Active, controller.getBackOffContext("jetty").get().getStatus()); - Assert.assertTrue(controller.getBackOffContext("jetty").get().getCurrentAttempts() > 0); - - try { - socket.close(); - } catch (Exception e) { - fail("Failed to close server socket", e); - } + Assert.assertTrue(controller.getBackOffContext("dummy").isPresent()); + Assert.assertEquals(BackOffTimer.Task.Status.Active, controller.getBackOffContext("dummy").get().getStatus()); + Assert.assertTrue(controller.getBackOffContext("dummy").get().getCurrentAttempts() > 0); // Wait for wile to give time to the controller to start the route Thread.sleep(1500); - Assert.assertEquals(ServiceStatus.Started, context.getRouteStatus("jetty")); - Assert.assertNotNull(context.getRoute("jetty").getRouteContext().getRouteController()); - Assert.assertFalse(controller.getBackOffContext("jetty").isPresent()); + Assert.assertEquals(ServiceStatus.Started, context.getRouteStatus("dummy")); + Assert.assertNotNull(context.getRoute("dummy").getRouteContext().getRouteController()); + Assert.assertFalse(controller.getBackOffContext("dummy").isPresent()); } // ************************************* @@ -119,13 +103,14 @@ public class SupervisingRouteControllerRestartTest { @Configuration public static class TestConfiguration { - private static final int PORT = AvailablePortFinder.getNextAvailable(); @Bean public RouteBuilder routeBuilder() { return new RouteBuilder() { @Override public void configure() throws Exception { + getContext().addComponent("dummy", new DummyComponent()); + from("timer:foo?period=5s") .id("foo") .startupOrder(2) @@ -142,9 +127,9 @@ public class SupervisingRouteControllerRestartTest { .autoStartup(false) .to("mock:timer-no-autostartup"); - fromF("jetty:http://localhost:%d", PORT) - .id("jetty") - .to("mock:jetty"); + from("dummy:foo?failOnRestart=true") + .id("dummy") + .to("mock:dummy"); } }; } http://git-wip-us.apache.org/repos/asf/camel/blob/eeb828cc/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyComponent.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyComponent.java new file mode 100644 index 0000000..04c2a81 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyComponent.java @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.spring.boot.dummy; + +import java.util.Map; + +import org.apache.camel.Endpoint; +import org.apache.camel.impl.DefaultComponent; + +public class DummyComponent extends DefaultComponent { + + private int counter; + + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + Endpoint answer = new DummyEndpoint(uri, this); + setProperties(answer, parameters); + return answer; + } + + public int increment() { + return ++counter; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/eeb828cc/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyConsumer.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyConsumer.java new file mode 100644 index 0000000..bf05043 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyConsumer.java @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.spring.boot.dummy; + +import java.io.IOException; + +import org.apache.camel.Endpoint; +import org.apache.camel.Processor; +import org.apache.camel.impl.DefaultConsumer; + +public class DummyConsumer extends DefaultConsumer { + + private boolean failOnRestart = true; + + public DummyConsumer(Endpoint endpoint, Processor processor) { + super(endpoint, processor); + } + + @Override + public DummyEndpoint getEndpoint() { + return (DummyEndpoint) super.getEndpoint(); + } + + public boolean isFailOnRestart() { + return failOnRestart; + } + + public void setFailOnRestart(boolean failOnRestart) { + this.failOnRestart = failOnRestart; + } + + @Override + protected void doStart() throws Exception { + int counter = getEndpoint().getComponent().increment(); + if (counter == 2) { + throw new IOException("Forced error on restart"); + } + super.doStart(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/eeb828cc/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyEndpoint.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyEndpoint.java new file mode 100644 index 0000000..4ae60cf --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/dummy/DummyEndpoint.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.spring.boot.dummy; + +import org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.impl.DefaultEndpoint; + +public class DummyEndpoint extends DefaultEndpoint { + + private boolean failOnRestart = true; + + public DummyEndpoint(String endpointUri, Component component) { + super(endpointUri, component); + } + + @Override + public DummyComponent getComponent() { + return (DummyComponent) super.getComponent(); + } + + @Override + public Producer createProducer() throws Exception { + throw new UnsupportedOperationException("Cannot produce"); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + DummyConsumer consumer = new DummyConsumer(this, processor); + configureConsumer(consumer); + consumer.setFailOnRestart(failOnRestart); + return consumer; + } + + @Override + public boolean isSingleton() { + return true; + } + + public boolean isFailOnRestart() { + return failOnRestart; + } + + public void setFailOnRestart(boolean failOnRestart) { + this.failOnRestart = failOnRestart; + } +}