I have written an Apache Camel Spring Boot test with localstack mock AWS services to run an integration test for a Camel Spring project.
The route has a SQS listener that receives the SQS message, logs it and sends it to a direct component.
public void configure() throws Exception {
from("aws2-sqs://sqs-queue")
.routeId("file-upload-route")
.bean(sqsEventProcessor, "processSQSEvent" )
.to("direct:processed-file");
}
The test passes, but the test case does not end after that. It waits for 45 seconds and then initiates a hard shutdown and logs exception java.lang.InterruptedException: null
.
The log says there are 2 inflight messages on the route, but I have checked that there is only one message sent and received.
2025-02-07T19:53:11.661-08:00 INFO 54200 --- [] [ - ShutdownTask] o.a.c.i.engine.DefaultShutdownStrategy :
Waiting as there are still 2 inflight and pending exchanges to complete, timeout in 10 seconds. Inflights per route: [file-upload-route = 2]
2025-02-07T19:53:12.670-08:00 INFO 54200 --- [] [ - ShutdownTask] o.a.c.i.engine.DefaultShutdownStrategy :
Waiting as there are still 2 inflight and pending exchanges to complete, timeout in 9 seconds. Inflights per route: [file-upload-route = 2]
...
Here is the test case:
@CamelSpringBootTest
@MockEndpoints("direct:processed-file") //mock the specified endpoint
@SpringBootTest
@TestPropertySource(properties = "spring.lifecycle.timeout-per-shutdown-phase=5s")
public class FileUploadRouteSpringTest {
@Autowired
private ProducerTemplate template;
@EndpointInject("mock:direct:processed-file")
private MockEndpoint mockEndpoint; //inject the mock endpoint as a bean
@BeforeEach
public void setUp() throws Exception {
mockEndpoint.setAssertPeriod(5000);
}
@Test
public void testReceive() throws Exception {
template.sendBody("aws2-sqs://sqs-queue" + "&deleteAfterRead=true", "Hello");
mockEndpoint.expectedMessageCount(1);
mockEndpoint.assertIsSatisfied();
List<Exchange> exchanges = mockEndpoint.getReceivedExchanges();
assertEquals(1, exchanges.size()); // Check if there is exactly one exchange message
}
@AfterEach
public void tearDown() throws Exception {
camelContext.shutdown();
}
}
I tried camelContext.shutdown();
and also added timeout-per-shutdown-phase
, still this error is logged.
- Why does the route show that there are 2 inflight messages, when I send only one message and it is processed?
- How can I end the test quicker without waiting, shutdown the Spring app gracefully and avoid the exception in the logs?