Search in sources :

Example 1 with MqttClient

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");
}
Also used : MqttClient(com.aws.greengrass.mqttclient.MqttClient) CountDownLatch(java.util.concurrent.CountDownLatch) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) Test(org.junit.jupiter.api.Test)

Example 2 with MqttClient

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);
}
Also used : MqttClient(com.aws.greengrass.mqttclient.MqttClient) Topics(com.aws.greengrass.config.Topics) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with MqttClient

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);
}
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 4 with MqttClient

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");
}
Also used : MqttClient(com.aws.greengrass.mqttclient.MqttClient) CountDownLatch(java.util.concurrent.CountDownLatch) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) Test(org.junit.jupiter.api.Test)

Aggregations

Kernel (com.aws.greengrass.lifecyclemanager.Kernel)4 MqttClient (com.aws.greengrass.mqttclient.MqttClient)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Topics (com.aws.greengrass.config.Topics)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)1 Spool (com.aws.greengrass.mqttclient.spool.Spool)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)1 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)1 AwsIotMqttConnectionBuilder (software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder)1