|
16 | 16 |
|
17 | 17 | package org.springframework.retry.backoff; |
18 | 18 |
|
| 19 | +import org.apache.commons.logging.Log; |
19 | 20 | import org.junit.jupiter.api.Test; |
| 21 | +import org.mockito.ArgumentCaptor; |
| 22 | +import org.springframework.beans.DirectFieldAccessor; |
20 | 23 |
|
21 | 24 | import static org.assertj.core.api.Assertions.assertThat; |
22 | | -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 25 | +import static org.mockito.Mockito.*; |
23 | 26 |
|
24 | 27 | /** |
25 | 28 | * @author Rob Harrop |
@@ -80,49 +83,49 @@ public void testMaximumBackOff() { |
80 | 83 | } |
81 | 84 |
|
82 | 85 | @Test |
83 | | - public void testMultiBackOff() { |
| 86 | + public void testSetMultiplierWithWarning() { |
| 87 | + Log logMock = mock(Log.class); |
84 | 88 | ExponentialBackOffPolicy strategy = new ExponentialBackOffPolicy(); |
85 | | - long seed = 40; |
86 | | - double multiplier = 1.2; |
87 | | - strategy.setInitialInterval(seed); |
88 | | - strategy.setMultiplier(multiplier); |
89 | | - strategy.setSleeper(sleeper); |
90 | | - BackOffContext context = strategy.start(null); |
91 | | - for (int x = 0; x < 5; x++) { |
92 | | - strategy.backOff(context); |
93 | | - assertThat(sleeper.getLastBackOff()).isEqualTo(seed); |
94 | | - seed *= multiplier; |
95 | | - } |
| 89 | + |
| 90 | + DirectFieldAccessor accessor = new DirectFieldAccessor(strategy); |
| 91 | + accessor.setPropertyValue("logger", logMock); |
| 92 | + |
| 93 | + strategy.setMultiplier(1.0); |
| 94 | + |
| 95 | + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 96 | + verify(logMock).warn(captor.capture()); |
| 97 | + assertThat(captor.getValue()) |
| 98 | + .isEqualTo("Multiplier must be > 1.0 for effective exponential backoff, but was 1.0"); |
96 | 99 | } |
97 | 100 |
|
98 | 101 | @Test |
99 | | - public void testMultiBackOffWithInitialDelaySupplier() { |
| 102 | + public void testSetInitialIntervalWithWarning() { |
| 103 | + Log logMock = mock(Log.class); |
100 | 104 | ExponentialBackOffPolicy strategy = new ExponentialBackOffPolicy(); |
101 | | - long seed = 40; |
102 | | - double multiplier = 1.2; |
103 | | - strategy.initialIntervalSupplier(() -> 40L); |
104 | | - strategy.setMultiplier(multiplier); |
105 | | - strategy.setSleeper(sleeper); |
106 | | - BackOffContext context = strategy.start(null); |
107 | | - for (int x = 0; x < 5; x++) { |
108 | | - strategy.backOff(context); |
109 | | - assertThat(sleeper.getLastBackOff()).isEqualTo(seed); |
110 | | - seed *= multiplier; |
111 | | - } |
| 105 | + |
| 106 | + DirectFieldAccessor accessor = new DirectFieldAccessor(strategy); |
| 107 | + accessor.setPropertyValue("logger", logMock); |
| 108 | + |
| 109 | + strategy.setInitialInterval(0); |
| 110 | + |
| 111 | + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 112 | + verify(logMock).warn(captor.capture()); |
| 113 | + assertThat(captor.getValue()).isEqualTo("Initial interval must be at least 1, but was 0"); |
112 | 114 | } |
113 | 115 |
|
114 | 116 | @Test |
115 | | - public void testInterruptedStatusIsRestored() { |
| 117 | + public void testSetMaxIntervalWithWarning() { |
| 118 | + Log logMock = mock(Log.class); |
116 | 119 | ExponentialBackOffPolicy strategy = new ExponentialBackOffPolicy(); |
117 | | - strategy.setSleeper(new Sleeper() { |
118 | | - @Override |
119 | | - public void sleep(long backOffPeriod) throws InterruptedException { |
120 | | - throw new InterruptedException("foo"); |
121 | | - } |
122 | | - }); |
123 | | - BackOffContext context = strategy.start(null); |
124 | | - assertThatExceptionOfType(BackOffInterruptedException.class).isThrownBy(() -> strategy.backOff(context)); |
125 | | - assertThat(Thread.interrupted()).isTrue(); |
| 120 | + |
| 121 | + DirectFieldAccessor accessor = new DirectFieldAccessor(strategy); |
| 122 | + accessor.setPropertyValue("logger", logMock); |
| 123 | + |
| 124 | + strategy.setMaxInterval(0); |
| 125 | + |
| 126 | + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 127 | + verify(logMock).warn(captor.capture()); |
| 128 | + assertThat(captor.getValue()).isEqualTo("Max interval must be positive, but was 0"); |
126 | 129 | } |
127 | 130 |
|
128 | 131 | } |
0 commit comments