1414 * limitations under the License.
1515 */
1616
17- package com .mongodb .internal ;
17+ package com .mongodb .internal . time ;
1818
19- import com .mongodb .internal .time .ExponentialBackoff ;
2019import org .junit .jupiter .api .Test ;
2120
2221import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -31,17 +30,18 @@ void testTransactionRetryBackoff() {
3130 // With jitter, actual values will be between 0 and these maxima
3231 double [] expectedMaxValues = {5.0 , 7.5 , 11.25 , 16.875 , 25.3125 , 37.96875 , 56.953125 , 85.4296875 , 128.14453125 , 192.21679688 , 288.32519531 , 432.48779297 , 500.0 };
3332
34- for (int attemptNumber = 0 ; attemptNumber < expectedMaxValues .length ; attemptNumber ++) {
33+ for (int attemptNumber = 1 ; attemptNumber <= expectedMaxValues .length ; attemptNumber ++) {
3534 long backoff = ExponentialBackoff .calculateTransactionBackoffMs (attemptNumber );
36- assertTrue (backoff >= 0 && backoff <= Math .round (expectedMaxValues [attemptNumber ]),
37- String .format ("Attempt %d: backoff should be 0-%d ms, got: %d" , attemptNumber , Math .round (expectedMaxValues [attemptNumber ]), backoff ));
35+ assertTrue (backoff >= 0 && backoff <= Math .round (expectedMaxValues [attemptNumber - 1 ]),
36+ String .format ("Attempt %d: backoff should be 0-%d ms, got: %d" , attemptNumber ,
37+ Math .round (expectedMaxValues [attemptNumber - 1 ]), backoff ));
3838 }
3939 }
4040
4141 @ Test
4242 void testTransactionRetryBackoffRespectsMaximum () {
4343 // Even at high attempt numbers, backoff should never exceed 500ms
44- for (int attemptNumber = 0 ; attemptNumber < 25 ; attemptNumber ++) {
44+ for (int attemptNumber = 1 ; attemptNumber < 26 ; attemptNumber ++) {
4545 long backoff = ExponentialBackoff .calculateTransactionBackoffMs (attemptNumber );
4646 assertTrue (backoff >= 0 && backoff <= 500 ,
4747 String .format ("Attempt %d: backoff should be capped at 500 ms, got: %d ms" , attemptNumber , backoff ));
@@ -56,9 +56,9 @@ void testCustomJitter() {
5656 // Test with jitter = 1.0
5757 ExponentialBackoff .setTestJitterSupplier (() -> 1.0 );
5858 try {
59- for (int attemptNumber = 0 ; attemptNumber < expectedBackoffs .length ; attemptNumber ++) {
59+ for (int attemptNumber = 1 ; attemptNumber <= expectedBackoffs .length ; attemptNumber ++) {
6060 long backoff = ExponentialBackoff .calculateTransactionBackoffMs (attemptNumber );
61- long expected = Math .round (expectedBackoffs [attemptNumber ]);
61+ long expected = Math .round (expectedBackoffs [attemptNumber - 1 ]);
6262 assertEquals (expected , backoff ,
6363 String .format ("Attempt %d: with jitter=1.0, backoff should be %d ms" , attemptNumber , expected ));
6464 }
0 commit comments