From e500c737e6835f93fcbb8cce9b855325ba7d9f59 Mon Sep 17 00:00:00 2001 From: gurpartap3697 Date: Sat, 11 Apr 2026 09:55:21 +0530 Subject: [PATCH 1/2] replace Thread.sleep() with waitfor() to fix flakiness in DurableSubInBrokerNetworkTest --- .../DurableSubInBrokerNetworkTest.java | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/DurableSubInBrokerNetworkTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/DurableSubInBrokerNetworkTest.java index b617dfce673..d7764dfc760 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/DurableSubInBrokerNetworkTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/DurableSubInBrokerNetworkTest.java @@ -30,6 +30,7 @@ import org.apache.activemq.network.DiscoveryNetworkConnector; import org.apache.activemq.network.NetworkConnector; import org.apache.activemq.network.NetworkTestSupport; +import org.apache.activemq.util.Wait; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.junit.experimental.categories.Category; @@ -59,6 +60,9 @@ protected void setUp() throws Exception { nc.setDuplex(true); remoteBroker.addNetworkConnector(nc); nc.start(); + + assertTrue("Network bridge did not establish in time", + Wait.waitFor(() -> !nc.activeBridges().isEmpty())); } protected void tearDown() throws Exception { @@ -92,28 +96,25 @@ public void testDurableSubNetwork() throws Exception { Destination dest = session.createTopic(topicName); TopicSubscriber sub = session.createDurableSubscriber((Topic)dest, subName); LOG.info("Durable subscription of name " + subName + "created."); - Thread.sleep(100); // query durable sub on local and remote broker // raise an error if not found - - assertTrue(foundSubInLocalBroker(subName)); - - - assertTrue(foundSubInRemoteBrokerByTopicName(topicName)); + assertTrue("Durable subscription not found in local broker", + Wait.waitFor(() -> foundSubInLocalBroker(subName))); + assertTrue("Durable subscription not propagated to remote broker", + Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName))); // unsubscribe from durable sub sub.close(); session.unsubscribe(subName); LOG.info("Unsubscribed from durable subscription."); - Thread.sleep(100); // query durable sub on local and remote broker // raise an error if its not removed from both brokers - assertFalse(foundSubInLocalBroker(subName)); - - assertFalse("Durable subscription not unregistered on remote broker", - foundSubInRemoteBrokerByTopicName(topicName)); + assertTrue("Durable subscription not removed from local broker", + Wait.waitFor(() -> !foundSubInLocalBroker(subName))); + assertTrue("Durable subscription not unregistered on remote broker", + Wait.waitFor(() -> !foundSubInRemoteBrokerByTopicName(topicName))); } @@ -131,39 +132,35 @@ public void testTwoDurableSubsInNetworkWithUnsubscribe() throws Exception{ TopicSubscriber sub2 = session.createDurableSubscriber((Topic) dest, subName2); LOG.info("Durable subscription of name " + subName2 + "created."); - Thread.sleep(100); - // query durable sub on local and remote broker // raise an error if not found - - assertTrue(foundSubInLocalBroker(subName)); - assertTrue(foundSubInLocalBroker(subName2)); - - - assertTrue(foundSubInRemoteBrokerByTopicName(topicName)); + assertTrue("Subscription1 not found in local broker", + Wait.waitFor(() -> foundSubInLocalBroker(subName))); + assertTrue("Subscription2 not found in local broker", + Wait.waitFor(() -> foundSubInLocalBroker(subName2))); + assertTrue("Durable subscription not propagated to remote broker", + Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName))); // unsubscribe from durable sub sub.close(); session.unsubscribe(subName); LOG.info("Unsubscribed from durable subscription."); - Thread.sleep(100); // query durable sub on local and remote broker - assertFalse(foundSubInLocalBroker(subName)); - assertTrue(foundSubInLocalBroker(subName2)); - + assertTrue("Subscription1 not removed from local broker", + Wait.waitFor(() -> !foundSubInLocalBroker(subName))); + assertTrue("Subscription2 should still be in local broker", + Wait.waitFor(() -> foundSubInLocalBroker(subName2))); assertTrue("Durable subscription should still be on remote broker", - foundSubInRemoteBrokerByTopicName(topicName)); + Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName))); sub2.close(); session.unsubscribe(subName2); - Thread.sleep(100); - - assertFalse(foundSubInLocalBroker(subName2)); - - assertFalse("Durable subscription not unregistered on remote broker", - foundSubInRemoteBrokerByTopicName(topicName)); + assertTrue("Subscription2 not removed from local broker", + Wait.waitFor(() -> !foundSubInLocalBroker(subName2))); + assertTrue("Durable subscription not unregistered on remote broker", + Wait.waitFor(() -> !foundSubInRemoteBrokerByTopicName(topicName))); } From 0f6db1c995153c3e3986e2b55a0ac5095f884931 Mon Sep 17 00:00:00 2001 From: gurpartap3697 Date: Sat, 11 Apr 2026 21:39:28 +0530 Subject: [PATCH 2/2] update polling time of Wait.waitFor() --- .../DurableSubInBrokerNetworkTest.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/DurableSubInBrokerNetworkTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/DurableSubInBrokerNetworkTest.java index d7764dfc760..000495d5c8a 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/DurableSubInBrokerNetworkTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/DurableSubInBrokerNetworkTest.java @@ -62,7 +62,7 @@ protected void setUp() throws Exception { nc.start(); assertTrue("Network bridge did not establish in time", - Wait.waitFor(() -> !nc.activeBridges().isEmpty())); + Wait.waitFor(() -> !nc.activeBridges().isEmpty(), 15000, 100)); } protected void tearDown() throws Exception { @@ -100,9 +100,9 @@ public void testDurableSubNetwork() throws Exception { // query durable sub on local and remote broker // raise an error if not found assertTrue("Durable subscription not found in local broker", - Wait.waitFor(() -> foundSubInLocalBroker(subName))); + Wait.waitFor(() -> foundSubInLocalBroker(subName), 15000, 100)); assertTrue("Durable subscription not propagated to remote broker", - Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName))); + Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName), 15000, 100)); // unsubscribe from durable sub sub.close(); @@ -112,9 +112,9 @@ public void testDurableSubNetwork() throws Exception { // query durable sub on local and remote broker // raise an error if its not removed from both brokers assertTrue("Durable subscription not removed from local broker", - Wait.waitFor(() -> !foundSubInLocalBroker(subName))); + Wait.waitFor(() -> !foundSubInLocalBroker(subName), 15000, 100)); assertTrue("Durable subscription not unregistered on remote broker", - Wait.waitFor(() -> !foundSubInRemoteBrokerByTopicName(topicName))); + Wait.waitFor(() -> !foundSubInRemoteBrokerByTopicName(topicName), 15000, 100)); } @@ -135,11 +135,11 @@ public void testTwoDurableSubsInNetworkWithUnsubscribe() throws Exception{ // query durable sub on local and remote broker // raise an error if not found assertTrue("Subscription1 not found in local broker", - Wait.waitFor(() -> foundSubInLocalBroker(subName))); + Wait.waitFor(() -> foundSubInLocalBroker(subName), 15000, 100)); assertTrue("Subscription2 not found in local broker", - Wait.waitFor(() -> foundSubInLocalBroker(subName2))); + Wait.waitFor(() -> foundSubInLocalBroker(subName2), 15000, 100)); assertTrue("Durable subscription not propagated to remote broker", - Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName))); + Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName), 15000, 100)); // unsubscribe from durable sub sub.close(); @@ -148,19 +148,19 @@ public void testTwoDurableSubsInNetworkWithUnsubscribe() throws Exception{ // query durable sub on local and remote broker assertTrue("Subscription1 not removed from local broker", - Wait.waitFor(() -> !foundSubInLocalBroker(subName))); + Wait.waitFor(() -> !foundSubInLocalBroker(subName), 15000, 100)); assertTrue("Subscription2 should still be in local broker", - Wait.waitFor(() -> foundSubInLocalBroker(subName2))); + Wait.waitFor(() -> foundSubInLocalBroker(subName2), 15000, 100)); assertTrue("Durable subscription should still be on remote broker", - Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName))); + Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName), 15000, 100)); sub2.close(); session.unsubscribe(subName2); assertTrue("Subscription2 not removed from local broker", - Wait.waitFor(() -> !foundSubInLocalBroker(subName2))); + Wait.waitFor(() -> !foundSubInLocalBroker(subName2), 15000, 100)); assertTrue("Durable subscription not unregistered on remote broker", - Wait.waitFor(() -> !foundSubInRemoteBrokerByTopicName(topicName))); + Wait.waitFor(() -> !foundSubInRemoteBrokerByTopicName(topicName), 15000, 100)); }