@@ -205,30 +205,19 @@ public void cappedExponentialDelays() {
205205 Duration MAX_DELAY = Duration .ofSeconds (15 );
206206 final Delay cappedExponential = CappedExponential .of ().timeout (Duration .ofMinutes (20 )).maxDelay (MAX_DELAY ).powerBy (1.3 )
207207 .minDelay (Duration .ofSeconds (1 )).build ();
208- int [] results = { 1 , 1 , 1 , 1 , 2 , 2 , 3 , 4 , 6 , 8 , 10 , 13 , 15 , 15 , 15 , 15 };
208+ int [] results = { 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 8 , 11 , 14 , 15 , 15 , 15 , 15 , 15 , 15 };
209209 for (int tries = 0 ; tries <= 15 ; tries ++) {
210210 Duration delay = cappedExponential .nextDelay (tries );
211211 assertThat (results [tries ]).isEqualTo ((int ) delay .getSeconds ());
212- if (tries >= 12 ) {
212+ if (tries >= 11 ) {
213213 assertThat (MAX_DELAY .getSeconds ()).isEqualTo (delay .getSeconds ());
214214 }
215215 }
216216
217- // If minDelay is not set, the retry is without delay.
218- final Delay cappedExponentialNoDelay = CappedExponential .of ().timeout (Duration .ofSeconds (12 )).build ();
219- for (int tries = 0 ; tries <= 15 ; tries ++) {
220- Duration delay = cappedExponentialNoDelay .nextDelay (tries );
221- assertThat (0 ).isEqualTo ((int ) delay .getSeconds ());
222- if (tries >= 12 ) {
223- assertThat (0 ).isEqualTo (delay .getSeconds ());
224- }
225- }
226-
227217 // If powerBy is not passed, it's set to default 2.
228218 final Delay cappedExponentialNoPower = CappedExponential .of ().timeout (Duration .ofMinutes (20 )).maxDelay (MAX_DELAY )
229- .minDelay (Duration .ofSeconds (1 )).build ();
230-
231- int [] resultsNoPower = { 1 , 1 , 2 , 4 , 8 , 15 , 15 , 15 , 15 , 15 };
219+ .minDelay (Duration .ofSeconds (2 )).build ();
220+ int [] resultsNoPower = { 2 , 2 , 4 , 8 , 15 , 15 , 15 , 15 , 15 };
232221 for (int tries = 0 ; tries <= 6 ; tries ++) {
233222 Duration delay = cappedExponentialNoPower .nextDelay (tries );
234223 assertThat (resultsNoPower [tries ]).isEqualTo ((int ) delay .getSeconds ());
@@ -237,5 +226,46 @@ public void cappedExponentialDelays() {
237226 }
238227 }
239228
229+ // If timeout is reached the delay is 0
230+ final Delay cappedExponentialTimeout = CappedExponential .of ().timeout (Duration .ofSeconds (5 ))
231+ .maxDelay (Duration .ofSeconds (1 )).powerBy (1.0 ).minDelay (Duration .ofSeconds (1 )).build ();
232+
233+ int [] resultsTimeout = { 1 , 1 , 1 , 1 , 1 , 0 };
234+ for (int tries = 0 ; tries <= 5 ; tries ++) {
235+ Duration delay = cappedExponentialTimeout .nextDelay (tries );
236+ assertThat (resultsTimeout [tries ]).isEqualTo ((int ) delay .getSeconds ());
237+ if (tries >= 5 ) {
238+ assertThat (0 ).isEqualTo (delay .getSeconds ());
239+ }
240+ }
241+
242+ // If minDelay is not passed, it's set to default 1.
243+ final Delay cappedExponentialNoMinDelay = CappedExponential .of ().timeout (Duration .ofSeconds (5 ))
244+ .maxDelay (Duration .ofSeconds (1 )).powerBy (1.0 ).build ();
245+ int [] resultsNoMinDelay = { 1 , 1 , 1 , 1 , 1 , 0 };
246+ for (int tries = 0 ; tries <= 5 ; tries ++) {
247+ Duration delay = cappedExponentialNoMinDelay .nextDelay (tries );
248+ assertThat (resultsNoMinDelay [tries ]).isEqualTo ((int ) delay .getSeconds ());
249+ }
250+
251+ // If maxDelay is not passed, it's set to default 20 sec.
252+ final Delay cappedExponentialNoMaxDelay = CappedExponential .of ().timeout (Duration .ofMinutes (20 ))
253+ .minDelay (Duration .ofSeconds (2 )).build ();
254+ int [] resultsNoMaxDelay = { 2 , 2 , 4 , 8 , 16 , 20 , 20 , 20 , 20 };
255+ for (int tries = 0 ; tries <= 6 ; tries ++) {
256+ Duration delay = cappedExponentialNoMaxDelay .nextDelay (tries );
257+ assertThat (resultsNoMaxDelay [tries ]).isEqualTo ((int ) delay .getSeconds ());
258+ }
259+
260+ final Delay cappedExponentialSameMinMaxDelay = CappedExponential .of ().timeout (Duration .ofSeconds (5 ))
261+ .maxDelay (Duration .ofSeconds (1 )).powerBy (1.3 ).minDelay (Duration .ofSeconds (1 )).build ();
262+ int [] resultsSameMinMaxDelay = { 1 , 1 , 1 , 1 , 1 , 0 };
263+ for (int tries = 0 ; tries <= 5 ; tries ++) {
264+ Duration delay = cappedExponentialSameMinMaxDelay .nextDelay (tries );
265+ assertThat (resultsSameMinMaxDelay [tries ]).isEqualTo ((int ) delay .getSeconds ());
266+ if (tries >= 5 ) {
267+ assertThat (0 ).isEqualTo (delay .getSeconds ());
268+ }
269+ }
240270 }
241271}
0 commit comments