package io.sgr.telegram.bot.engine.utils;

import static io.sgr.telegram.bot.engine.utils.ExponentialBackOff.DEFAULT_INITIAL_INTERVAL_IN_MILLI;
import static io.sgr.telegram.bot.engine.utils.ExponentialBackOff.DEFAULT_MAX_INTERVAL_IN_MILLI;
import static io.sgr.telegram.bot.engine.utils.ExponentialBackOff.DEFAULT_MULTIPLIER;
import static io.sgr.telegram.bot.engine.utils.ExponentialBackOff.DEFAULT_RANDOMIZATION_FACTOR;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import java.util.Random;

public class ExponentialBackOffTest {

    @Test
    public void testGetInterval() {
        final ExponentialBackOff backOff = ExponentialBackOff.newInstance();
        assertEquals(DEFAULT_INITIAL_INTERVAL_IN_MILLI, backOff.getInitialIntervalInMilli());
        assertEquals(DEFAULT_RANDOMIZATION_FACTOR, backOff.getRandomizationFactor(), 0);
        assertEquals(DEFAULT_MULTIPLIER, backOff.getMultiplier(), 0);
        assertEquals(DEFAULT_MAX_INTERVAL_IN_MILLI, backOff.getMaxIntervalInMilli());

        long currentInterval = backOff.getCurrentInterval();
        long nextBackOff = backOff.getNextBackOffInMilli();
        assertEquals(currentInterval, nextBackOff, currentInterval * backOff.getRandomizationFactor());
        long lastInterval = currentInterval;

        int round = new Random().nextInt(100) + 1;
        while (round > 0) {
            currentInterval = backOff.getCurrentInterval();
            nextBackOff = backOff.getNextBackOffInMilli();
            if (lastInterval != backOff.getMaxIntervalInMilli()) {
                assertTrue(String.format("Round %d, last interval is %d but next back off is %d", round, lastInterval, nextBackOff), nextBackOff > lastInterval);
            }
            double delta = currentInterval * backOff.getRandomizationFactor();
            assertTrue(nextBackOff > currentInterval - delta);
            assertTrue(nextBackOff < currentInterval + delta);
            lastInterval = currentInterval;
            round--;
        }
    }

}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2017-2019 SgrAlpha%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A *%0A */%0A%0A
 package 
@@ -1978,17 +1978,44 @@
 100) + 1
-;
+00;  // At least 100 rounds.
 %0A       
@@ -2353,16 +2353,40 @@
 ackOff),
+%0A                       
  nextBac
@@ -2515,32 +2515,166 @@
      assertTrue(
+String.format(%22Next backoff %25d is smaller than expected %25d minus delta %25f%22, nextBackOff, currentInterval, delta),%0A                    
 nextBackOff %3E cu
@@ -2670,16 +2670,17 @@
 ackOff %3E
+=
  current
@@ -2691,24 +2691,28 @@
 rval - delta
+ + 1
 );%0A         
@@ -2725,16 +2725,148 @@
 ertTrue(
+String.format(%22Next backoff %25d is larger than expected %25d plus delta %25f%22, nextBackOff, currentInterval, delta),%0A                    
 nextBack
@@ -2870,16 +2870,17 @@
 ackOff %3C
+=
  current
@@ -2895,16 +2895,20 @@
  + delta
+ + 1
 );%0A     

Update one test