|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.utils.backoff.impl; |
| 18 | + |
| 19 | +import org.apache.commons.lang3.reflect.FieldUtils; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +public class ExponentialWithJitterBackoffTest { |
| 28 | + |
| 29 | + private ExponentialWithJitterBackoff backoff; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setUp() { |
| 33 | + backoff = new ExponentialWithJitterBackoff(); |
| 34 | + Map<String, Object> params = new HashMap<>(); |
| 35 | + params.put(ExponentialWithJitterBackoff.MIN_DELAY_MS_CONFIG_KEY, "1"); |
| 36 | + params.put(ExponentialWithJitterBackoff.MAX_DELAY_MS_CONFIG_KEY, "8"); |
| 37 | + backoff.configure("test", params); |
| 38 | + backoff.reset(); |
| 39 | + } |
| 40 | + |
| 41 | + private int attemptNumber() throws IllegalAccessException { |
| 42 | + return (Integer) FieldUtils.readField(backoff, "attemptNumber", true); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testAttemptCyclesUpToMaxAndBackToBase() throws IllegalAccessException { |
| 47 | + // the maximum delay is used for two consecutive waits before the delay decreases back to base |
| 48 | + int[] expected = {1, 2, 3, 3, 2, 1, 0, 1, 2, 3, 3, 2, 1, 0}; |
| 49 | + for (int i = 0; i < expected.length; i++) { |
| 50 | + backoff.waitBeforeRetry(); |
| 51 | + Assert.assertEquals("attempt number after wait #" + (i + 1), expected[i], attemptNumber()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testTimeToWaitStaysWithinBounds() throws IllegalAccessException { |
| 57 | + for (int i = 0; i < 20; i++) { |
| 58 | + long wait = backoff.getTimeToWait(); |
| 59 | + long delay = (long) Math.min(Math.pow(2, attemptNumber()), 8); |
| 60 | + Assert.assertTrue("wait " + wait + " below delay " + delay, wait >= delay); |
| 61 | + Assert.assertTrue("wait " + wait + " above delay plus jitter", wait < delay + Math.max(1, delay / 2)); |
| 62 | + backoff.waitBeforeRetry(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testResetRestartsIncreasingFromBase() throws IllegalAccessException { |
| 68 | + backoff.waitBeforeRetry(); |
| 69 | + backoff.waitBeforeRetry(); |
| 70 | + backoff.reset(); |
| 71 | + Assert.assertEquals(0, attemptNumber()); |
| 72 | + backoff.waitBeforeRetry(); |
| 73 | + Assert.assertEquals(1, attemptNumber()); |
| 74 | + } |
| 75 | +} |
0 commit comments