use of com.aws.greengrass.mqttclient.spool.SpoolerStoreException in project aws-greengrass-nucleus by aws-greengrass.
the class MqttProxyIPCAgentTest method GIVEN_full_mqtt_spool_WHEN_publish_on_topic_THEN_service_error_thrown.
@Test
void GIVEN_full_mqtt_spool_WHEN_publish_on_topic_THEN_service_error_thrown() throws Exception {
PublishToIoTCoreRequest publishToIoTCoreRequest = new PublishToIoTCoreRequest();
publishToIoTCoreRequest.setPayload(TEST_PAYLOAD);
publishToIoTCoreRequest.setTopicName(TEST_TOPIC);
publishToIoTCoreRequest.setQos(QOS.AT_LEAST_ONCE);
CompletableFuture<Integer> f = new CompletableFuture<>();
f.completeExceptionally(new SpoolerStoreException("Spool full"));
when(mqttClient.publish(any())).thenReturn(f);
when(authorizationHandler.isAuthorized(any(), any(), any())).thenReturn(true);
try (MqttProxyIPCAgent.PublishToIoTCoreOperationHandler publishToIoTCoreOperationHandler = mqttProxyIPCAgent.getPublishToIoTCoreOperationHandler(mockContext)) {
assertThrows(ServiceError.class, () -> {
publishToIoTCoreOperationHandler.handleRequest(publishToIoTCoreRequest);
});
}
}
use of com.aws.greengrass.mqttclient.spool.SpoolerStoreException in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_reserved_topic_including_prefix_equal_to_topic_size_limit_WHEN_publish_THEN_future_complete.
@Test
void GIVEN_reserved_topic_including_prefix_equal_to_topic_size_limit_WHEN_publish_THEN_future_complete() throws SpoolerStoreException, InterruptedException, ExecutionException {
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
String topic = String.join("", Collections.nCopies(MAX_LENGTH_OF_TOPIC, "a"));
PublishRequest request = PublishRequest.builder().topic(reservedTopicPrefix + topic).payload(new byte[1]).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(0L).request(request).build();
when(spool.addMessage(request)).thenReturn(message);
CompletableFuture<Integer> future = client.publish(request);
assertEquals(0, future.get());
verify(spool, times(1)).addMessage(request);
verify(spool, never()).getSpoolConfig();
}
use of com.aws.greengrass.mqttclient.spool.SpoolerStoreException in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_qos_is_1_and_mqtt_is_offline_WHEN_publish_THEN_return_future_complete.
@Test
void GIVEN_qos_is_1_and_mqtt_is_offline_WHEN_publish_THEN_return_future_complete() throws ExecutionException, InterruptedException, SpoolerStoreException {
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
PublishRequest request = PublishRequest.builder().topic("spool").payload(new byte[0]).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(0L).request(request).build();
when(spool.addMessage(request)).thenReturn(message);
CompletableFuture<Integer> future = client.publish(request);
assertEquals(0, future.get());
verify(spool, times(1)).addMessage(request);
verify(spool, never()).getSpoolConfig();
}
use of com.aws.greengrass.mqttclient.spool.SpoolerStoreException in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClient method publish.
/**
* Publish to a MQTT topic.
*
* @param request publish request
*/
public CompletableFuture<Integer> publish(PublishRequest request) {
CompletableFuture<Integer> future = new CompletableFuture<>();
if (!deviceConfiguration.isDeviceConfiguredToTalkToCloud()) {
SpoolerStoreException e = new SpoolerStoreException("Cannot publish because device is configured to run offline");
logger.atDebug().kv("topic", request.getTopic()).log(e.getMessage());
future.completeExceptionally(e);
return future;
}
try {
isValidPublishRequest(request);
} catch (MqttRequestException e) {
logger.atError().kv("topic", request.getTopic()).log("Invalid publish request: {}", e.getMessage());
future.completeExceptionally(e);
return future;
}
boolean willDropTheRequest = !mqttOnline.get() && request.getQos().getValue() == 0 && !spool.getSpoolConfig().isKeepQos0WhenOffline();
if (willDropTheRequest) {
SpoolerStoreException e = new SpoolerStoreException("Device is offline. Dropping QoS 0 message.");
logger.atDebug().kv("topic", request.getTopic()).log(e.getMessage());
future.completeExceptionally(e);
return future;
}
try {
spool.addMessage(request);
triggerSpooler();
} catch (InterruptedException | SpoolerStoreException e) {
logger.atDebug().log("Fail to add publish request to spooler queue", e);
future.completeExceptionally(e);
return future;
}
return CompletableFuture.completedFuture(0);
}
use of com.aws.greengrass.mqttclient.spool.SpoolerStoreException in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_keep_qos_0_when_offline_is_false_and_mqtt_is_online_WHEN_publish_THEN_return_future_complete.
@Test
void GIVEN_keep_qos_0_when_offline_is_false_and_mqtt_is_online_WHEN_publish_THEN_return_future_complete() throws ExecutionException, InterruptedException, SpoolerStoreException {
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, true, (c) -> builder, executorService));
PublishRequest request = PublishRequest.builder().topic("spool").payload(new byte[0]).qos(QualityOfService.AT_MOST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(0L).request(request).build();
when(spool.addMessage(request)).thenReturn(message);
when(spool.popId()).thenThrow(InterruptedException.class);
CompletableFuture<Integer> future = client.publish(request);
assertEquals(0, future.get());
verify(spool, times(1)).addMessage(request);
verify(spool, never()).getSpoolConfig();
}
Aggregations