Search in sources :

Example 6 with RetryPolicy

use of com.microsoft.azure.sdk.iot.device.transport.RetryPolicy 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 RetryPolicy

use of com.microsoft.azure.sdk.iot.device.transport.RetryPolicy 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 RetryPolicy

use of com.microsoft.azure.sdk.iot.device.transport.RetryPolicy 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 RetryPolicy

use of com.microsoft.azure.sdk.iot.device.transport.RetryPolicy 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)

Aggregations

RetryPolicy (com.microsoft.azure.sdk.iot.device.transport.RetryPolicy)9 Test (org.junit.Test)9 ExponentialBackoffWithJitter (com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter)8 RetryDecision (com.microsoft.azure.sdk.iot.device.transport.RetryDecision)6 NonStrictExpectations (mockit.NonStrictExpectations)4