From f276af9fb749c745bb3533dd8c5eeb7dc5fed730 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 9 Apr 2025 13:45:42 -0400 Subject: [PATCH] Fix another `LockRequestHandlerAdviceTests` race condition There is no guarantee which message would reach the service first when we use `asyncConvertSendAndReceive()`. * Add `config.longProcessEnteredLatch` to wait before sending the second (fourth in our case) message until the long-running process against the first (third in our case) message has been started. --- .../handler/advice/LockRequestHandlerAdviceTests.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/advice/LockRequestHandlerAdviceTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/advice/LockRequestHandlerAdviceTests.java index d57864254c..b25c6fe0c4 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/handler/advice/LockRequestHandlerAdviceTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/advice/LockRequestHandlerAdviceTests.java @@ -90,6 +90,10 @@ public class LockRequestHandlerAdviceTests { Future test3 = messagingTemplate.asyncConvertSendAndReceive(this.inputChannel, "longer_process", messagePostProcessor); + // Ensure that we have entered a long process before sending new message. + // Otherwise, there is no guarantee which message would reach the service first. + assertThat(this.config.longProcessEnteredLatch.await(10, TimeUnit.SECONDS)).isTrue(); + Future test4 = messagingTemplate.asyncConvertSendAndReceive(this.inputChannel, "test4", messagePostProcessor); @@ -130,6 +134,8 @@ public class LockRequestHandlerAdviceTests { AtomicInteger counter = new AtomicInteger(); + CountDownLatch longProcessEnteredLatch = new CountDownLatch(1); + CountDownLatch longProcessLatch = new CountDownLatch(1); @ServiceActivator(inputChannel = "inputChannel", adviceChain = "lockRequestHandlerAdvice") @@ -138,6 +144,7 @@ public class LockRequestHandlerAdviceTests { if ("longer_process".equals(payload)) { // Hard to achieve blocking expectations just with timeouts. // So, wait for count-down-latch to be fulfilled. + longProcessEnteredLatch.countDown(); longProcessLatch.await(10, TimeUnit.SECONDS); } else {