Search in sources :

Example 1 with IotHubSendTask

use of com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask in project azure-iot-sdk-java by Azure.

the class DeviceIO method open.

/**
     * Starts asynchronously sending and receiving messages from an IoT Hub. If
     * the client is already open, the function shall do nothing.
     *
     * @throws IOException if a connection to an IoT Hub is cannot be established.
     */
public void open() throws IOException {
    /* Codes_SRS_DEVICE_IO_21_007: [If the client is already open, the open shall do nothing.] */
    if (this.state == IotHubClientState.OPEN) {
        return;
    }
    if (this.config.getPathToCertificate() == null && this.config.getUserCertificateString() == null) {
        try {
            /* Codes_SRS_DEVICE_IO_21_008: [The open shall create default IotHubSSL context if no certificate input was provided by user and save it by calling setIotHubSSLContext.] */
            IotHubSSLContext iotHubSSLContext = new IotHubSSLContext();
            this.config.setIotHubSSLContext(iotHubSSLContext);
        } catch (Exception e) {
            /* Codes_SRS_DEVICE_IO_21_011: [If an exception is thrown when creating a SSL context then Open shall throw IOException to the user indicating the failure] */
            throw new IOException(e.getCause());
        }
    } else if (this.config.getPathToCertificate() != null) {
        try {
            /* Codes_SRS_DEVICE_IO_21_009: [The open shall create IotHubSSL context with the certificate path if input was provided by user and save it by calling setIotHubSSLContext.] */
            IotHubSSLContext iotHubSSLContext = new IotHubSSLContext(this.config.getPathToCertificate(), true);
            this.config.setIotHubSSLContext(iotHubSSLContext);
        } catch (Exception e) {
            /* Codes_SRS_DEVICE_IO_21_011: [If an exception is thrown when creating a SSL context then open shall throw IOException to the user indicating the failure] */
            throw new IOException(e.getCause());
        }
    } else {
        try {
            /* Codes_SRS_DEVICE_IO_21_010: [The open shall create IotHubSSL context with the certificate String if input was provided by user and save it by calling setIotHubSSLContext.] */
            IotHubSSLContext iotHubSSLContext = new IotHubSSLContext(this.config.getUserCertificateString(), false);
            this.config.setIotHubSSLContext(iotHubSSLContext);
        } catch (Exception e) {
            /* Codes_SRS_DEVICE_IO_21_011: [If an exception is thrown when creating a SSL context then open shall throw IOException to the user indicating the failure] */
            throw new IOException(e.getCause());
        }
    }
    /* Codes_SRS_DEVICE_IO_21_012: [The open shall open the transport to communicate with an IoT Hub.] */
    /* Codes_SRS_DEVICE_IO_21_015: [If an error occurs in opening the transport, the open shall throw an IOException.] */
    this.transport.open();
    this.sendTask = new IotHubSendTask(this.transport);
    this.receiveTask = new IotHubReceiveTask(this.transport);
    this.taskScheduler = Executors.newScheduledThreadPool(2);
    // the scheduler waits until each execution is finished before
    // scheduling the next one, so executions of a given task
    // will never overlap.
    /* Codes_SRS_DEVICE_IO_21_013: [The open shall schedule send tasks to run every SEND_PERIOD_MILLIS milliseconds.] */
    this.taskScheduler.scheduleAtFixedRate(this.sendTask, 0, sendPeriodInMilliseconds, TimeUnit.MILLISECONDS);
    /* Codes_SRS_DEVICE_IO_21_014: [The open shall schedule receive tasks to run every receivePeriodInMilliseconds milliseconds.] */
    this.taskScheduler.scheduleAtFixedRate(this.receiveTask, 0, receivePeriodInMilliseconds, TimeUnit.MILLISECONDS);
    /* Codes_SRS_DEVICE_IO_21_016: [The open shall set the `state` as `OPEN`.] */
    this.state = IotHubClientState.OPEN;
}
Also used : IotHubSendTask(com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask) IotHubReceiveTask(com.microsoft.azure.sdk.iot.device.transport.IotHubReceiveTask) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with IotHubSendTask

use of com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask in project azure-iot-sdk-java by Azure.

the class IotHubSendTaskTest method runDoesNotCrashFromIoException.

// Tests_SRS_IOTHUBSENDTASK_11_005: [The function shall not crash because of an IOException thrown by the transport.]
@Test
public void runDoesNotCrashFromIoException() throws IOException, URISyntaxException {
    new NonStrictExpectations() {

        {
            mockTransport.sendMessages();
            result = new IOException();
        }
    };
    IotHubSendTask sendTask = new IotHubSendTask(mockTransport);
    sendTask.run();
}
Also used : IotHubSendTask(com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 3 with IotHubSendTask

use of com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask in project azure-iot-sdk-java by Azure.

the class IotHubSendTaskTest method runSendsAllMessages.

// Tests_SRS_IOTHUBSENDTASK_11_001: [The constructor shall save the transport.]
// Tests_SRS_IOTHUBSENDTASK_11_002: [The function shall send all messages on the transport queue.]
@Test
public void runSendsAllMessages() throws IOException, URISyntaxException {
    IotHubSendTask sendTask = new IotHubSendTask(mockTransport);
    sendTask.run();
    new Verifications() {

        {
            mockTransport.sendMessages();
        }
    };
}
Also used : IotHubSendTask(com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 4 with IotHubSendTask

use of com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask in project azure-iot-sdk-java by Azure.

the class IotHubSendTaskTest method runInvokesAllCallbacks.

@Test
public void runInvokesAllCallbacks() {
    final Object sendThreadLock = new Object();
    new Expectations() {

        {
            mockTransport.getSendThreadLock();
            result = sendThreadLock;
            mockTransport.hasMessagesToSend();
            result = false;
            mockTransport.hasCallbacksToExecute();
            result = true;
        }
    };
    IotHubSendTask sendTask = new IotHubSendTask(mockTransport);
    sendTask.run();
    new Verifications() {

        {
            mockTransport.sendMessages();
        }
    };
}
Also used : NonStrictExpectations(mockit.NonStrictExpectations) Expectations(mockit.Expectations) IotHubSendTask(com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 5 with IotHubSendTask

use of com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask in project azure-iot-sdk-java by Azure.

the class IotHubSendTaskTest method runSendsAllMessages.

// Tests_SRS_IOTHUBSENDTASK_11_001: [The constructor shall save the transport.]
// Tests_SRS_IOTHUBSENDTASK_11_002: [The function shall send all messages on the transport queue.]
@Test
public void runSendsAllMessages() {
    final Object sendThreadLock = new Object();
    new Expectations() {

        {
            mockTransport.getSendThreadLock();
            result = sendThreadLock;
            mockTransport.hasMessagesToSend();
            result = true;
        }
    };
    IotHubSendTask sendTask = new IotHubSendTask(mockTransport);
    sendTask.run();
    new Verifications() {

        {
            mockTransport.sendMessages();
        }
    };
}
Also used : NonStrictExpectations(mockit.NonStrictExpectations) Expectations(mockit.Expectations) IotHubSendTask(com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask) Verifications(mockit.Verifications) Test(org.junit.Test)

Aggregations

IotHubSendTask (com.microsoft.azure.sdk.iot.device.transport.IotHubSendTask)10 Test (org.junit.Test)8 NonStrictExpectations (mockit.NonStrictExpectations)6 Verifications (mockit.Verifications)4 IOException (java.io.IOException)3 IotHubReceiveTask (com.microsoft.azure.sdk.iot.device.transport.IotHubReceiveTask)2 Expectations (mockit.Expectations)2