use of com.aws.greengrass.mqttclient.spool.Spool in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_publish_request_successfully_WHEN_spool_single_message_THEN_remove_message_from_spooler_queue.
@Test
void GIVEN_publish_request_successfully_WHEN_spool_single_message_THEN_remove_message_from_spooler_queue() throws InterruptedException {
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, true, (c) -> builder, executorService));
long id = 1L;
when(spool.popId()).thenReturn(id);
PublishRequest request = PublishRequest.builder().topic("spool").payload("What's up".getBytes(StandardCharsets.UTF_8)).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(id).request(request).build();
when(spool.getMessageById(id)).thenReturn(message);
AwsIotMqttClient awsIotMqttClient = mock(AwsIotMqttClient.class);
when(awsIotMqttClient.publish(any(), any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(0));
client.publishSingleSpoolerMessage(awsIotMqttClient);
verify(spool).removeMessageById(anyLong());
verify(awsIotMqttClient).publish(any(), any(), anyBoolean());
verify(spool, never()).addId(anyLong());
}
use of com.aws.greengrass.mqttclient.spool.Spool in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_spool_pop_id_interrupted_WHEN_spool_message_THEN_stop_spooling_message.
@Test
void GIVEN_spool_pop_id_interrupted_WHEN_spool_message_THEN_stop_spooling_message(ExtensionContext context) throws InterruptedException {
ignoreExceptionOfType(context, InterruptedException.class);
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, true, (c) -> builder, executorService));
client.setMqttOnline(true);
long id = 1L;
when(spool.popId()).thenReturn(id).thenThrow(InterruptedException.class);
PublishRequest request = PublishRequest.builder().topic("spool").payload("What's up".getBytes(StandardCharsets.UTF_8)).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(id).request(request).build();
when(spool.getMessageById(id)).thenReturn(message);
AwsIotMqttClient awsIotMqttClient = mock(AwsIotMqttClient.class);
when(client.getNewMqttClient()).thenReturn(awsIotMqttClient);
when(awsIotMqttClient.connect()).thenReturn(CompletableFuture.completedFuture(true));
when(client.getNewMqttClient()).thenReturn(awsIotMqttClient);
when(awsIotMqttClient.publish(any(), any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(0));
client.runSpooler();
verify(client).runSpooler();
verify(awsIotMqttClient).publish(any(), any(), anyBoolean());
verify(spool).getMessageById(anyLong());
verify(spool).removeMessageById(anyLong());
// The 3rd call is to trigger Interrupted Exception and exit the loop
verify(spool, times(2)).popId();
verify(client, times(2)).publishSingleSpoolerMessage(awsIotMqttClient);
}
use of com.aws.greengrass.mqttclient.spool.Spool in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_publish_request_unsuccessfully_WHEN_spool_single_message_THEN_not_retry_if_have_retried_max_times.
@Test
void GIVEN_publish_request_unsuccessfully_WHEN_spool_single_message_THEN_not_retry_if_have_retried_max_times(ExtensionContext context) throws InterruptedException {
ignoreExceptionOfType(context, ExecutionException.class);
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, true, (c) -> builder, executorService));
long id = 1L;
when(spool.popId()).thenReturn(id);
PublishRequest request = PublishRequest.builder().topic("spool").payload("What's up".getBytes(StandardCharsets.UTF_8)).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(id).request(request).build();
message.getRetried().set(DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT);
when(spool.getMessageById(id)).thenReturn(message);
AwsIotMqttClient awsIotMqttClient = mock(AwsIotMqttClient.class);
CompletableFuture<Integer> future = new CompletableFuture<>();
future.completeExceptionally(new ExecutionException("exception", new Throwable()));
when(awsIotMqttClient.publish(any(), any(), anyBoolean())).thenReturn(future);
client.publishSingleSpoolerMessage(awsIotMqttClient);
verify(awsIotMqttClient).publish(any(), any(), anyBoolean());
verify(spool, never()).removeMessageById(anyLong());
verify(spool, never()).addId(anyLong());
}
use of com.aws.greengrass.mqttclient.spool.Spool 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();
}
use of com.aws.greengrass.mqttclient.spool.Spool in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_reserved_topic_have_9_forward_slashes_WHEN_publish_THEN_future_complete.
@Test
void GIVEN_reserved_topic_have_9_forward_slashes_WHEN_publish_THEN_future_complete() throws SpoolerStoreException, InterruptedException, ExecutionException {
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
String topic = reservedTopicPrefix + String.join("/", Collections.nCopies(MAX_NUMBER_OF_FORWARD_SLASHES, "a"));
assertEquals(9, topic.chars().filter(num -> num == '/').count());
PublishRequest request = PublishRequest.builder().topic(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();
}
Aggregations