gnodet-bot commented on code in PR #31133:
URL: https://github.com/apache/camel/pull/31133#discussion_r4743667143
##########
components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumerTest.java:
##########
@@ -1086,6 +1090,300 @@ void doShutdownDoesNotCloseUserProvidedClient() throws
Exception {
verify(client, never()).close();
}
+ //
-------------------------------------------------------------------------
+ // SessionLockRenewer tests
+ //
-------------------------------------------------------------------------
+
+ @Test
+ void sessionLockRenewerCreatedForSessionEnabledConsumerInPeekLockMode()
throws Exception {
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+
verify(clientFactory).createServiceBusSessionReceiverAsyncClient(any());
+ verify(lockRenewerExecutor).scheduleWithFixedDelay(any(),
anyLong(), anyLong(), any());
+ // the non-session renewer must NOT be created
+ verify(clientFactory,
never()).createServiceBusReceiverAsyncClient(any());
+ }
+ }
+
+ @Test
+ void sessionLockRenewerNotCreatedWhenReceiveModeIsReceiveAndDelete()
throws Exception {
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.RECEIVE_AND_DELETE);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+ verify(clientFactory,
never()).createServiceBusSessionReceiverAsyncClient(any());
+ }
+ }
+
+ @Test
+ void sessionLockRenewerNotCreatedWhenMaxAutoLockRenewDurationIsZero()
throws Exception {
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(0L);
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+ verify(clientFactory,
never()).createServiceBusSessionReceiverAsyncClient(any());
+ }
+ }
+
+ @Test
+ void sessionLockRenewerNotCreatedWhenCustomProcessorClientProvided()
throws Exception {
+ ServiceBusProcessorClient customClient = mock();
+ when(configuration.getProcessorClient()).thenReturn(customClient);
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+ verify(clientFactory,
never()).createServiceBusSessionReceiverAsyncClient(any());
+ }
+ }
+
+ @Test
+ void sessionLockRenewerAcceptsSessionAndRenewsSessionLock() throws
Exception {
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+ when(messageContext.getMessage()).thenReturn(message);
+ // lock expiring soon so the renewer triggers renewal
+
when(message.getLockedUntil()).thenReturn(OffsetDateTime.now().plusSeconds(5));
+ when(message.getSessionId()).thenReturn(MESSAGE_SESSION_ID);
+ when(sessionBoundReceiver.renewSessionLock())
+ .thenReturn(Mono.just(OffsetDateTime.now().plusMinutes(1)));
+
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+
+ ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.captor();
+
verify(lockRenewerExecutor).scheduleWithFixedDelay(taskCaptor.capture(),
anyLong(), anyLong(), any());
+ Runnable sessionRenewerTask = taskCaptor.getValue();
+
+ processMessageCaptor.getValue().accept(messageContext);
+
+ // the session renewer accepted the session via the session
receiver client
+ verify(sessionRenewalClient).acceptSession(MESSAGE_SESSION_ID);
+
+ // run the renewer: it should renew the session lock
+ sessionRenewerTask.run();
+ verify(sessionBoundReceiver).renewSessionLock();
+ }
+ }
+
+ @Test
+ void sessionLockRenewerRemovesSessionWhenLastExchangeCompletes() throws
Exception {
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+ when(messageContext.getMessage()).thenReturn(message);
+
when(message.getLockedUntil()).thenReturn(OffsetDateTime.now().plusSeconds(5));
+ when(message.getSessionId()).thenReturn(MESSAGE_SESSION_ID);
+
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+
+ ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.captor();
+
verify(lockRenewerExecutor).scheduleWithFixedDelay(taskCaptor.capture(),
anyLong(), anyLong(), any());
+ Runnable sessionRenewerTask = taskCaptor.getValue();
+
+ processMessageCaptor.getValue().accept(messageContext);
+ verify(sessionRenewalClient).acceptSession(MESSAGE_SESSION_ID);
+
+ // complete the exchange — the session-bound receiver must be
closed
+ Exchange exchange = exchangeCaptor.getValue();
+ for (Synchronization sync :
exchange.getExchangeExtension().handoverCompletions()) {
+ sync.onComplete(exchange);
+ }
+ verify(sessionBoundReceiver).close();
+
+ // run the renewer: no session in flight, no renewal attempted
+ sessionRenewerTask.run();
+ verify(sessionBoundReceiver, never()).renewSessionLock();
+ }
+ }
+
+ @Test
+ void sessionLockRenewerDoesNotRenewWhenLockHasEnoughTimeLeft() throws
Exception {
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+ when(messageContext.getMessage()).thenReturn(message);
+ // 22 seconds left — more than the LOCK_RENEW_BEFORE_EXPIRY_SECONDS
(20) threshold
+
when(message.getLockedUntil()).thenReturn(OffsetDateTime.now().plusSeconds(22));
+ when(message.getSessionId()).thenReturn(MESSAGE_SESSION_ID);
+
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+
+ ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.captor();
+
verify(lockRenewerExecutor).scheduleWithFixedDelay(taskCaptor.capture(),
anyLong(), anyLong(), any());
+
+ processMessageCaptor.getValue().accept(messageContext);
+ taskCaptor.getValue().run();
+
+ verify(sessionBoundReceiver, never()).renewSessionLock();
+ }
+ }
+
+ @Test
+ void sessionLockRenewerRetriesTransientError() throws Exception {
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+ when(messageContext.getMessage()).thenReturn(message);
+
when(message.getLockedUntil()).thenReturn(OffsetDateTime.now().plusSeconds(5));
+ when(message.getSessionId()).thenReturn(MESSAGE_SESSION_ID);
+ ServiceBusException busy = new ServiceBusException(
+ new AmqpException(true, AmqpErrorCondition.SERVER_BUSY_ERROR,
"server busy", null),
+ ServiceBusErrorSource.RENEW_LOCK);
+ when(sessionBoundReceiver.renewSessionLock())
+ .thenReturn(Mono.error(busy))
+ .thenReturn(Mono.just(OffsetDateTime.now().plusMinutes(1)));
+
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+
+ ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.captor();
+
verify(lockRenewerExecutor).scheduleWithFixedDelay(taskCaptor.capture(),
anyLong(), anyLong(), any());
+
+ processMessageCaptor.getValue().accept(messageContext);
+
+ // first run: transient error — entry kept, renewal retried next
run
+ taskCaptor.getValue().run();
+ verify(sessionBoundReceiver, times(1)).renewSessionLock();
+
+ // second run: success
+ taskCaptor.getValue().run();
+ verify(sessionBoundReceiver, times(2)).renewSessionLock();
+ }
+ }
+
+ @Test
+ void sessionLockRenewerStopsRenewingOnNonTransientError() throws Exception
{
+ when(configuration.isSessionEnabled()).thenReturn(true);
+
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+ when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+ when(messageContext.getMessage()).thenReturn(message);
+
when(message.getLockedUntil()).thenReturn(OffsetDateTime.now().plusSeconds(5));
+ when(message.getSessionId()).thenReturn(MESSAGE_SESSION_ID);
+ ServiceBusException lockLost = new ServiceBusException(
+ new AmqpException(false, AmqpErrorCondition.SESSION_LOCK_LOST,
"session lock lost", null),
+ ServiceBusErrorSource.RENEW_LOCK);
+
when(sessionBoundReceiver.renewSessionLock()).thenReturn(Mono.error(lockLost));
+
+ try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint,
processor)) {
+ consumer.doStart();
+
+ ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.captor();
+
verify(lockRenewerExecutor).scheduleWithFixedDelay(taskCaptor.capture(),
anyLong(), anyLong(), any());
+
+ processMessageCaptor.getValue().accept(messageContext);
+
+ // first run: non-transient error — renewal stops
+ taskCaptor.getValue().run();
+ // second run: session entry still present (receiver closed by
exchange completion)
+ // but the renewal is not retried
+ taskCaptor.getValue().run();
+ verify(sessionBoundReceiver, times(1)).renewSessionLock();
Review Comment:
⚠️ **Misleading comment + missing assertion.** The comment says the receiver
is closed by exchange completion, but the exchange is never completed in this
test — the non-transient error handler's `computeIfPresent` removes the entry
and closes the receiver on the first run. The second run finds an empty
`sessions` map and skips the loop.
Suggestion: fix the comment and add a `verify(sessionBoundReceiver).close()`
to assert the receiver was actually closed by the error handler:
```suggestion
// first run: non-transient error — entry removed and receiver
closed by computeIfPresent
taskCaptor.getValue().run();
verify(sessionBoundReceiver).close();
// second run: sessions map is empty (entry removed by error
handler), no renewal attempted
taskCaptor.getValue().run();
verify(sessionBoundReceiver, times(1)).renewSessionLock();
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]