Search in sources :

Example 6 with ExponentialBackoffWithJitter

use of com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter in project azure-iot-sdk-java by Azure.

the class ExponentialBackoffWithJitterTest method shouldRetryResultRetry1stTime.

// Tests_SRS_EXPONENTIALBACKOFF_28_005: [The function shall return waitTime according to
// F(x) = min(Cmin+ (2^(x-1)-1) * rand(C * (1 – Jd), C*(1-Ju)), Cmax) where  x is the xth retry.]
@Test
public void shouldRetryResultRetry1stTime() {
    // arrange
    final RetryPolicy exp = new ExponentialBackoffWithJitter(10000, 10, 100, 10, true);
    final double deltaBackoffLowBound = 10 * 0.8;
    final int count = 2;
    Deencapsulation.setField(exp, "random", mockedRandom);
    new NonStrictExpectations() {

        {
            mockedRandom.nextInt(anyInt);
            result = 0;
        }
    };
    // act
    RetryDecision actual = exp.getRetryDecision(count, null);
    // assert
    int expected = (int) ((Math.pow(2.0, (double) count) - 1.0) * deltaBackoffLowBound) + 10;
    assertTrue(actual.shouldRetry());
    assertEquals(expected, actual.getDuration());
}
Also used : RetryDecision(com.microsoft.azure.sdk.iot.device.transport.RetryDecision) RetryPolicy(com.microsoft.azure.sdk.iot.device.transport.RetryPolicy) NonStrictExpectations(mockit.NonStrictExpectations) ExponentialBackoffWithJitter(com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter) Test(org.junit.Test)

Example 7 with ExponentialBackoffWithJitter

use of com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter in project azure-iot-sdk-java by Azure.

the class ExponentialBackoffWithJitterTest method shouldRetryResultWithOnlyCurrentRetryCount.

// Tests_SRS_EXPONENTIALBACKOFF_28_005: [The function shall return waitTime according to
// F(x) = min(Cmin+ (2^(x-1)-1) * rand(C * (1 – Jd), C*(1-Ju)), Cmax) where  x is the xth retry.]
@Test
public void shouldRetryResultWithOnlyCurrentRetryCount() {
    // arrange
    final RetryPolicy exp = new ExponentialBackoffWithJitter(10, 0, 0, 0, true);
    Deencapsulation.setField(exp, "random", mockedRandom);
    new NonStrictExpectations() {

        {
            mockedRandom.nextInt(anyInt);
            result = 0;
        }
    };
    // act
    RetryDecision actual = exp.getRetryDecision(1, null);
    // assert
    assertTrue(actual.shouldRetry());
    assertEquals(0, actual.getDuration());
}
Also used : RetryDecision(com.microsoft.azure.sdk.iot.device.transport.RetryDecision) RetryPolicy(com.microsoft.azure.sdk.iot.device.transport.RetryPolicy) NonStrictExpectations(mockit.NonStrictExpectations) ExponentialBackoffWithJitter(com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter) Test(org.junit.Test)

Example 8 with ExponentialBackoffWithJitter

use of com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter in project azure-iot-sdk-java by Azure.

the class ExponentialBackoffWithJitterTest method constructorSavesParameterToLocal.

// Tests_SRS_EXPONENTIALBACKOFF_28_002: [Constructor should save retryCount, minBackoff, maxBackoff, deltaBackoff and firstFastRetry]
@Test
public void constructorSavesParameterToLocal() {
    // act
    final RetryPolicy exp = new ExponentialBackoffWithJitter(10, 2 * 1000, 2 * 1000, 2 * 1000, false);
    // assert
    assertEquals(10, Deencapsulation.getField(exp, "retryCount"));
    assertEquals(2 * 1000L, Deencapsulation.getField(exp, "minBackoff"));
    assertEquals(2 * 1000L, Deencapsulation.getField(exp, "maxBackoff"));
    assertEquals(2 * 1000L, Deencapsulation.getField(exp, "deltaBackoff"));
    assertFalse((boolean) Deencapsulation.getField(exp, "firstFastRetry"));
}
Also used : RetryPolicy(com.microsoft.azure.sdk.iot.device.transport.RetryPolicy) ExponentialBackoffWithJitter(com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter) Test(org.junit.Test)

Example 9 with ExponentialBackoffWithJitter

use of com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter in project azure-iot-sdk-java by Azure.

the class ExponentialBackoffWithJitterTest method shouldRetryResultWithFirstFastRetry.

// Tests_SRS_EXPONENTIALBACKOFF_28_003: [The function shall indicate immediate retry on first retry if firstFastRetry is true]
@Test
public void shouldRetryResultWithFirstFastRetry() {
    // arrange
    final RetryPolicy exp = new ExponentialBackoffWithJitter(10, 100, 10 * 1000, 100, true);
    RetryDecision expected = new RetryDecision(true, 0);
    // act
    RetryDecision actual = exp.getRetryDecision(0, null);
    // assert
    assertTrue(actual.shouldRetry());
    assertEquals(0, actual.getDuration());
}
Also used : RetryDecision(com.microsoft.azure.sdk.iot.device.transport.RetryDecision) RetryPolicy(com.microsoft.azure.sdk.iot.device.transport.RetryPolicy) ExponentialBackoffWithJitter(com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter) Test(org.junit.Test)

Example 10 with ExponentialBackoffWithJitter

use of com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter in project azure-iot-sdk-java by Azure.

the class InternalClientTest method setRetryPolicySetPolicy.

// Tests_SRS_INTERNALCLIENT_28_001: [The function shall set the device config's RetryPolicy .]
@Test
public void setRetryPolicySetPolicy() throws URISyntaxException {
    // arrange
    final IotHubClientProtocol protocol = IotHubClientProtocol.AMQPS;
    InternalClient client = Deencapsulation.newInstance(InternalClient.class, new Class[] { IotHubConnectionString.class, IotHubClientProtocol.class, long.class, long.class, ClientOptions.class }, mockIotHubConnectionString, protocol, SEND_PERIOD, RECEIVE_PERIOD, null);
    Deencapsulation.setField(client, "config", mockConfig);
    // act
    Deencapsulation.invoke(client, "setRetryPolicy", new ExponentialBackoffWithJitter());
    // assert
    new Verifications() {

        {
            mockConfig.setRetryPolicy((RetryPolicy) any);
            times = 1;
        }
    };
}
Also used : ExponentialBackoffWithJitter(com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter) Test(org.junit.Test)

Aggregations

ExponentialBackoffWithJitter (com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter)10 Test (org.junit.Test)10 RetryPolicy (com.microsoft.azure.sdk.iot.device.transport.RetryPolicy)8 RetryDecision (com.microsoft.azure.sdk.iot.device.transport.RetryDecision)6 NonStrictExpectations (mockit.NonStrictExpectations)4 NoRetry (com.microsoft.azure.sdk.iot.device.transport.NoRetry)1 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)1 ErrInjTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ErrInjTest)1 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)1