Search in sources :

Example 1 with Success

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.Success in project azure-iot-sdk-java by Azure.

the class ReceiveMessagesTests method receiveMessage.

public void receiveMessage(int messageSize) throws Exception {
    if (testInstance.protocol == HTTPS) {
        testInstance.identity.getClient().setOption(SET_MINIMUM_POLLING_INTERVAL, ONE_SECOND_POLLING_INTERVAL);
    }
    testInstance.identity.getClient().open();
    Message serviceMessage = createCloudToDeviceMessage(messageSize);
    com.microsoft.azure.sdk.iot.device.MessageCallback callback = new MessageCallback(serviceMessage);
    if (testInstance.protocol == MQTT || testInstance.protocol == MQTT_WS) {
        callback = new MessageCallbackMqtt(serviceMessage);
    }
    Success messageReceived = new Success();
    if (testInstance.identity.getClient() instanceof DeviceClient) {
        ((DeviceClient) testInstance.identity.getClient()).setMessageCallback(callback, messageReceived);
    } else if (testInstance.identity.getClient() instanceof ModuleClient) {
        ((ModuleClient) testInstance.identity.getClient()).setMessageCallback(callback, messageReceived);
    }
    if (testInstance.identity.getClient() instanceof DeviceClient) {
        testInstance.serviceClient.send(testInstance.identity.getDeviceId(), serviceMessage);
    } else if (testInstance.identity.getClient() instanceof ModuleClient) {
        testInstance.serviceClient.send(testInstance.identity.getDeviceId(), ((TestModuleIdentity) testInstance.identity).getModuleId(), serviceMessage);
    }
    waitForMessageToBeReceived(messageReceived, testInstance.protocol.toString());
    Thread.sleep(200);
    testInstance.identity.getClient().closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient) Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success) TestModuleIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity)

Example 2 with Success

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.Success in project azure-iot-sdk-java by Azure.

the class SendMessagesTests method sendMessagesFromConnectionStatusChangeCallback.

// Ensure that a user can use a device/module client as soon as the connection status callback executes with status CONNECTED
// even from the callback itself
@Test
public void sendMessagesFromConnectionStatusChangeCallback() throws Exception {
    Success messageSent = new Success();
    messageSent.setResult(false);
    testInstance.setup();
    testInstance.identity.getClient().registerConnectionStatusChangeCallback((status, statusChangeReason, throwable, callbackContext) -> {
        if (status == IotHubConnectionStatus.CONNECTED) {
            try {
                testInstance.identity.getClient().sendEventAsync(new Message("test message"), (responseStatus, callbackContext1) -> {
                    ((Success) callbackContext1).setResult(true);
                    ((Success) callbackContext1).setCallbackStatusCode(responseStatus);
                    ((Success) callbackContext1).callbackWasFired();
                }, messageSent);
            } catch (Exception e) {
                log.error("Encountered an error when sending the message", e);
                messageSent.setResult(false);
                messageSent.setCallbackStatusCode(IotHubStatusCode.ERROR);
                messageSent.callbackWasFired();
            }
        }
    }, null);
    testInstance.identity.getClient().open();
    long startTime = System.currentTimeMillis();
    while (!messageSent.wasCallbackFired()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > SEND_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for sent message to be acknowledged");
        }
    }
    assertTrue(messageSent.getResult());
    testInstance.identity.getClient().closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) Test(org.junit.Test)

Example 3 with Success

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.Success in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method multiplexedConnectionRecoversFromTcpConnectionDrop.

// Open a multiplexed connection, send a fault injection message to drop the TCP connection, and ensure that the multiplexed
// connection recovers
@Test
@ErrInjTest
@IotHubTest
public void multiplexedConnectionRecoversFromTcpConnectionDrop() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    ConnectionStatusChangeTracker multiplexedConnectionStatusChangeTracker = new ConnectionStatusChangeTracker();
    ConnectionStatusChangeTracker[] connectionStatusChangeTrackers = new ConnectionStatusChangeTracker[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        connectionStatusChangeTrackers[i] = new ConnectionStatusChangeTracker();
        testInstance.deviceClientArray.get(i).registerConnectionStatusChangeCallback(connectionStatusChangeTrackers[i], null);
    }
    testInstance.multiplexingClient.registerConnectionStatusChangeCallback(multiplexedConnectionStatusChangeTracker, null);
    testInstance.multiplexingClient.open();
    assertTrue("Multiplexed level connection status callback should have fired with CONNECTED after opening the multiplexing client", multiplexedConnectionStatusChangeTracker.isOpen);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        assertTrue("Multiplexing client opened successfully, but connection status change callback didn't execute.", connectionStatusChangeTrackers[i].isOpen);
    }
    Message errorInjectionMessage = ErrorInjectionHelper.tcpConnectionDropErrorInjectionMessage(1, 10);
    Success messageSendSuccess = testSendingMessageFromDeviceClient(testInstance.deviceClientArray.get(0), errorInjectionMessage);
    waitForMessageToBeAcknowledged(messageSendSuccess, "Timed out waiting for error injection message to be acknowledged");
    // Now that error injection message has been sent, need to wait for the device session to drop
    // Every registered device level connection status change callback should have fired with DISCONNECTED_RETRYING
    // and so should the multiplexing level connection status change callback
    assertConnectionStateCallbackFiredDisconnectedRetrying(multiplexedConnectionStatusChangeTracker);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        assertConnectionStateCallbackFiredDisconnectedRetrying(connectionStatusChangeTrackers[i]);
    }
    // Now that the fault injection has taken place, make sure that the multiplexed connection and all of its device
    // sessions recover. Once recovered, try sending telemetry on each device.
    assertConnectionStateCallbackFiredConnected(multiplexedConnectionStatusChangeTracker, FAULT_INJECTION_RECOVERY_TIMEOUT_MILLIS);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // The faulted device should eventually recover
        assertConnectionStateCallbackFiredConnected(connectionStatusChangeTrackers[i], FAULT_INJECTION_RECOVERY_TIMEOUT_MILLIS);
        // Try to send a message over the now-recovered device session
        testSendingMessageFromDeviceClient(testInstance.deviceClientArray.get(i));
    }
    // double check that the recovery of any particular device did not cause a device earlier in the array to lose connection
    testSendingMessagesFromMultiplexedClients(testInstance.deviceClientArray);
    testInstance.multiplexingClient.close();
    assertTrue("Multiplexed level connection status callback should have fired with DISCONNECTED after closing the multiplexing client", multiplexedConnectionStatusChangeTracker.clientClosedGracefully);
    assertMultiplexedDevicesClosedGracefully(connectionStatusChangeTrackers);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 4 with Success

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.Success in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method multiplexedConnectionRecoversFromDeviceSessionDropsSequential.

// Fault every device session, wait for it to recover, test sending from it, and verify that no other device sessions were dropped
// other than the deliberately dropped session.
@Test
@ErrInjTest
@IotHubTest
public void multiplexedConnectionRecoversFromDeviceSessionDropsSequential() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    ConnectionStatusChangeTracker multiplexedConnectionStatusChangeTracker = new ConnectionStatusChangeTracker();
    testInstance.multiplexingClient.registerConnectionStatusChangeCallback(multiplexedConnectionStatusChangeTracker, null);
    ConnectionStatusChangeTracker[] connectionStatusChangeTrackers = new ConnectionStatusChangeTracker[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        connectionStatusChangeTrackers[i] = new ConnectionStatusChangeTracker();
        testInstance.deviceClientArray.get(i).registerConnectionStatusChangeCallback(connectionStatusChangeTrackers[i], null);
    }
    testInstance.multiplexingClient.open();
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        assertTrue("Multiplexing client opened successfully, but connection status change callback didn't execute.", connectionStatusChangeTrackers[i].isOpen);
    }
    // For each multiplexed device, use fault injection to drop the session and see if it can recover, one device at a time
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        log.info("Starting loop for device {}", testInstance.deviceClientArray.get(i).getConfig().getDeviceId());
        Message errorInjectionMessage = ErrorInjectionHelper.amqpsSessionDropErrorInjectionMessage(1, 10);
        Success messageSendSuccess = testSendingMessageFromDeviceClient(testInstance.deviceClientArray.get(i), errorInjectionMessage);
        waitForMessageToBeAcknowledged(messageSendSuccess, "Timed out waiting for error injection message to be acknowledged");
        // Now that error injection message has been sent, need to wait for the device session to drop
        assertConnectionStateCallbackFiredDisconnectedRetrying(connectionStatusChangeTrackers[i]);
        // Next, the faulted device should eventually recover
        log.info("Waiting for device {} to reconnect", testInstance.deviceClientArray.get(i).getConfig().getDeviceId());
        assertConnectionStateCallbackFiredConnected(connectionStatusChangeTrackers[i], FAULT_INJECTION_RECOVERY_TIMEOUT_MILLIS);
        for (int j = i + 1; j < DEVICE_MULTIPLEX_COUNT; j++) {
            // devices above index i have not been deliberately faulted yet, so make sure they haven't seen a DISCONNECTED_RETRYING event yet.
            assertFalse("Multiplexed device that hasn't been deliberately faulted yet saw an unexpected DISCONNECTED_RETRYING connection status callback", connectionStatusChangeTrackers[j].wentDisconnectedRetrying);
        }
        // Try to send a message over the now-recovered device session
        testSendingMessageFromDeviceClient(testInstance.deviceClientArray.get(i));
    }
    // double check that the recovery of any particular device did not cause a device earlier in the array to lose connection
    testSendingMessagesFromMultiplexedClients(testInstance.deviceClientArray);
    assertFalse(multiplexedConnectionStatusChangeTracker.wentDisconnectedRetrying);
    testInstance.multiplexingClient.close();
    assertMultiplexedDevicesClosedGracefully(connectionStatusChangeTrackers);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 5 with Success

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.Success in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method multiplexedConnectionRecoversFromDeviceSessionDropsParallel.

// Fault every device session basically at once, make sure that the clients all recover
@Test
@ErrInjTest
@IotHubTest
public void multiplexedConnectionRecoversFromDeviceSessionDropsParallel() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    ConnectionStatusChangeTracker[] connectionStatusChangeTrackers = new ConnectionStatusChangeTracker[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        connectionStatusChangeTrackers[i] = new ConnectionStatusChangeTracker();
        testInstance.deviceClientArray.get(i).registerConnectionStatusChangeCallback(connectionStatusChangeTrackers[i], null);
    }
    testInstance.multiplexingClient.open();
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        assertTrue("Multiplexing client opened successfully, but connection status change callback didn't execute.", connectionStatusChangeTrackers[i].isOpen);
    }
    // For each multiplexed device, use fault injection to drop the session and see if it can recover, one device at a time
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        Message errorInjectionMessage = ErrorInjectionHelper.amqpsSessionDropErrorInjectionMessage(1, 10);
        Success messageSendSuccess = testSendingMessageFromDeviceClient(testInstance.deviceClientArray.get(i), errorInjectionMessage);
        waitForMessageToBeAcknowledged(messageSendSuccess, "Timed out waiting for error injection message to be acknowledged");
    }
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // Now that error injection message has been sent, need to wait for the device session to drop
        assertConnectionStateCallbackFiredDisconnectedRetrying(connectionStatusChangeTrackers[i]);
        // Next, the faulted device should eventually recover
        assertConnectionStateCallbackFiredConnected(connectionStatusChangeTrackers[i], FAULT_INJECTION_RECOVERY_TIMEOUT_MILLIS);
        // Try to send a message over the now-recovered device session
        testSendingMessageFromDeviceClient(testInstance.deviceClientArray.get(i));
    }
    // double check that the recovery of any particular device did not cause a device earlier in the array to lose connection
    testSendingMessagesFromMultiplexedClients(testInstance.deviceClientArray);
    testInstance.multiplexingClient.close();
    assertMultiplexedDevicesClosedGracefully(connectionStatusChangeTrackers);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Aggregations

Success (tests.integration.com.microsoft.azure.sdk.iot.helpers.Success)9 Message (com.microsoft.azure.sdk.iot.device.Message)5 Test (org.junit.Test)5 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)4 IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)2 EventCallback (tests.integration.com.microsoft.azure.sdk.iot.helpers.EventCallback)2 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)1 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 Message (com.microsoft.azure.sdk.iot.service.Message)1 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)1 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)1 TestModuleIdentity (tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity)1 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)1 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)1