public void testBackoffComputationWithJitter()

in tst/com/amazon/kinesis/streaming/agent/tailing/AsyncPublisherThrottlerTest.java [112:160]


    public void testBackoffComputationWithJitter() {
        final int initialBackoffMillis = 100;
        final int maxBackoffMillis = 1000;
        final double backoffFactor = 2.0;
        final double jitter = 0.3;
        AsyncPublisherThrottler<FirehoseRecord> bo = new AsyncPublisherThrottler<FirehoseRecord>(
                getTestPublisher(), initialBackoffMillis, maxBackoffMillis,
                backoffFactor, jitter, AsyncPublisherThrottler.DEFAULT_RECOVERY_FACTOR);
        assertEquals(bo.getNextBackoff(), 0);

        bo.onSendError();
        long min = (long) (initialBackoffMillis * (1 - jitter));
        long max = (long) (initialBackoffMillis * (1 + jitter));
        assertTrue(bo.getNextBackoff() >= min);
        assertTrue(bo.getNextBackoff() <= max);

        bo.onSendError();
        long expected = (long) (backoffFactor * initialBackoffMillis);
        min = (long) (expected * (1 - jitter));
        max = (long) (expected * (1 + jitter));
        assertTrue(bo.getNextBackoff() >= min);
        assertTrue(bo.getNextBackoff() <= max);

        bo.onSendError();
        expected = (long) (backoffFactor * backoffFactor * initialBackoffMillis);
        min = (long) (expected * (1 - jitter));
        max = (long) (expected * (1 + jitter));
        assertTrue(bo.getNextBackoff() >= min);
        assertTrue(bo.getNextBackoff() <= max);

        bo.onSendError();
        expected = (long) (backoffFactor * backoffFactor * backoffFactor * initialBackoffMillis);
        min = (long) (expected * (1 - jitter));
        max = (long) (expected * (1 + jitter));
        assertTrue(bo.getNextBackoff() >= min);
        assertTrue(bo.getNextBackoff() <= max);

        bo.onSendError();
        min = (long) (maxBackoffMillis * (1 - jitter));
        max = (long) (maxBackoffMillis * (1 + jitter));
        assertTrue(bo.getNextBackoff() >= min);
        assertTrue(bo.getNextBackoff() <= max);

        bo.onSendError();
        min = (long) (maxBackoffMillis * (1 - jitter));
        max = (long) (maxBackoffMillis * (1 + jitter));
        assertTrue(bo.getNextBackoff() >= min);
        assertTrue(bo.getNextBackoff() <= max);
    }