use of com.aws.greengrass.dependency.Context in project aws-greengrass-nucleus by aws-greengrass.
the class LogManagerHelperTest method loggers_created_before_or_after_log_level_change_get_the_correct_level.
@Test
void loggers_created_before_or_after_log_level_change_get_the_correct_level() throws IOException {
try (Context context = new Context()) {
Configuration config = new Configuration(context);
Topics logTopics = config.lookupTopics(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY, NUCLEUS_CONFIG_LOGGING_TOPICS);
when(kernel.getConfig()).thenReturn(config);
lenient().when(kernel.getNucleusPaths()).thenReturn(mock(NucleusPaths.class));
when(mockGreengrassService.getServiceName()).thenReturn("MockService3");
Logger logger1 = LogManagerHelper.getComponentLogger(mockGreengrassService);
assertFalse(logger1.isDebugEnabled());
new DeviceConfiguration(kernel);
context.runOnPublishQueueAndWait(() -> logTopics.updateFromMap(Utils.immutableMap("level", "DEBUG"), new UpdateBehaviorTree(UpdateBehaviorTree.UpdateBehavior.REPLACE, System.currentTimeMillis())));
context.waitForPublishQueueToClear();
when(mockGreengrassService.getServiceName()).thenReturn("MockService4");
Logger logger2 = LogManagerHelper.getComponentLogger(mockGreengrassService);
assertTrue(logger2.isDebugEnabled());
assertTrue(logger1.isDebugEnabled());
}
}
use of com.aws.greengrass.dependency.Context 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.dependency.Context in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_add_message_to_spooler_throw_spooler_load_exception_WHEN_publish_THEN_return_future_complete_exceptionally.
@Test
void GIVEN_add_message_to_spooler_throw_spooler_load_exception_WHEN_publish_THEN_return_future_complete_exceptionally(ExtensionContext context) throws SpoolerStoreException, InterruptedException {
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
PublishRequest request = PublishRequest.builder().topic("spool").payload(new byte[10]).qos(QualityOfService.AT_LEAST_ONCE).build();
when(spool.addMessage(any())).thenThrow(new SpoolerStoreException("spooler is full"));
ignoreExceptionOfType(context, SpoolerStoreException.class);
CompletableFuture<Integer> future = client.publish(request);
assertTrue(future.isCompletedExceptionally());
verify(spool, times(1)).addMessage(request);
verify(spool, never()).getSpoolConfig();
}
use of com.aws.greengrass.dependency.Context in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_connection_resumed_WHEN_callback_THEN_start_spool_messages.
@Test
void GIVEN_connection_resumed_WHEN_callback_THEN_start_spool_messages(ExtensionContext context) throws InterruptedException {
ignoreExceptionOfType(context, InterruptedException.class);
ignoreExceptionWithMessage(context, "interrupted");
// The mqttClient is initiated when connectivity is offline
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
Long id = 1L;
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);
// Throw an InterruptedException to break the while loop in the client.spoolMessages()
when(spool.popId()).thenReturn(id).thenThrow(new InterruptedException("interrupted"));
client.getCallbacks().onConnectionResumed(false);
// Confirm the spooler was working
verify(spool, times(1)).getMessageById(anyLong());
verify(spool, times(2)).popId();
SpoolerConfig config = SpoolerConfig.builder().spoolSizeInBytes(10L).storageType(SpoolerStorageType.Memory).keepQos0WhenOffline(false).build();
when(spool.getSpoolConfig()).thenReturn(config);
client.getCallbacks().onConnectionInterrupted(1);
verify(spool).getSpoolConfig();
verify(spool).popOutMessagesWithQosZero();
}
use of com.aws.greengrass.dependency.Context in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentStatusKeeperTest method setup.
@BeforeEach
void setup() {
context = new Context();
Configuration config = new Configuration(context);
when(deploymentService.getRuntimeConfig()).thenReturn(config.lookupTopics(GreengrassService.SERVICES_NAMESPACE_TOPIC, DeploymentService.DEPLOYMENT_SERVICE_TOPICS, GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC));
deploymentStatusKeeper = new DeploymentStatusKeeper();
deploymentStatusKeeper.setDeploymentService(deploymentService);
processedDeployments = deploymentStatusKeeper.getProcessedDeployments();
}
Aggregations