Search in sources :

Example 1 with IotHubReceiveTask

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

the class IotHubReceiveTaskTest method runDoesNotCrashFromThrowable.

// Tests_SRS_IOTHUBRECEIVETASK_11_005: [The function shall not crash because of any error or exception thrown by the transport.]
@Test
public void runDoesNotCrashFromThrowable() throws IOException, URISyntaxException {
    new NonStrictExpectations() {

        {
            mockTransport.handleMessage();
            result = new Throwable("Test if the receive task does not crash.");
        }
    };
    IotHubReceiveTask receiveTask = new IotHubReceiveTask(mockTransport);
    receiveTask.run();
}
Also used : IotHubReceiveTask(com.microsoft.azure.sdk.iot.device.transport.IotHubReceiveTask) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 2 with IotHubReceiveTask

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

the class IotHubReceiveTaskTest method runDoesNotCrashFromIoException.

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

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

Example 3 with IotHubReceiveTask

use of com.microsoft.azure.sdk.iot.device.transport.IotHubReceiveTask 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 4 with IotHubReceiveTask

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

the class IotHubReceiveTaskTest method runReceivesAllMessages.

// Tests_SRS_IOTHUBRECEIVETASK_11_001: [The constructor shall save the transport.]
// Tests_SRS_IOTHUBRECEIVETASK_11_002: [The function shall poll an IoT Hub for messages, invoke the message callback if one exists, and return one of COMPLETE, ABANDON, or REJECT to the IoT Hub.]
@Test
public void runReceivesAllMessages() throws IOException, URISyntaxException {
    IotHubReceiveTask receiveTask = new IotHubReceiveTask(mockTransport);
    receiveTask.run();
    new Verifications() {

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

Aggregations

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