-
Notifications
You must be signed in to change notification settings - Fork 5.1k
MQTT: Throw Runtime Exception when manual ack fails #22117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nkokitkar
wants to merge
3
commits into
apache:main
Choose a base branch
from
nkokitkar:nkokitkar/mqtt_expFix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−2
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...t/java/org/apache/camel/component/paho/mqtt5/PahoMqtt5ConsumerManualAckExceptionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.camel.component.paho.mqtt5; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.apache.camel.Exchange; | ||
| import org.apache.camel.Processor; | ||
| import org.apache.camel.spi.ExceptionHandler; | ||
| import org.apache.camel.spi.Synchronization; | ||
| import org.apache.camel.test.junit6.CamelTestSupport; | ||
| import org.eclipse.paho.mqttv5.client.MqttClient; | ||
| import org.eclipse.paho.mqttv5.common.MqttException; | ||
| import org.eclipse.paho.mqttv5.common.MqttMessage; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.contains; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
|
|
||
| public class PahoMqtt5ConsumerManualAckExceptionTest extends CamelTestSupport { | ||
|
|
||
| @Test | ||
| public void testOnCompleteCallsExceptionHandlerOnMqttException() throws Exception { | ||
| PahoMqtt5Endpoint endpoint = context.getEndpoint( | ||
| "paho-mqtt5:test?manualAcksEnabled=true&brokerUrl=tcp://localhost:1883", PahoMqtt5Endpoint.class); | ||
|
|
||
| Processor mockProcessor = mock(Processor.class); | ||
| PahoMqtt5Consumer consumer = new PahoMqtt5Consumer(endpoint, mockProcessor); | ||
|
|
||
| MqttClient mockClient = mock(MqttClient.class); | ||
| doThrow(new MqttException(MqttException.REASON_CODE_CLIENT_EXCEPTION)) | ||
| .when(mockClient).messageArrivedComplete(any(int.class), any(int.class)); | ||
| consumer.setClient(mockClient); | ||
|
|
||
| ExceptionHandler mockExceptionHandler = mock(ExceptionHandler.class); | ||
| consumer.setExceptionHandler(mockExceptionHandler); | ||
|
|
||
| MqttMessage mqttMessage = new MqttMessage("test".getBytes()); | ||
| mqttMessage.setQos(2); | ||
|
|
||
| Exchange exchange = consumer.createExchange(mqttMessage, "test-topic"); | ||
| List<Synchronization> completions = exchange.getExchangeExtension().handoverCompletions(); | ||
|
|
||
| assertNotNull(completions); | ||
| assertFalse(completions.isEmpty()); | ||
|
|
||
| // Trigger the onComplete callback (simulating successful exchange processing) | ||
| completions.get(0).onComplete(exchange); | ||
|
|
||
| // Verify exception handler was called instead of throwing RuntimeException | ||
| verify(mockExceptionHandler).handleException( | ||
| contains("Error acknowledging MQTT message"), | ||
| eq(exchange), | ||
| any(MqttException.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOnCompleteAcknowledgesSuccessfully() throws Exception { | ||
| PahoMqtt5Endpoint endpoint = context.getEndpoint( | ||
| "paho-mqtt5:test?manualAcksEnabled=true&brokerUrl=tcp://localhost:1883", PahoMqtt5Endpoint.class); | ||
|
|
||
| Processor mockProcessor = mock(Processor.class); | ||
| PahoMqtt5Consumer consumer = new PahoMqtt5Consumer(endpoint, mockProcessor); | ||
|
|
||
| MqttClient mockClient = mock(MqttClient.class); | ||
| consumer.setClient(mockClient); | ||
|
|
||
| ExceptionHandler mockExceptionHandler = mock(ExceptionHandler.class); | ||
| consumer.setExceptionHandler(mockExceptionHandler); | ||
|
|
||
| MqttMessage mqttMessage = new MqttMessage("test".getBytes()); | ||
| mqttMessage.setQos(2); | ||
|
|
||
| Exchange exchange = consumer.createExchange(mqttMessage, "test-topic"); | ||
| List<Synchronization> completions = exchange.getExchangeExtension().handoverCompletions(); | ||
|
|
||
| assertNotNull(completions); | ||
| assertEquals(1, completions.size()); | ||
|
|
||
| // Trigger the onComplete callback | ||
| completions.get(0).onComplete(exchange); | ||
|
|
||
| // Verify messageArrivedComplete was called and no exception was handled | ||
| verify(mockClient).messageArrivedComplete(mqttMessage.getId(), mqttMessage.getQos()); | ||
| verifyNoInteractions(mockExceptionHandler); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoCompletionRegisteredWhenManualAcksDisabled() throws Exception { | ||
| PahoMqtt5Endpoint endpoint = context.getEndpoint( | ||
| "paho-mqtt5:test?brokerUrl=tcp://localhost:1883", PahoMqtt5Endpoint.class); | ||
|
|
||
| Processor mockProcessor = mock(Processor.class); | ||
| PahoMqtt5Consumer consumer = new PahoMqtt5Consumer(endpoint, mockProcessor); | ||
|
|
||
| MqttClient mockClient = mock(MqttClient.class); | ||
| consumer.setClient(mockClient); | ||
|
|
||
| MqttMessage mqttMessage = new MqttMessage("test".getBytes()); | ||
|
|
||
| Exchange exchange = consumer.createExchange(mqttMessage, "test-topic"); | ||
| List<Synchronization> completions = exchange.getExchangeExtension().handoverCompletions(); | ||
|
|
||
| // No synchronization should be registered when manualAcks is disabled | ||
| assertTrue(completions == null || completions.isEmpty()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...aho/src/test/java/org/apache/camel/component/paho/PahoConsumerManualAckExceptionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.camel.component.paho; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.apache.camel.Exchange; | ||
| import org.apache.camel.Processor; | ||
| import org.apache.camel.spi.ExceptionHandler; | ||
| import org.apache.camel.spi.Synchronization; | ||
| import org.apache.camel.test.junit6.CamelTestSupport; | ||
| import org.eclipse.paho.client.mqttv3.MqttClient; | ||
| import org.eclipse.paho.client.mqttv3.MqttException; | ||
| import org.eclipse.paho.client.mqttv3.MqttMessage; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.contains; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
|
|
||
| public class PahoConsumerManualAckExceptionTest extends CamelTestSupport { | ||
|
|
||
| @Test | ||
| public void testOnCompleteCallsExceptionHandlerOnMqttException() throws Exception { | ||
| PahoEndpoint endpoint = context.getEndpoint( | ||
| "paho:test?manualAcksEnabled=true&brokerUrl=tcp://localhost:1883", PahoEndpoint.class); | ||
|
|
||
| Processor mockProcessor = mock(Processor.class); | ||
| PahoConsumer consumer = new PahoConsumer(endpoint, mockProcessor); | ||
|
|
||
| MqttClient mockClient = mock(MqttClient.class); | ||
| doThrow(new MqttException(MqttException.REASON_CODE_CLIENT_EXCEPTION)) | ||
| .when(mockClient).messageArrivedComplete(any(int.class), any(int.class)); | ||
| consumer.setClient(mockClient); | ||
|
|
||
| ExceptionHandler mockExceptionHandler = mock(ExceptionHandler.class); | ||
| consumer.setExceptionHandler(mockExceptionHandler); | ||
|
|
||
| MqttMessage mqttMessage = new MqttMessage("test".getBytes()); | ||
| mqttMessage.setQos(2); | ||
|
|
||
| Exchange exchange = consumer.createExchange(mqttMessage, "test-topic"); | ||
| List<Synchronization> completions = exchange.getExchangeExtension().handoverCompletions(); | ||
|
|
||
| assertNotNull(completions); | ||
| assertFalse(completions.isEmpty()); | ||
|
|
||
| // Trigger the onComplete callback (simulating successful exchange processing) | ||
| completions.get(0).onComplete(exchange); | ||
|
|
||
| // Verify exception handler was called instead of throwing RuntimeException | ||
| verify(mockExceptionHandler).handleException( | ||
| contains("Error acknowledging MQTT message"), | ||
| eq(exchange), | ||
| any(MqttException.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOnCompleteAcknowledgesSuccessfully() throws Exception { | ||
| PahoEndpoint endpoint = context.getEndpoint( | ||
| "paho:test?manualAcksEnabled=true&brokerUrl=tcp://localhost:1883", PahoEndpoint.class); | ||
|
|
||
| Processor mockProcessor = mock(Processor.class); | ||
| PahoConsumer consumer = new PahoConsumer(endpoint, mockProcessor); | ||
|
|
||
| MqttClient mockClient = mock(MqttClient.class); | ||
| consumer.setClient(mockClient); | ||
|
|
||
| ExceptionHandler mockExceptionHandler = mock(ExceptionHandler.class); | ||
| consumer.setExceptionHandler(mockExceptionHandler); | ||
|
|
||
| MqttMessage mqttMessage = new MqttMessage("test".getBytes()); | ||
| mqttMessage.setQos(2); | ||
|
|
||
| Exchange exchange = consumer.createExchange(mqttMessage, "test-topic"); | ||
| List<Synchronization> completions = exchange.getExchangeExtension().handoverCompletions(); | ||
|
|
||
| assertNotNull(completions); | ||
| assertEquals(1, completions.size()); | ||
|
|
||
| // Trigger the onComplete callback | ||
| completions.get(0).onComplete(exchange); | ||
|
|
||
| // Verify messageArrivedComplete was called and no exception was handled | ||
| verify(mockClient).messageArrivedComplete(mqttMessage.getId(), mqttMessage.getQos()); | ||
| verifyNoInteractions(mockExceptionHandler); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoCompletionRegisteredWhenManualAcksDisabled() throws Exception { | ||
| PahoEndpoint endpoint = context.getEndpoint( | ||
| "paho:test?brokerUrl=tcp://localhost:1883", PahoEndpoint.class); | ||
|
|
||
| Processor mockProcessor = mock(Processor.class); | ||
| PahoConsumer consumer = new PahoConsumer(endpoint, mockProcessor); | ||
|
|
||
| MqttClient mockClient = mock(MqttClient.class); | ||
| consumer.setClient(mockClient); | ||
|
|
||
| MqttMessage mqttMessage = new MqttMessage("test".getBytes()); | ||
|
|
||
| Exchange exchange = consumer.createExchange(mqttMessage, "test-topic"); | ||
| List<Synchronization> completions = exchange.getExchangeExtension().handoverCompletions(); | ||
|
|
||
| // No synchronization should be registered when manualAcks is disabled | ||
| assertTrue(completions == null || completions.isEmpty()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why
debuginstead oferror?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted