Skip to content

Commit d06054b

Browse files
CopilotNetyyyy
andcommitted
Add test for 2.0.0 bug where unique toString() prevents caching
Added new test cachingConnectionFactoryFailsToReuseProducerWithUniqueToString that demonstrates the bug in azure-servicebus-jms 2.0.0 where each Queue instance had a unique toString() like "ServiceBusJmsQueue@11655", which prevented CachingConnectionFactory from caching producers for the same queue name. The test verifies that: - When toString() returns unique values, createProducer is called twice - Different producers are returned even for the same queue name - This contrasts with the fixed 2.1.0 behavior where toString() returns the queue name, enabling proper caching Co-authored-by: Netyyyy <[email protected]>
1 parent f966bd2 commit d06054b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/jms/ServiceBusJmsConnectionFactoryConfigurationTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,67 @@ void cachingConnectionFactoryReusesSameProducerForSameDestination() throws JMSEx
257257
connection.close();
258258
}
259259

260+
@Test
261+
void cachingConnectionFactoryFailsToReuseProducerWithUniqueToString() throws JMSException {
262+
// This test demonstrates the bug in azure-servicebus-jms 2.0.0 where each Queue instance
263+
// had a unique toString() like "ServiceBusJmsQueue@11655", preventing CachingConnectionFactory
264+
// from reusing producers for the same queue name.
265+
266+
// Create mock objects for JMS components
267+
ConnectionFactory mockTargetConnectionFactory = mock(ConnectionFactory.class);
268+
Connection mockConnection = mock(Connection.class);
269+
Session mockSession = mock(Session.class);
270+
Queue mockQueue1FirstCall = mock(Queue.class);
271+
Queue mockQueue1SecondCall = mock(Queue.class);
272+
MessageProducer mockProducer1 = mock(MessageProducer.class);
273+
MessageProducer mockProducer2 = mock(MessageProducer.class);
274+
275+
// Setup mock behavior
276+
when(mockTargetConnectionFactory.createConnection()).thenReturn(mockConnection);
277+
when(mockConnection.createSession(anyBoolean(), anyInt())).thenReturn(mockSession);
278+
when(mockSession.createQueue("queue1"))
279+
.thenReturn(mockQueue1FirstCall)
280+
.thenReturn(mockQueue1SecondCall);
281+
when(mockSession.createProducer(mockQueue1FirstCall)).thenReturn(mockProducer1);
282+
when(mockSession.createProducer(mockQueue1SecondCall)).thenReturn(mockProducer2);
283+
// Simulate azure-servicebus-jms 2.0.0 behavior: each Queue has unique toString()
284+
// This prevents CachingConnectionFactory from caching because it uses toString() as key
285+
when(mockQueue1FirstCall.toString()).thenReturn("ServiceBusJmsQueue@11655");
286+
when(mockQueue1SecondCall.toString()).thenReturn("ServiceBusJmsQueue@22766");
287+
288+
// Create CachingConnectionFactory with caching enabled
289+
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(mockTargetConnectionFactory);
290+
cachingConnectionFactory.setCacheProducers(true);
291+
292+
// Get connection and session
293+
Connection connection = cachingConnectionFactory.createConnection();
294+
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
295+
296+
// Create queues - these are separate Queue instances with unique toString() values
297+
Queue queue1FirstCall = session.createQueue("queue1");
298+
Queue queue1SecondCall = session.createQueue("queue1");
299+
300+
// First call: create producer for queue1 (first Queue instance)
301+
MessageProducer producer1ForQueue1 = session.createProducer(queue1FirstCall);
302+
// Second call: create producer for queue1 (second Queue instance with different toString())
303+
// With azure-servicebus-jms 2.0.0, this creates a NEW producer because toString() differs
304+
MessageProducer producer2ForQueue1 = session.createProducer(queue1SecondCall);
305+
306+
// Verify: different producers are returned because toString() values differ
307+
// This demonstrates the bug in azure-servicebus-jms 2.0.0
308+
assertThat(producer1ForQueue1.toString())
309+
.as("Different producers returned when destination.toString() differs (2.0.0 bug)")
310+
.isNotEqualTo(producer2ForQueue1.toString());
311+
312+
// Verify createProducer was called TWICE - once for each unique toString() value
313+
// This proves caching is NOT working due to unique toString() values in 2.0.0
314+
verify(mockSession, times(1)).createProducer(mockQueue1FirstCall);
315+
verify(mockSession, times(1)).createProducer(mockQueue1SecondCall);
316+
317+
// Cleanup
318+
connection.close();
319+
}
320+
260321
@Configuration
261322
@PropertySource("classpath:servicebus/additional.properties")
262323
static class AdditionalPropertySourceConfiguration {

0 commit comments

Comments
 (0)