Search in sources :

Example 11 with MbedCloudException

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);
    }
}
Also used : MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) TimePeriod(com.arm.mbed.cloud.sdk.common.TimePeriod) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) Test(org.junit.Test)

Aggregations

MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)11 Test (org.junit.Test)5 LinkedList (java.util.LinkedList)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 TimePeriod (com.arm.mbed.cloud.sdk.common.TimePeriod)3 Resource (com.arm.mbed.cloud.sdk.connect.model.Resource)3 Connect (com.arm.mbed.cloud.sdk.Connect)2 API (com.arm.mbed.cloud.sdk.annotations.API)2 Callback (com.arm.mbed.cloud.sdk.common.Callback)2 CloudCall (com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall)2 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)2 Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)2 DeviceListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions)2 Predicate (io.reactivex.functions.Predicate)2 Call (retrofit2.Call)2 AbstractExample (utils.AbstractExample)2 Example (utils.Example)2 Daemon (com.arm.mbed.cloud.sdk.annotations.Daemon)1 Webhook (com.arm.mbed.cloud.sdk.connect.model.Webhook)1 NotificationMessage (com.arm.mbed.cloud.sdk.internal.mds.model.NotificationMessage)1