Search in sources :

Example 1 with JobStatus

use of com.microsoft.azure.sdk.iot.service.jobs.JobStatus 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)

Example 2 with JobStatus

use of com.microsoft.azure.sdk.iot.service.jobs.JobStatus in project azure-iot-sdk-java by Azure.

the class JobClientTests method cancelScheduleDeviceMethodSucceed.

@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
@ContinuousIntegrationTest
@Ignore
public void cancelScheduleDeviceMethodSucceed() 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 + "']";
    // 3 minutes in the future.
    final Date future = new Date(new Date().getTime() + 180000L);
    final ConcurrentMap<String, Exception> jobExceptions = new ConcurrentHashMap<>();
    final ArrayList<String> jobIdsPending = new ArrayList<>();
    // Act
    for (int i = 0; i < MAX_NUMBER_JOBS; i++) {
        final int index = 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, (index % 2 == 0) ? future : new Date(), MAX_EXECUTION_TIME_IN_SECONDS);
                JobStatus expectedJobStatus = JobStatus.completed;
                if (index % 2 == 0) {
                    expectedJobStatus = JobStatus.cancelled;
                    // wait 1 seconds and cancel.
                    Thread.sleep(1000);
                    jobClient.cancelJob(jobId);
                }
                JobResult jobResult = jobClient.getJob(jobId);
                while (jobResult.getJobStatus() != expectedJobStatus) {
                    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
                    jobResult = jobClient.getJob(jobId);
                }
                log.info("Iothub confirmed {} {} for type {}", jobId, expectedJobStatus, JobType.scheduleDeviceMethod);
            } catch (IotHubException | IOException | InterruptedException e) {
                jobExceptions.put(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");
    }
    // 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) 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) JobStatus(com.microsoft.azure.sdk.iot.service.jobs.JobStatus) 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) 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)

Aggregations

JobStatus (com.microsoft.azure.sdk.iot.service.jobs.JobStatus)2 DeviceTestManager (tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager)2 JobsResponseParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsResponseParser)1 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)1 SqlQuery (com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery)1 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)1 IotHubUnathorizedException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException)1 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1