Search in sources :

Example 1 with DeviceTestManager

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

the class JobClientTests method scheduleDeviceMethod.

private static void scheduleDeviceMethod(JobClient jobClient) throws IOException, IotHubException, InterruptedException {
    DeviceTestManager deviceTestManger = devices.get(0);
    final String deviceId = testDevice.getDeviceId();
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    // Act
    String jobId = JOB_ID_NAME + UUID.randomUUID();
    jobClient.scheduleDeviceMethod(jobId, queryCondition, DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING, new Date(), MAX_EXECUTION_TIME_IN_SECONDS);
    JobResult jobResult = jobClient.getJob(jobId);
    while (jobResult.getJobStatus() != JobStatus.completed) {
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
        jobResult = jobClient.getJob(jobId);
    }
    log.info("job finished with status {}", jobResult.getJobStatus());
    if (jobResult.getJobStatus().equals(JobStatus.completed)) {
        jobResult = queryDeviceJobResult(jobId, JobType.scheduleDeviceMethod, JobStatus.completed);
    } else {
        fail("Failed to schedule a method invocation, job status " + jobResult.getJobStatus() + ":" + jobResult.getStatusMessage());
    }
    MethodResult methodResult = jobResult.getOutcomeResult();
    assertNotNull("Device method didn't return any outcome", methodResult);
    assertEquals(200L, (long) methodResult.getStatus());
    assertEquals(DeviceEmulator.METHOD_LOOPBACK + ":" + PAYLOAD_STRING, methodResult.getPayload());
    // asserts for the client side.
    assertEquals(0, deviceTestManger.getStatusError());
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Date(java.util.Date) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)

Example 2 with DeviceTestManager

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

the class JobClientTests method tearDown.

@AfterClass
public static void tearDown() throws Exception {
    for (DeviceTestManager device : devices) {
        device.tearDown();
    }
    if (registryManager != null) {
        registryManager.close();
        registryManager = null;
    }
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) AfterClass(org.junit.AfterClass)

Example 3 with DeviceTestManager

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

the class JobClientTests method scheduleDeviceMethodSucceed.

@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
@Ignore
public void scheduleDeviceMethodSucceed() throws InterruptedException {
    // Arrange
    ExecutorService executor = Executors.newFixedThreadPool(MAX_NUMBER_JOBS);
    DeviceTestManager deviceTestManger = devices.get(0);
    final String deviceId = testDevice.getDeviceId();
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    final ConcurrentMap<String, Exception> jobExceptions = new ConcurrentHashMap<>();
    final ConcurrentMap<String, JobResult> jobResults = new ConcurrentHashMap<>();
    final ArrayList<String> jobIdsPending = new ArrayList<>();
    // Act
    for (int i = 0; i < MAX_NUMBER_JOBS; i++) {
        executor.submit(() -> {
            String jobId = JOB_ID_NAME + UUID.randomUUID();
            jobIdsPending.add(jobId);
            try {
                jobClient.scheduleDeviceMethod(jobId, queryCondition, DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING, new Date(), MAX_EXECUTION_TIME_IN_SECONDS);
                JobResult jobResult = jobClient.getJob(jobId);
                while (jobResult.getJobStatus() != JobStatus.completed) {
                    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
                    jobResult = jobClient.getJob(jobId);
                }
                log.info("job finished with status {}", jobResult.getJobStatus());
                if (jobResult.getJobStatus().equals(JobStatus.completed)) {
                    jobResult = queryDeviceJobResult(jobId, JobType.scheduleDeviceMethod, JobStatus.completed);
                    jobResults.put(jobId, jobResult);
                } else {
                    jobExceptions.put(jobId, new Exception("Scheduled job did not finish with status 'completed' but with " + jobResult.getJobStatus()));
                }
            } catch (IotHubException | IOException | InterruptedException e) {
                jobExceptions.put(jobId, e);
                log.warn("Adding {} to job exceptions...", jobId, e);
            }
            jobIdsPending.remove(jobId);
        });
    }
    cleanupJobs(executor, jobIdsPending);
    // asserts for the service side.
    if (jobExceptions.size() != 0) {
        for (Map.Entry<String, Exception> jobException : jobExceptions.entrySet()) {
            log.error("{} threw", jobException.getKey(), jobException.getValue());
        }
        fail("Service throw an exception enqueuing jobs");
    }
    assertEquals("Missing job result", MAX_NUMBER_JOBS, jobResults.size());
    for (Map.Entry<String, JobResult> jobResult : jobResults.entrySet()) {
        assertNotNull(jobResult.getValue());
        MethodResult methodResult = jobResult.getValue().getOutcomeResult();
        assertNotNull("Device method didn't return any outcome", methodResult);
        assertEquals(200L, (long) methodResult.getStatus());
        assertEquals(DeviceEmulator.METHOD_LOOPBACK + ":" + PAYLOAD_STRING, methodResult.getPayload());
    }
    // asserts for the client side.
    assertEquals(0, deviceTestManger.getStatusError());
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) Date(java.util.Date) DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Ignore(org.junit.Ignore) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 4 with DeviceTestManager

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

the class JobClientTests method mixScheduleInFutureSucceed.

@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
@ContinuousIntegrationTest
@Ignore
public void mixScheduleInFutureSucceed() throws InterruptedException {
    // Arrange
    ExecutorService executor = Executors.newFixedThreadPool(MAX_NUMBER_JOBS);
    DeviceTestManager deviceTestManger = devices.get(0);
    final String deviceId = testDevice.getDeviceId();
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    // 10 seconds in the future.
    final Date future = new Date(new Date().getTime() + 10000L);
    final ConcurrentMap<String, Exception> jobExceptions = new ConcurrentHashMap<>();
    final ConcurrentMap<String, JobResult> jobResults = new ConcurrentHashMap<>();
    final ConcurrentMap<String, Integer> twinExpectedTemperature = new ConcurrentHashMap<>();
    final ArrayList<String> jobIdsPending = new ArrayList<>();
    // Act
    for (int i = 0; i < MAX_NUMBER_JOBS; i++) {
        final int index = i;
        final int jobTemperature = (newTemperature++);
        executor.submit(() -> {
            String jobId = JOB_ID_NAME + UUID.randomUUID();
            jobIdsPending.add(jobId);
            try {
                if (index % 2 == 0) {
                    jobClient.scheduleDeviceMethod(jobId, queryCondition, DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING, future, MAX_EXECUTION_TIME_IN_SECONDS);
                } else {
                    DeviceTwinDevice deviceTwinDevice = new DeviceTwinDevice(deviceId);
                    Set<Pair> testDesProp = new HashSet<>();
                    testDesProp.add(new Pair(STANDARD_PROPERTY_HOMETEMP, jobTemperature));
                    deviceTwinDevice.setDesiredProperties(testDesProp);
                    twinExpectedTemperature.put(jobId, jobTemperature);
                    jobClient.scheduleUpdateTwin(jobId, queryCondition, deviceTwinDevice, new Date(), MAX_EXECUTION_TIME_IN_SECONDS);
                }
                JobResult jobResult = jobClient.getJob(jobId);
                while (jobResult.getJobStatus() != JobStatus.completed) {
                    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
                    jobResult = jobClient.getJob(jobId);
                }
                jobResult = queryDeviceJobResult(jobId, ((index % 2 == 0) ? JobType.scheduleDeviceMethod : JobType.scheduleUpdateTwin), JobStatus.completed);
                jobResults.put(jobId, jobResult);
            } catch (IotHubException | IOException | InterruptedException e) {
                jobExceptions.put(jobId, e);
            }
            jobIdsPending.remove(jobId);
        });
    }
    cleanupJobs(executor, jobIdsPending);
    // wait until identity receive the twin change
    ConcurrentMap<String, ConcurrentLinkedQueue<Object>> changes = deviceTestManger.getTwinChanges();
    int timeout = 0;
    while (changes.size() == 0) {
        if ((timeout += MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS) >= TEST_TIMEOUT_MILLISECONDS) {
            fail("Device didn't receive the twin change");
        }
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
        changes = deviceTestManger.getTwinChanges();
    }
    // asserts for the service side.
    if (jobExceptions.size() != 0) {
        for (Map.Entry<String, Exception> jobException : jobExceptions.entrySet()) {
            log.error("{} threw", jobException.getKey(), jobException.getValue());
        }
        fail("Service throw an exception enqueuing jobs");
    }
    assertEquals("Missing job result", MAX_NUMBER_JOBS, jobResults.size());
    ConcurrentLinkedQueue<Object> temperatures = changes.get(STANDARD_PROPERTY_HOMETEMP);
    assertNotNull("There is no " + STANDARD_PROPERTY_HOMETEMP + " in the device changes", temperatures);
    for (Map.Entry<String, JobResult> job : jobResults.entrySet()) {
        JobResult jobResult = job.getValue();
        String jobId = jobResult.getJobId();
        assertNotNull(jobResult);
        if (jobResult.getJobType() == JobType.scheduleDeviceMethod) {
            MethodResult methodResult = jobResult.getOutcomeResult();
            assertNotNull("Device method didn't return any outcome", methodResult);
            assertEquals(200L, (long) methodResult.getStatus());
            assertEquals(DeviceEmulator.METHOD_LOOPBACK + ":" + PAYLOAD_STRING, methodResult.getPayload());
        } else {
            String temperature = twinExpectedTemperature.get(jobId) + ".0";
            assertTrue("Device do not change " + STANDARD_PROPERTY_HOMETEMP + " to " + temperature, temperatures.contains(temperature));
        }
    }
    // asserts for the client side.
    assertEquals(0, deviceTestManger.getStatusError());
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) HashSet(java.util.HashSet) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IOException(java.io.IOException) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Ignore(org.junit.Ignore) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Example 5 with DeviceTestManager

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

the class JobClientTests method cleanToStart.

@Before
public void cleanToStart() throws IOException, IotHubException {
    for (DeviceTestManager device : devices) {
        device.clearStatistics();
    }
    log.info("Waiting for all previously scheduled jobs to finish...");
    long startTime = System.currentTimeMillis();
    Query activeJobsQuery = jobClient.queryDeviceJob("SELECT * FROM devices.jobs");
    while (activeJobsQuery.hasNext()) {
        JobsResponseParser job = JobsResponseParser.createFromJson(activeJobsQuery.next().toString());
        JobStatus jobStatus = jobClient.getJob(job.getJobId()).getJobStatus();
        while (jobStatus.equals(JobStatus.enqueued) || jobStatus.equals(JobStatus.queued) || jobStatus.equals(JobStatus.running) || jobStatus.equals(JobStatus.scheduled)) {
            try {
                Thread.sleep(500);
                jobStatus = jobClient.getJob(job.getJobId()).getJobStatus();
            } catch (InterruptedException e) {
                fail("Unexpected interrupted exception occurred");
            }
            if (System.currentTimeMillis() - startTime > MAX_TIME_WAIT_FOR_PREVIOUSLY_SCHEDULED_JOBS_TO_FINISH_IN_MILLIS) {
                fail("Waited too long for previously scheduled jobs to finish");
            }
        }
    }
    log.info("Done waiting for jobs to finish!");
}
Also used : JobStatus(com.microsoft.azure.sdk.iot.service.jobs.JobStatus) DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) JobsResponseParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsResponseParser) Before(org.junit.Before)

Aggregations

DeviceTestManager (tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager)11 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)6 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)6 Test (org.junit.Test)6 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)6 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)6 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)5 Date (java.util.Date)5 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)4 IotHubUnathorizedException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 Ignore (org.junit.Ignore)4 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)4 TestModuleIdentity (tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity)3