Search in sources :

Example 6 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 testSendingMessageFromDeviceClient.

private static Success testSendingMessageFromDeviceClient(DeviceClient multiplexedClient, Message message) {
    Success messageSendSuccess = new Success();
    EventCallback messageSentCallback = new EventCallback(IotHubStatusCode.OK_EMPTY);
    multiplexedClient.sendEventAsync(message, messageSentCallback, messageSendSuccess);
    return messageSendSuccess;
}
Also used : EventCallback(tests.integration.com.microsoft.azure.sdk.iot.helpers.EventCallback) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success)

Example 7 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 startTwin.

private static void startTwin(DeviceClient deviceClient, IotHubEventCallback twinEventCallback, TwinPropertyCallBack twinPropertyCallBack) throws IOException, InterruptedException {
    Success twinStarted = new Success();
    deviceClient.startDeviceTwin(twinEventCallback, twinStarted, twinPropertyCallBack, null);
    long startTime = System.currentTimeMillis();
    while (!twinStarted.wasCallbackFired()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > TWIN_SUBSCRIBE_TIMEOUT_MILLIS) {
            fail("Timed out waiting for twin to start");
        }
    }
    assertTrue("Failed to start twin. Unexpected status code " + twinStarted.getCallbackStatusCode(), twinStarted.getResult());
}
Also used : Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success)

Example 8 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 multiplexedSessionsRecoverSubscriptionsFromDeviceSessionDrops.

// If a multiplexed device is subscribed to twin and/or methods and/or cloud to device messages, then loses its
// session due to network issues, it should still be subscribed to twin and/or methods and/or cloud to device messages
// after it finishes reconnection
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
@ErrInjTest
@IotHubTest
@Test
public void multiplexedSessionsRecoverSubscriptionsFromDeviceSessionDrops() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT, MultiplexingClientOptions.builder().build(), true);
    ConnectionStatusChangeTracker multiplexedConnectionStatusChangeTracker = new ConnectionStatusChangeTracker();
    testInstance.multiplexingClient.registerConnectionStatusChangeCallback(multiplexedConnectionStatusChangeTracker, null);
    ConnectionStatusChangeTracker[] connectionStatusChangeTrackers = new ConnectionStatusChangeTracker[DEVICE_MULTIPLEX_COUNT];
    DeviceTwin deviceTwinServiceClient = new DeviceTwin(iotHubConnectionString, DeviceTwinClientOptions.builder().httpReadTimeout(0).build());
    DeviceMethod deviceMethodServiceClient = new DeviceMethod(iotHubConnectionString, DeviceMethodClientOptions.builder().httpReadTimeout(0).build());
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        connectionStatusChangeTrackers[i] = new ConnectionStatusChangeTracker();
        testInstance.deviceClientArray.get(i).registerConnectionStatusChangeCallback(connectionStatusChangeTrackers[i], null);
    }
    testInstance.multiplexingClient.open();
    // Subscribe to methods for all multiplexed clients
    DeviceMethodCallback[] deviceMethodCallbacks = new DeviceMethodCallback[DEVICE_MULTIPLEX_COUNT];
    String[] expectedMethodNames = new String[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        expectedMethodNames[i] = UUID.randomUUID().toString();
        deviceMethodCallbacks[i] = new DeviceMethodCallback(expectedMethodNames[i]);
        subscribeToDeviceMethod(testInstance.deviceClientArray.get(i), deviceMethodCallbacks[i]);
    }
    // Start twin for all multiplexed clients
    String[] expectedPropertyKeys = new String[DEVICE_MULTIPLEX_COUNT];
    String[] expectedPropertyValues = new String[DEVICE_MULTIPLEX_COUNT];
    TwinPropertyCallBackImpl[] twinPropertyCallBacks = new TwinPropertyCallBackImpl[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // The twin for this test identity is about to be modified. Set this flag so that the test identity recycler re-uses this identity only for tests
        // that don't care about the initial twin state of an identity
        testInstance.testDevicesArrayIdentity.get(i).twinUpdated = true;
        expectedPropertyKeys[i] = UUID.randomUUID().toString();
        expectedPropertyValues[i] = UUID.randomUUID().toString();
        twinPropertyCallBacks[i] = new TwinPropertyCallBackImpl(expectedPropertyKeys[i], expectedPropertyValues[i]);
        startTwin(testInstance.deviceClientArray.get(i), new EventCallback(IotHubStatusCode.OK), twinPropertyCallBacks[i]);
    }
    // Subscribe to cloud to device messages for all multiplexed clients
    String[] expectedMessageCorrelationIds = new String[DEVICE_MULTIPLEX_COUNT];
    MessageCallback[] messageCallbacks = new MessageCallback[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        expectedMessageCorrelationIds[i] = UUID.randomUUID().toString();
        messageCallbacks[i] = new MessageCallback(expectedMessageCorrelationIds[i]);
        testInstance.deviceClientArray.get(i).setMessageCallback(messageCallbacks[i], null);
    }
    // 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);
        }
    }
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // test d2c telemetry
        testSendingMessagesFromMultiplexedClients(testInstance.deviceClientArray);
        // test receiving direct methods
        testDeviceMethod(deviceMethodServiceClient, testInstance.deviceIdentityArray.get(i).getDeviceId(), expectedMethodNames[i], deviceMethodCallbacks[i]);
        // Send desired property update to multiplexed device
        testDesiredPropertiesFlow(testInstance.deviceClientArray.get(i), deviceTwinServiceClient, twinPropertyCallBacks[i], expectedPropertyKeys[i], expectedPropertyValues[i]);
        // Testing sending reported properties
        testReportedPropertiesFlow(testInstance.deviceClientArray.get(i), deviceTwinServiceClient, expectedPropertyKeys[i], expectedPropertyValues[i]);
        testReceivingCloudToDeviceMessage(testInstance.deviceIdentityArray.get(i).getDeviceId(), messageCallbacks[i], expectedMessageCorrelationIds[i]);
    }
    assertFalse(multiplexedConnectionStatusChangeTracker.wentDisconnectedRetrying);
    testInstance.multiplexingClient.close();
    assertMultiplexedDevicesClosedGracefully(connectionStatusChangeTrackers);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) EventCallback(tests.integration.com.microsoft.azure.sdk.iot.helpers.EventCallback) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 9 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 subscribeToDeviceMethod.

private static void subscribeToDeviceMethod(DeviceClient deviceClient, DeviceMethodCallback deviceMethodCallback) throws InterruptedException, IOException {
    Success methodsSubscribedSuccess = new Success();
    deviceClient.subscribeToDeviceMethod(deviceMethodCallback, null, (responseStatus, callbackContext) -> {
        ((Success) callbackContext).setCallbackStatusCode(responseStatus);
        ((Success) callbackContext).setResult(responseStatus == IotHubStatusCode.OK_EMPTY);
        ((Success) callbackContext).callbackWasFired();
    }, methodsSubscribedSuccess);
    // Wait for methods subscription to be acknowledged by hub
    long startTime = System.currentTimeMillis();
    while (methodsSubscribedSuccess.wasCallbackFired()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > DEVICE_METHOD_SUBSCRIBE_TIMEOUT_MILLISECONDS) {
            throw new AssertionError("Timed out waiting for device method subscription to be acknowledged");
        }
    }
}
Also used : Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success)

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