use of com.aws.greengrass.mqttclient.MqttClient in project aws-greengrass-nucleus by aws-greengrass.
the class MqttTest method GIVEN_mqttclient_WHEN_closes_new_connection_is_created_THEN_previous_session_is_invalidated.
@Test
void GIVEN_mqttclient_WHEN_closes_new_connection_is_created_THEN_previous_session_is_invalidated() throws Throwable {
kernel = new Kernel().parseArgs("-r", tempRootDir.toAbsolutePath().toString());
setDefaultRunWithUser(kernel);
deviceProvisioningHelper.updateKernelConfigWithIotConfiguration(kernel, thingInfo, GAMMA_REGION.toString(), TES_ROLE_ALIAS_NAME);
MqttClient client = kernel.getContext().get(MqttClient.class);
// subscribe to 50 topics using first connection.
int numberOfTopics = 50;
for (int i = 0; i < numberOfTopics; i++) {
client.subscribe(SubscribeRequest.builder().topic("A/" + i).callback((m) -> {
}).build());
}
// close the first connections and create a second connection.
client.close();
client = kernel.getContext().newInstance(MqttClient.class);
CountDownLatch cdl = new CountDownLatch(numberOfTopics);
// if the session from first connection is not terminated, subscribe operations made by second connection will not succeed.
for (int i = 0; i < numberOfTopics; i++) {
client.subscribe(SubscribeRequest.builder().topic("B/" + i).callback((m) -> {
cdl.countDown();
logger.atInfo().kv("remaining", cdl.getCount()).log("Received 1 message from cloud.");
}).build());
}
for (int i = 0; i < numberOfTopics; i++) {
client.publish(PublishRequest.builder().topic("B/" + i).payload("What's up".getBytes(StandardCharsets.UTF_8)).build()).get(5, TimeUnit.SECONDS);
logger.atInfo().kv("total", i + 1).log("Added 1 message to spooler.");
}
assertTrue(cdl.await(1, TimeUnit.MINUTES), "All messages published and received");
}
use of com.aws.greengrass.mqttclient.MqttClient in project aws-greengrass-nucleus by aws-greengrass.
the class IPCMqttProxyTest method beforeEach.
@BeforeEach
void beforeEach() throws Exception {
mqttClient = mock(MqttClient.class);
when(mqttClient.publish(any())).thenReturn(CompletableFuture.completedFuture(0));
System.setProperty("root", tempRootDir.toAbsolutePath().toString());
kernel = new Kernel();
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, IPCMqttProxyTest.class.getResource("mqttproxy.yaml"));
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);
}
use of com.aws.greengrass.mqttclient.MqttClient 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.MqttClient in project aws-greengrass-nucleus by aws-greengrass.
the class MqttTest method GIVEN_mqttclient_WHEN_subscribe_and_publish_THEN_receives_all_messages.
@Test
void GIVEN_mqttclient_WHEN_subscribe_and_publish_THEN_receives_all_messages() throws IOException, ExecutionException, InterruptedException, TimeoutException, DeviceConfigurationException {
kernel = new Kernel().parseArgs("-r", tempRootDir.toAbsolutePath().toString());
setDefaultRunWithUser(kernel);
deviceProvisioningHelper.updateKernelConfigWithIotConfiguration(kernel, thingInfo, GAMMA_REGION.toString(), TES_ROLE_ALIAS_NAME);
MqttClient client = kernel.getContext().get(MqttClient.class);
CountDownLatch cdl = new CountDownLatch(NUM_MESSAGES);
client.subscribe(SubscribeRequest.builder().topic("A/B/C").callback((m) -> {
cdl.countDown();
logger.atInfo().kv("remaining", cdl.getCount()).log("Received 1 message from cloud.");
}).build());
for (int i = 0; i < NUM_MESSAGES; i++) {
client.publish(PublishRequest.builder().topic("A/B/C").payload("What's up".getBytes(StandardCharsets.UTF_8)).build()).get(5, TimeUnit.SECONDS);
logger.atInfo().kv("total", i + 1).log("Added 1 message to spooler.");
}
assertTrue(cdl.await(1, TimeUnit.MINUTES), "All messages published and received");
}
Aggregations