Skip to content

Commit e5eabc5

Browse files
committed
AMQ-9845 Fix race condition in AMQ4889Test by ensuring proper connection handling and verification of successful and failed connections
1 parent fac8b6e commit e5eabc5

1 file changed

Lines changed: 37 additions & 21 deletions

File tree

activemq-unit-tests/src/test/java/org/apache/activemq/proxy/AMQ4889Test.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232

3333
import jakarta.jms.Connection;
3434
import jakarta.jms.ConnectionFactory;
35-
import jakarta.jms.JMSSecurityException;
35+
import jakarta.jms.JMSException;
3636
import jakarta.jms.Session;
3737
import java.net.URI;
3838
import java.util.ArrayList;
3939
import java.util.List;
40+
import java.util.concurrent.TimeUnit;
4041

4142
import static org.apache.activemq.util.TestUtils.findOpenPort;
4243
import static org.junit.Assert.assertEquals;
@@ -109,32 +110,47 @@ public void tearDown() throws Exception {
109110

110111
@Test(timeout = 60000)
111112
public void testForConnectionLeak() throws Exception {
112-
Integer expectedConnectionCount = 0;
113-
for (int i=0; i < ITERATIONS; i++) {
114-
try {
115-
if (i % 2 == 0) {
116-
LOG.debug("Iteration {} adding bad connection", i);
117-
Connection connection = connectionFactory.createConnection(USER, WRONG_PASSWORD);
113+
int successfulConnections = 0;
114+
int failedConnections = 0;
115+
116+
for (int i = 0; i < ITERATIONS; i++) {
117+
if (i % 2 == 0) {
118+
// Expected to fail - bad password
119+
// Use try-with-resources to ensure failed connections are cleaned up
120+
LOG.debug("Iteration {} adding bad connection", i);
121+
try (final Connection connection = connectionFactory.createConnection(USER, WRONG_PASSWORD)) {
118122
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
119-
fail("createSession should fail");
120-
} else {
121-
LOG.debug("Iteration {} adding good connection", i);
122-
Connection connection = connectionFactory.createConnection(USER, GOOD_USER_PASSWORD);
123+
fail("createSession should fail with bad password");
124+
} catch (final JMSException e) {
125+
// Authentication failure can be JMSSecurityException (synchronous)
126+
// or JMSException "Disposed due to prior exception" (asynchronous)
127+
// we don't care much here as long as it fails, but we could add an ExceptionListener to the connection
128+
// to check more closely if desired.
129+
failedConnections++;
130+
}
131+
} else {
132+
// Expected to succeed - good password
133+
// Do NOT close - test verifies these connections persist
134+
LOG.debug("Iteration {} adding good connection", i);
135+
try { // do not use try-with-resources here, because we want to keep the connection open and assert at the end
136+
final Connection connection = connectionFactory.createConnection(USER, GOOD_USER_PASSWORD);
123137
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
124-
expectedConnectionCount++;
138+
successfulConnections++;
139+
} catch (final JMSException e) {
140+
fail("Good connection should not fail: " + e.getMessage());
125141
}
126-
} catch (JMSSecurityException e) {
127142
}
128143
LOG.debug("Iteration {} Connections? {}", i, proxyConnector.getConnectionCount());
129144
}
130-
final Integer val = expectedConnectionCount;
131-
Wait.waitFor(new Wait.Condition() {
132-
@Override
133-
public boolean isSatisified() throws Exception {
134-
return val.equals(proxyConnector.getConnectionCount());
135-
}
136-
}, 20);
137-
assertEquals(val, proxyConnector.getConnectionCount());
145+
146+
// Verify we had the expected number of failures and successes
147+
assertEquals("Failed connections", ITERATIONS / 2, failedConnections);
148+
assertEquals("Successful connections", ITERATIONS / 2, successfulConnections);
149+
150+
// Wait for proxy connector to reflect only the successful (still open) connections
151+
final int expectedConnections = successfulConnections;
152+
Wait.waitFor(() -> expectedConnections == proxyConnector.getConnectionCount(), TimeUnit.SECONDS.toMillis(20));
153+
assertEquals("Proxy connection count", expectedConnections, (int) proxyConnector.getConnectionCount());
138154
}
139155

140156
}

0 commit comments

Comments
 (0)