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!");
}
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());
}
Aggregations