Search in sources :

Example 1 with Spool

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);
}
Also used : MqttClient(com.aws.greengrass.mqttclient.MqttClient) Topics(com.aws.greengrass.config.Topics) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) AwsIotMqttConnectionBuilder(software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder) Spool(com.aws.greengrass.mqttclient.spool.Spool) CountDownLatch(java.util.concurrent.CountDownLatch) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with Spool

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());
}
Also used : SpoolerStoreException(com.aws.greengrass.mqttclient.spool.SpoolerStoreException) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Matchers.either(org.hamcrest.Matchers.either) DEVICE_PARAM_ROOT_CA_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_ROOT_CA_PATH) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TimeoutException(java.util.concurrent.TimeoutException) SpoolMessage(com.aws.greengrass.mqttclient.spool.SpoolMessage) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AwsIotMqttConnectionBuilder(software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder) TestUtils.asyncAssertOnConsumer(com.aws.greengrass.testcommons.testutilities.TestUtils.asyncAssertOnConsumer) DEVICE_PARAM_PRIVATE_KEY_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_PRIVATE_KEY_PATH) Configuration(com.aws.greengrass.config.Configuration) SpoolerStorageType(com.aws.greengrass.mqttclient.spool.SpoolerStorageType) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT(com.aws.greengrass.mqttclient.MqttClient.DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TestUtils(com.aws.greengrass.testcommons.testutilities.TestUtils) Mockito.doNothing(org.mockito.Mockito.doNothing) ExceptionLogProtector.ignoreExceptionOfType(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionOfType) Pair(com.aws.greengrass.util.Pair) StandardCharsets(java.nio.charset.StandardCharsets) ChildChanged(com.aws.greengrass.config.ChildChanged) Test(org.junit.jupiter.api.Test) Topics(com.aws.greengrass.config.Topics) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) List(java.util.List) DEVICE_PARAM_CERTIFICATE_FILE_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_CERTIFICATE_FILE_PATH) MAX_NUMBER_OF_FORWARD_SLASHES(com.aws.greengrass.mqttclient.MqttClient.MAX_NUMBER_OF_FORWARD_SLASHES) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DEVICE_PARAM_AWS_REGION(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_AWS_REGION) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) CompletableFuture(java.util.concurrent.CompletableFuture) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Mockito.lenient(org.mockito.Mockito.lenient) SpoolerConfig(com.aws.greengrass.mqttclient.spool.SpoolerConfig) MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES(com.aws.greengrass.mqttclient.MqttClient.MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES) Mockito.timeout(org.mockito.Mockito.timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Context(com.aws.greengrass.dependency.Context) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) DEVICE_PARAM_IOT_DATA_ENDPOINT(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_IOT_DATA_ENDPOINT) DEVICE_MQTT_NAMESPACE(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_MQTT_NAMESPACE) Spool(com.aws.greengrass.mqttclient.spool.Spool) DEVICE_PARAM_THING_NAME(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_THING_NAME) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ExceptionLogProtector.ignoreExceptionWithMessage(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionWithMessage) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) WhatHappened(com.aws.greengrass.config.WhatHappened) Collections(java.util.Collections) MAX_LENGTH_OF_TOPIC(com.aws.greengrass.mqttclient.MqttClient.MAX_LENGTH_OF_TOPIC) CompletableFuture(java.util.concurrent.CompletableFuture) SpoolMessage(com.aws.greengrass.mqttclient.spool.SpoolMessage) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 3 with Spool

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();
}
Also used : SpoolerStoreException(com.aws.greengrass.mqttclient.spool.SpoolerStoreException) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Matchers.either(org.hamcrest.Matchers.either) DEVICE_PARAM_ROOT_CA_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_ROOT_CA_PATH) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TimeoutException(java.util.concurrent.TimeoutException) SpoolMessage(com.aws.greengrass.mqttclient.spool.SpoolMessage) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AwsIotMqttConnectionBuilder(software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder) TestUtils.asyncAssertOnConsumer(com.aws.greengrass.testcommons.testutilities.TestUtils.asyncAssertOnConsumer) DEVICE_PARAM_PRIVATE_KEY_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_PRIVATE_KEY_PATH) Configuration(com.aws.greengrass.config.Configuration) SpoolerStorageType(com.aws.greengrass.mqttclient.spool.SpoolerStorageType) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT(com.aws.greengrass.mqttclient.MqttClient.DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TestUtils(com.aws.greengrass.testcommons.testutilities.TestUtils) Mockito.doNothing(org.mockito.Mockito.doNothing) ExceptionLogProtector.ignoreExceptionOfType(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionOfType) Pair(com.aws.greengrass.util.Pair) StandardCharsets(java.nio.charset.StandardCharsets) ChildChanged(com.aws.greengrass.config.ChildChanged) Test(org.junit.jupiter.api.Test) Topics(com.aws.greengrass.config.Topics) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) List(java.util.List) DEVICE_PARAM_CERTIFICATE_FILE_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_CERTIFICATE_FILE_PATH) MAX_NUMBER_OF_FORWARD_SLASHES(com.aws.greengrass.mqttclient.MqttClient.MAX_NUMBER_OF_FORWARD_SLASHES) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DEVICE_PARAM_AWS_REGION(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_AWS_REGION) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) CompletableFuture(java.util.concurrent.CompletableFuture) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Mockito.lenient(org.mockito.Mockito.lenient) SpoolerConfig(com.aws.greengrass.mqttclient.spool.SpoolerConfig) MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES(com.aws.greengrass.mqttclient.MqttClient.MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES) Mockito.timeout(org.mockito.Mockito.timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Context(com.aws.greengrass.dependency.Context) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) DEVICE_PARAM_IOT_DATA_ENDPOINT(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_IOT_DATA_ENDPOINT) DEVICE_MQTT_NAMESPACE(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_MQTT_NAMESPACE) Spool(com.aws.greengrass.mqttclient.spool.Spool) DEVICE_PARAM_THING_NAME(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_THING_NAME) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ExceptionLogProtector.ignoreExceptionWithMessage(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionWithMessage) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) WhatHappened(com.aws.greengrass.config.WhatHappened) Collections(java.util.Collections) MAX_LENGTH_OF_TOPIC(com.aws.greengrass.mqttclient.MqttClient.MAX_LENGTH_OF_TOPIC) SpoolMessage(com.aws.greengrass.mqttclient.spool.SpoolMessage) Test(org.junit.jupiter.api.Test)

Example 4 with Spool

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();
}
Also used : SpoolerStoreException(com.aws.greengrass.mqttclient.spool.SpoolerStoreException) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Matchers.either(org.hamcrest.Matchers.either) DEVICE_PARAM_ROOT_CA_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_ROOT_CA_PATH) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TimeoutException(java.util.concurrent.TimeoutException) SpoolMessage(com.aws.greengrass.mqttclient.spool.SpoolMessage) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AwsIotMqttConnectionBuilder(software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder) TestUtils.asyncAssertOnConsumer(com.aws.greengrass.testcommons.testutilities.TestUtils.asyncAssertOnConsumer) DEVICE_PARAM_PRIVATE_KEY_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_PRIVATE_KEY_PATH) Configuration(com.aws.greengrass.config.Configuration) SpoolerStorageType(com.aws.greengrass.mqttclient.spool.SpoolerStorageType) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT(com.aws.greengrass.mqttclient.MqttClient.DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TestUtils(com.aws.greengrass.testcommons.testutilities.TestUtils) Mockito.doNothing(org.mockito.Mockito.doNothing) ExceptionLogProtector.ignoreExceptionOfType(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionOfType) Pair(com.aws.greengrass.util.Pair) StandardCharsets(java.nio.charset.StandardCharsets) ChildChanged(com.aws.greengrass.config.ChildChanged) Test(org.junit.jupiter.api.Test) Topics(com.aws.greengrass.config.Topics) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) List(java.util.List) DEVICE_PARAM_CERTIFICATE_FILE_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_CERTIFICATE_FILE_PATH) MAX_NUMBER_OF_FORWARD_SLASHES(com.aws.greengrass.mqttclient.MqttClient.MAX_NUMBER_OF_FORWARD_SLASHES) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DEVICE_PARAM_AWS_REGION(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_AWS_REGION) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) CompletableFuture(java.util.concurrent.CompletableFuture) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Mockito.lenient(org.mockito.Mockito.lenient) SpoolerConfig(com.aws.greengrass.mqttclient.spool.SpoolerConfig) MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES(com.aws.greengrass.mqttclient.MqttClient.MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES) Mockito.timeout(org.mockito.Mockito.timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Context(com.aws.greengrass.dependency.Context) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) DEVICE_PARAM_IOT_DATA_ENDPOINT(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_IOT_DATA_ENDPOINT) DEVICE_MQTT_NAMESPACE(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_MQTT_NAMESPACE) Spool(com.aws.greengrass.mqttclient.spool.Spool) DEVICE_PARAM_THING_NAME(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_THING_NAME) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ExceptionLogProtector.ignoreExceptionWithMessage(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionWithMessage) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) WhatHappened(com.aws.greengrass.config.WhatHappened) Collections(java.util.Collections) MAX_LENGTH_OF_TOPIC(com.aws.greengrass.mqttclient.MqttClient.MAX_LENGTH_OF_TOPIC) SpoolMessage(com.aws.greengrass.mqttclient.spool.SpoolMessage) Test(org.junit.jupiter.api.Test)

Example 5 with Spool

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);
}
Also used : SpoolerStoreException(com.aws.greengrass.mqttclient.spool.SpoolerStoreException) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Matchers.either(org.hamcrest.Matchers.either) DEVICE_PARAM_ROOT_CA_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_ROOT_CA_PATH) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TimeoutException(java.util.concurrent.TimeoutException) SpoolMessage(com.aws.greengrass.mqttclient.spool.SpoolMessage) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AwsIotMqttConnectionBuilder(software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder) TestUtils.asyncAssertOnConsumer(com.aws.greengrass.testcommons.testutilities.TestUtils.asyncAssertOnConsumer) DEVICE_PARAM_PRIVATE_KEY_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_PRIVATE_KEY_PATH) Configuration(com.aws.greengrass.config.Configuration) SpoolerStorageType(com.aws.greengrass.mqttclient.spool.SpoolerStorageType) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT(com.aws.greengrass.mqttclient.MqttClient.DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TestUtils(com.aws.greengrass.testcommons.testutilities.TestUtils) Mockito.doNothing(org.mockito.Mockito.doNothing) ExceptionLogProtector.ignoreExceptionOfType(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionOfType) Pair(com.aws.greengrass.util.Pair) StandardCharsets(java.nio.charset.StandardCharsets) ChildChanged(com.aws.greengrass.config.ChildChanged) Test(org.junit.jupiter.api.Test) Topics(com.aws.greengrass.config.Topics) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) List(java.util.List) DEVICE_PARAM_CERTIFICATE_FILE_PATH(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_CERTIFICATE_FILE_PATH) MAX_NUMBER_OF_FORWARD_SLASHES(com.aws.greengrass.mqttclient.MqttClient.MAX_NUMBER_OF_FORWARD_SLASHES) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DEVICE_PARAM_AWS_REGION(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_AWS_REGION) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) CompletableFuture(java.util.concurrent.CompletableFuture) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Mockito.lenient(org.mockito.Mockito.lenient) SpoolerConfig(com.aws.greengrass.mqttclient.spool.SpoolerConfig) MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES(com.aws.greengrass.mqttclient.MqttClient.MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES) Mockito.timeout(org.mockito.Mockito.timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) GGExtension(com.aws.greengrass.testcommons.testutilities.GGExtension) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Context(com.aws.greengrass.dependency.Context) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) DEVICE_PARAM_IOT_DATA_ENDPOINT(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_IOT_DATA_ENDPOINT) DEVICE_MQTT_NAMESPACE(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_MQTT_NAMESPACE) Spool(com.aws.greengrass.mqttclient.spool.Spool) DEVICE_PARAM_THING_NAME(com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_THING_NAME) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ExceptionLogProtector.ignoreExceptionWithMessage(com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionWithMessage) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) WhatHappened(com.aws.greengrass.config.WhatHappened) Collections(java.util.Collections) MAX_LENGTH_OF_TOPIC(com.aws.greengrass.mqttclient.MqttClient.MAX_LENGTH_OF_TOPIC) CompletableFuture(java.util.concurrent.CompletableFuture) SpoolMessage(com.aws.greengrass.mqttclient.spool.SpoolMessage) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

Topics (com.aws.greengrass.config.Topics)14 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)14 Spool (com.aws.greengrass.mqttclient.spool.Spool)14 ChildChanged (com.aws.greengrass.config.ChildChanged)13 Configuration (com.aws.greengrass.config.Configuration)13 WhatHappened (com.aws.greengrass.config.WhatHappened)13 Context (com.aws.greengrass.dependency.Context)13 DEVICE_MQTT_NAMESPACE (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_MQTT_NAMESPACE)13 DEVICE_PARAM_AWS_REGION (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_AWS_REGION)13 DEVICE_PARAM_CERTIFICATE_FILE_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_CERTIFICATE_FILE_PATH)13 DEVICE_PARAM_IOT_DATA_ENDPOINT (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_IOT_DATA_ENDPOINT)13 DEVICE_PARAM_PRIVATE_KEY_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_PRIVATE_KEY_PATH)13 DEVICE_PARAM_ROOT_CA_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_ROOT_CA_PATH)13 DEVICE_PARAM_THING_NAME (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_THING_NAME)13 DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT (com.aws.greengrass.mqttclient.MqttClient.DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT)13 MAX_LENGTH_OF_TOPIC (com.aws.greengrass.mqttclient.MqttClient.MAX_LENGTH_OF_TOPIC)13 MAX_NUMBER_OF_FORWARD_SLASHES (com.aws.greengrass.mqttclient.MqttClient.MAX_NUMBER_OF_FORWARD_SLASHES)13 MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES (com.aws.greengrass.mqttclient.MqttClient.MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES)13 SpoolMessage (com.aws.greengrass.mqttclient.spool.SpoolMessage)13 SpoolerConfig (com.aws.greengrass.mqttclient.spool.SpoolerConfig)13