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