Skip to content

Commit f14bdbe

Browse files
committed
AMQ-8525: Try to harden the JMS Client tests
1 parent b05d74b commit f14bdbe

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/JMSClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ public void testBrokerRestartWontHangConnectionClose() throws Exception {
633633
@Test(timeout=30 * 1000)
634634
public void testProduceAndConsumeLargeNumbersOfMessages() throws Exception {
635635
int count = 1000;
636-
connection = createConnection();
636+
connection = createConnectionWithRetry(name.toString(), false);
637637
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
638638
Queue queue = session.createQueue(getDestinationName());
639639
connection.start();

activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/JMSClientTestSupport.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,37 @@ protected Connection createConnection(String clientId, boolean syncPublish) thro
145145
connection.start();
146146
return connection;
147147
}
148+
149+
/**
150+
* Some SSL/NIO test combinations can occasionally fail the initial frame negotiation
151+
* when brokers are started in parallel forks. Retry once on the specific framing error
152+
* to smooth out that transient race without hiding other issues.
153+
*/
154+
protected Connection createConnectionWithRetry(String clientId, boolean syncPublish) throws JMSException, InterruptedException {
155+
JMSException lastException = null;
156+
for (int attempt = 0; attempt < 2; attempt++) {
157+
try {
158+
return createConnection(clientId, syncPublish);
159+
} catch (JMSException ex) {
160+
lastException = ex;
161+
if (!containsAmqpHeaderMismatch(ex)) {
162+
throw ex;
163+
}
164+
LOG.warn("AMQP header mismatch on connection attempt {} for {}, retrying once", attempt + 1, getBrokerURI());
165+
Thread.sleep(500);
166+
}
167+
}
168+
throw lastException;
169+
}
170+
171+
private boolean containsAmqpHeaderMismatch(Throwable throwable) {
172+
Throwable current = throwable;
173+
while (current != null) {
174+
if (current.getMessage() != null && current.getMessage().contains("AMQP header mismatch")) {
175+
return true;
176+
}
177+
current = current.getCause();
178+
}
179+
return false;
180+
}
148181
}

0 commit comments

Comments
 (0)