|
32 | 32 |
|
33 | 33 | import jakarta.jms.Connection; |
34 | 34 | import jakarta.jms.ConnectionFactory; |
35 | | -import jakarta.jms.JMSSecurityException; |
| 35 | +import jakarta.jms.JMSException; |
36 | 36 | import jakarta.jms.Session; |
37 | 37 | import java.net.URI; |
38 | 38 | import java.util.ArrayList; |
39 | 39 | import java.util.List; |
| 40 | +import java.util.concurrent.TimeUnit; |
40 | 41 |
|
41 | 42 | import static org.apache.activemq.util.TestUtils.findOpenPort; |
42 | 43 | import static org.junit.Assert.assertEquals; |
@@ -109,32 +110,47 @@ public void tearDown() throws Exception { |
109 | 110 |
|
110 | 111 | @Test(timeout = 60000) |
111 | 112 | 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)) { |
118 | 122 | 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); |
123 | 137 | connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
124 | | - expectedConnectionCount++; |
| 138 | + successfulConnections++; |
| 139 | + } catch (final JMSException e) { |
| 140 | + fail("Good connection should not fail: " + e.getMessage()); |
125 | 141 | } |
126 | | - } catch (JMSSecurityException e) { |
127 | 142 | } |
128 | 143 | LOG.debug("Iteration {} Connections? {}", i, proxyConnector.getConnectionCount()); |
129 | 144 | } |
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()); |
138 | 154 | } |
139 | 155 |
|
140 | 156 | } |
0 commit comments