Skip to content

Commit e36a43c

Browse files
committed
Refactor LoadBalanceTest to improve bridge formation wait logic and add consumer count check
1 parent a86cffa commit e36a43c

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

activemq-spring/src/test/java/org/apache/bugs/LoadBalanceTest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
import org.apache.activemq.ActiveMQConnectionFactory;
3636
import org.apache.activemq.broker.BrokerRegistry;
3737
import org.apache.activemq.broker.BrokerService;
38+
import org.apache.activemq.broker.region.Destination;
39+
import org.apache.activemq.broker.region.RegionBroker;
3840
import org.apache.activemq.command.ActiveMQQueue;
3941
import org.apache.activemq.network.NetworkConnector;
4042
import org.apache.activemq.store.memory.MemoryPersistenceAdapter;
43+
import org.apache.activemq.util.Wait;
4144
import org.junit.Ignore;
4245
import org.junit.Test;
4346
import org.slf4j.Logger;
@@ -336,26 +339,27 @@ public Message createMessage(final Session session)
336339
broker.stop();
337340
}
338341

339-
// need to ensure broker bridge is alive before starting the consumer
340-
// peeking at the internals will give us this info
342+
// Wait until both brokers have their local consumer AND the remote demand subscription
343+
// from the other broker's bridge (>= 2 consumers per queue). This guarantees:
344+
// 1. Both local consumers (container1, container2) are truly subscribed
345+
// 2. The network bridges are fully started and have propagated demand subscriptions
341346
private void waitForBridgeFormation() throws Exception {
342-
long done = System.currentTimeMillis() + 30000;
343-
while (done > System.currentTimeMillis()) {
344-
if (hasBridge("one") && hasBridge("two")) {
345-
return;
346-
}
347-
Thread.sleep(1000);
348-
}
347+
assertTrue("Both brokers should have local + bridge demand consumers for " + TESTING_QUEUE,
348+
Wait.waitFor(() -> getQueueConsumerCount("one") >= 2 && getQueueConsumerCount("two") >= 2,
349+
30000, 100));
349350
}
350351

351-
private boolean hasBridge(String name) {
352-
boolean result = false;
353-
BrokerService broker = BrokerRegistry.getInstance().lookup(name);
354-
if (broker != null && !broker.getNetworkConnectors().isEmpty()) {
355-
if (!broker.getNetworkConnectors().get(0).activeBridges().isEmpty()) {
356-
result = true;
357-
}
352+
private int getQueueConsumerCount(String brokerName) {
353+
try {
354+
final BrokerService broker = BrokerRegistry.getInstance().lookup(brokerName);
355+
if (broker == null) {
356+
return 0;
358357
}
359-
return result;
358+
final RegionBroker regionBroker = (RegionBroker) broker.getRegionBroker();
359+
final Destination dest = regionBroker.getDestinationMap().get(new ActiveMQQueue(TESTING_QUEUE));
360+
return dest != null ? dest.getConsumers().size() : 0;
361+
} catch (Exception ignored) {
362+
return 0;
363+
}
360364
}
361365
}

0 commit comments

Comments
 (0)