|
| 1 | +/* |
| 2 | + * Copyright 2013-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.awspring.cloud.sqs.listener.errorhandler; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Random; |
| 21 | +import java.util.concurrent.ThreadLocalRandom; |
| 22 | +import java.util.function.Supplier; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests for {@link Jitter} strategies. |
| 27 | + * |
| 28 | + * @author Tomaz Fernandes |
| 29 | + */ |
| 30 | +class JitterStrategiesTest { |
| 31 | + |
| 32 | + private final Supplier<Random> randomSupplier = ThreadLocalRandom::current; |
| 33 | + |
| 34 | + @Test |
| 35 | + void shouldReturnOriginalTimeoutForNoneJitter() { |
| 36 | + // Given |
| 37 | + int timeout = 100; |
| 38 | + Jitter.Context context = new Jitter.Context(timeout, randomSupplier); |
| 39 | + |
| 40 | + // When |
| 41 | + int result = Jitter.NONE.applyJitter(context); |
| 42 | + |
| 43 | + // Then |
| 44 | + assertThat(result).isEqualTo(timeout); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void shouldReturnValueBetweenZeroAndTimeoutForFullJitter() { |
| 49 | + // Given |
| 50 | + int timeout = 100; |
| 51 | + Jitter.Context context = new Jitter.Context(timeout, randomSupplier); |
| 52 | + |
| 53 | + // When |
| 54 | + int result = Jitter.FULL.applyJitter(context); |
| 55 | + |
| 56 | + // Then |
| 57 | + assertThat(result).isGreaterThanOrEqualTo(1); |
| 58 | + assertThat(result).isLessThanOrEqualTo(timeout); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void shouldReturnValueBetweenHalfAndTimeoutForHalfJitter() { |
| 63 | + // Given |
| 64 | + int timeout = 100; |
| 65 | + Jitter.Context context = new Jitter.Context(timeout, randomSupplier); |
| 66 | + |
| 67 | + // When |
| 68 | + int result = Jitter.HALF.applyJitter(context); |
| 69 | + |
| 70 | + // Then |
| 71 | + int expectedLowerBound = (int) Math.ceil(timeout / 2.0); |
| 72 | + assertThat(result).isGreaterThanOrEqualTo(expectedLowerBound); |
| 73 | + assertThat(result).isLessThanOrEqualTo(timeout); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void shouldHandleOddTimeoutForHalfJitter() { |
| 78 | + // Given |
| 79 | + int timeout = 101; // Odd number |
| 80 | + Jitter.Context context = new Jitter.Context(timeout, randomSupplier); |
| 81 | + |
| 82 | + // When |
| 83 | + int result = Jitter.HALF.applyJitter(context); |
| 84 | + |
| 85 | + // Then |
| 86 | + int expectedLowerBound = (int) Math.ceil(timeout / 2.0); // Should be 51 |
| 87 | + assertThat(result).isGreaterThanOrEqualTo(expectedLowerBound); |
| 88 | + assertThat(result).isLessThanOrEqualTo(timeout); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void shouldHandleMinimumTimeoutForAllStrategies() { |
| 93 | + // Given |
| 94 | + int timeout = 1; |
| 95 | + Jitter.Context context = new Jitter.Context(timeout, randomSupplier); |
| 96 | + |
| 97 | + // When & Then |
| 98 | + assertThat(Jitter.NONE.applyJitter(context)).isEqualTo(1); |
| 99 | + assertThat(Jitter.FULL.applyJitter(context)).isEqualTo(1); |
| 100 | + assertThat(Jitter.HALF.applyJitter(context)).isEqualTo(1); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void shouldEnsureMinimumValueOfOneForAllStrategies() { |
| 105 | + // Given |
| 106 | + int timeout = 2; |
| 107 | + Jitter.Context context = new Jitter.Context(timeout, randomSupplier); |
| 108 | + |
| 109 | + // When |
| 110 | + int noneResult = Jitter.NONE.applyJitter(context); |
| 111 | + int fullResult = Jitter.FULL.applyJitter(context); |
| 112 | + int halfResult = Jitter.HALF.applyJitter(context); |
| 113 | + |
| 114 | + // Then |
| 115 | + assertThat(noneResult).isGreaterThanOrEqualTo(1); |
| 116 | + assertThat(fullResult).isGreaterThanOrEqualTo(1); |
| 117 | + assertThat(halfResult).isGreaterThanOrEqualTo(1); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void shouldProduceDifferentResultsForMultipleCalls() { |
| 122 | + // Given |
| 123 | + int timeout = 100; |
| 124 | + Jitter.Context context = new Jitter.Context(timeout, randomSupplier); |
| 125 | + |
| 126 | + // When |
| 127 | + int result1 = Jitter.FULL.applyJitter(context); |
| 128 | + int result2 = Jitter.FULL.applyJitter(context); |
| 129 | + int result3 = Jitter.FULL.applyJitter(context); |
| 130 | + |
| 131 | + // Then |
| 132 | + // While it's theoretically possible for all results to be the same, |
| 133 | + // it's extremely unlikely with a good random number generator |
| 134 | + // We'll just verify they're all within the expected range |
| 135 | + assertThat(result1).isBetween(1, timeout); |
| 136 | + assertThat(result2).isBetween(1, timeout); |
| 137 | + assertThat(result3).isBetween(1, timeout); |
| 138 | + } |
| 139 | +} |
0 commit comments