Search in sources :

Example 1 with ExponentialBackoffWithJitter

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

the class ExponentialBackoffWithJitterTest method constructorHaveDefaultValues.

// Tests_SRS_EXPONENTIALBACKOFF_28_006: [Constructor should have default values retryCount, minBackoff, maxBackoff, deltaBackoff and firstFastRetry]
@Test
public void constructorHaveDefaultValues() {
    // act
    final RetryPolicy exp = new ExponentialBackoffWithJitter();
    // assert
    assertEquals(Integer.MAX_VALUE, Deencapsulation.getField(exp, "retryCount"));
    assertEquals(100L, Deencapsulation.getField(exp, "minBackoff"));
    assertEquals(10 * 1000L, Deencapsulation.getField(exp, "maxBackoff"));
    assertEquals(100L, Deencapsulation.getField(exp, "deltaBackoff"));
    assertTrue((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 2 with ExponentialBackoffWithJitter

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

the class ExponentialBackoffWithJitterTest method shouldRetryResultWithMinMaxBackOffReturnsMinBackOff.

// 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 shouldRetryResultWithMinMaxBackOffReturnsMinBackOff() {
    // arrange
    final RetryPolicy exp = new ExponentialBackoffWithJitter(10, 10, 100, 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(10, 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 3 with ExponentialBackoffWithJitter

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

the class ExponentialBackoffWithJitterTest method shouldRetryResultWithLargeCurrentRetryCountReturnsMaxBackOff.

// 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 shouldRetryResultWithLargeCurrentRetryCountReturnsMaxBackOff() {
    // arrange
    final RetryPolicy exp = new ExponentialBackoffWithJitter(10000, 10, 100, 10, true);
    Deencapsulation.setField(exp, "random", mockedRandom);
    new NonStrictExpectations() {

        {
            mockedRandom.nextInt(anyInt);
            result = 0;
        }
    };
    // act
    RetryDecision actual = exp.getRetryDecision(999, null);
    // assert
    assertTrue(actual.shouldRetry());
    assertEquals(100, 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 4 with ExponentialBackoffWithJitter

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

the class SendMessagesErrInjTests method sendMessagesWithTcpConnectionDropNotifiesUserIfRetryExpires.

@Test
@ContinuousIntegrationTest
public void sendMessagesWithTcpConnectionDropNotifiesUserIfRetryExpires() throws Exception {
    if (testInstance.protocol == HTTPS || (testInstance.protocol == MQTT_WS && testInstance.authenticationType != SAS) || testInstance.useHttpProxy) {
        // MQTT_WS + x509 is not supported for sending messages
        return;
    }
    this.testInstance.setup();
    testInstance.identity.getClient().setRetryPolicy(new NoRetry());
    Message tcpConnectionDropErrorInjectionMessageUnrecoverable = ErrorInjectionHelper.tcpConnectionDropErrorInjectionMessage(1, 100000);
    IotHubServicesCommon.sendMessagesExpectingUnrecoverableConnectionLossAndTimeout(testInstance.identity.getClient(), testInstance.protocol, tcpConnectionDropErrorInjectionMessageUnrecoverable, testInstance.authenticationType);
    // reset back to default
    testInstance.identity.getClient().setRetryPolicy(new ExponentialBackoffWithJitter());
}
Also used : NoRetry(com.microsoft.azure.sdk.iot.device.transport.NoRetry) ExponentialBackoffWithJitter(com.microsoft.azure.sdk.iot.device.transport.ExponentialBackoffWithJitter) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) ErrInjTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ErrInjTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Example 5 with ExponentialBackoffWithJitter

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

the class ExponentialBackoffWithJitterTest method shouldRetryResultWithNoFirstFastRetry.

// Tests_SRS_EXPONENTIALBACKOFF_28_004: [The function shall return non-zero wait time on first retry if firstFastRetry is false]
@Test
public void shouldRetryResultWithNoFirstFastRetry() {
    // arrange
    final RetryPolicy exp = new ExponentialBackoffWithJitter(10, 100, 10 * 1000, 100, false);
    // act
    RetryDecision actual = exp.getRetryDecision(0, null);
    // assert
    assertTrue(actual.shouldRetry());
    assertTrue(actual.getDuration() > 0);
}
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

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