use of com.aws.greengrass.mqttclient.spool.Spool in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientPublishTest method before.
@BeforeEach
void before() throws IOException, InterruptedException, ExecutionException {
kernel = new Kernel();
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, MqttClientPublishTest.class.getResource("config.yaml"));
config = kernel.getConfig();
deviceConfiguration = mock(DeviceConfiguration.class);
spool = mock(Spool.class);
builder = mock(AwsIotMqttConnectionBuilder.class);
connection = mock(MqttClientConnection.class);
Topics mqttNamespace = config.lookupTopics("mqtt");
when(deviceConfiguration.getMQTTNamespace()).thenReturn(mqttNamespace);
lenient().when(deviceConfiguration.isDeviceConfiguredToTalkToCloud()).thenReturn(true);
lenient().when(builder.build()).thenReturn(connection);
lenient().when(connection.connect()).thenReturn(CompletableFuture.completedFuture(false));
lenient().when(connection.disconnect()).thenReturn(CompletableFuture.completedFuture(null));
lenient().when(connection.publish(any(), any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(0));
mqttClient = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
CountDownLatch awaitIpcServiceLatch = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals(TEST_SERVICE_NAME) && newState.equals(State.FINISHED)) {
awaitIpcServiceLatch.countDown();
}
});
kernel.getContext().put(MqttClient.class, mqttClient);
kernel.launch();
assertTrue(awaitIpcServiceLatch.await(10, TimeUnit.SECONDS));
Topics servicePrivateConfig = kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC, TEST_SERVICE_NAME, PRIVATE_STORE_NAMESPACE_TOPIC);
String authToken = Coerce.toString(servicePrivateConfig.find(SERVICE_UNIQUE_ID_KEY));
socketOptions = TestUtils.getSocketOptionsForIPC();
clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel);
greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
}
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_add_id_back_to_spooler_if_will_retry.
@Test
void GIVEN_publish_request_unsuccessfully_WHEN_spool_single_message_THEN_add_id_back_to_spooler_if_will_retry(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();
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).addId(anyLong());
}
use of com.aws.greengrass.mqttclient.spool.Spool 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.Spool 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.Spool in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_publish_request_execution_exception_WHEN_spool_message_THEN_continue_spooling_message.
@Test
void GIVEN_publish_request_execution_exception_WHEN_spool_message_THEN_continue_spooling_message(ExtensionContext context) throws InterruptedException {
ignoreExceptionOfType(context, ExecutionException.class);
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).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));
CompletableFuture<Integer> future = new CompletableFuture<>();
future.completeExceptionally(new ExecutionException("exception", new Throwable()));
when(awsIotMqttClient.publish(any(), any(), anyBoolean())).thenReturn(future);
client.runSpooler();
verify(client).runSpooler();
verify(awsIotMqttClient, times(2)).publish(any(), any(), anyBoolean());
verify(spool, times(2)).getMessageById(anyLong());
verify(spool, never()).removeMessageById(anyLong());
// The 3rd call is to trigger Interrupted Exception and exit the loop
verify(spool, times(3)).popId();
verify(client, times(3)).publishSingleSpoolerMessage(awsIotMqttClient);
}
Aggregations