use of com.arm.mbed.cloud.sdk.common.MbedCloudException in project mbed-cloud-sdk-java by ARMmbed.
the class TestAbstractObserver method testOne.
@Test
public void testOne() {
SubscriptionTestManagerFixedInput manager = new SubscriptionTestManagerFixedInput(true);
TestObserver obs = manager.createObserver(SubscriptionType.DEVICE_STATE_CHANGE, null, BackpressureStrategy.BUFFER);
try {
NotificationTestMessageValue value = obs.one(new TimePeriod(5));
// Value should be the first element received.
assertEquals(0, value.i);
// The observer should have been disposed
assertTrue(obs.isDisposed());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
// Checking that timeout works properly.
SubscriptionTestManagerNeverInput manager2 = new SubscriptionTestManagerNeverInput(true);
obs = manager2.createObserver(SubscriptionType.DEVICE_STATE_CHANGE, null, BackpressureStrategy.BUFFER);
long start = 0;
try {
start = System.currentTimeMillis();
NotificationTestMessageValue value = obs.one(new TimePeriod(2));
assertNull(value);
// Value should never be received.
} catch (MbedCloudException e) {
double time = (System.currentTimeMillis() - start) / 1000d;
assertNotNull(e);
assertEquals("The value could not be retrieved", e.getMessage());
// Check that at least 2 seconds elapsed before the exception was thrown.
assertTrue(time > 2);
// Check that the timeout was of the right order of magnitude.
assertTrue(time < 5);
}
}
Aggregations