@@ -31,43 +31,50 @@ void testTransactionRetryBackoff() {
3131 // With jitter, actual values will be between 0 and these maxima
3232 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 };
3333
34- ExponentialBackoff backoff = ExponentialBackoff . TRANSACTION ;
35- for ( int retry = 0 ; retry < expectedMaxValues . length ; retry ++) {
36- long delay = backoff . calculateDelayBeforeNextRetryMs ( retry );
37- assertTrue ( delay >= 0 && delay <= Math . round ( expectedMaxValues [ retry ]), String .format ("Retry %d: delay should be 0-%d ms, got: %d" , retry , Math .round (expectedMaxValues [retry ]), delay ));
34+ for ( int attemptNumber = 0 ; attemptNumber < expectedMaxValues . length ; attemptNumber ++) {
35+ 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 ));
3838 }
3939 }
4040
4141 @ Test
4242 void testTransactionRetryBackoffRespectsMaximum () {
43- ExponentialBackoff backoff = ExponentialBackoff .TRANSACTION ;
44-
45- // Even at high retry counts, delay should never exceed 500ms
46- for (int retry = 0 ; retry < 25 ; retry ++) {
47- long delay = backoff .calculateDelayBeforeNextRetryMs (retry );
48- assertTrue (delay >= 0 && delay <= 500 , String .format ("Retry %d: delay should be capped at 500 ms, got: %d ms" , retry , delay ));
43+ // Even at high attempt numbers, backoff should never exceed 500ms
44+ for (int attemptNumber = 0 ; attemptNumber < 25 ; attemptNumber ++) {
45+ long backoff = ExponentialBackoff .calculateTransactionBackoffMs (attemptNumber );
46+ assertTrue (backoff >= 0 && backoff <= 500 ,
47+ String .format ("Attempt %d: backoff should be capped at 500 ms, got: %d ms" , attemptNumber , backoff ));
4948 }
5049 }
5150
5251 @ Test
5352 void testCustomJitter () {
54- ExponentialBackoff backoff = ExponentialBackoff .TRANSACTION ;
55-
56- // Expected delays with jitter=1.0 and growth factor 1.5
57- double [] expectedDelays = {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 };
58- double jitter = 1.0 ;
53+ // Expected backoffs with jitter=1.0 and growth factor 1.5
54+ double [] expectedBackoffs = {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 };
5955
60- for (int retry = 0 ; retry < expectedDelays .length ; retry ++) {
61- long delay = backoff .calculateDelayBeforeNextRetryMs (retry , jitter );
62- long expected = Math .round (expectedDelays [retry ]);
63- assertEquals (expected , delay , String .format ("Retry %d: with jitter=1.0, delay should be %d ms" , retry , expected ));
56+ // Test with jitter = 1.0
57+ ExponentialBackoff .setTestJitterSupplier (() -> 1.0 );
58+ try {
59+ for (int attemptNumber = 0 ; attemptNumber < expectedBackoffs .length ; attemptNumber ++) {
60+ long backoff = ExponentialBackoff .calculateTransactionBackoffMs (attemptNumber );
61+ long expected = Math .round (expectedBackoffs [attemptNumber ]);
62+ assertEquals (expected , backoff ,
63+ String .format ("Attempt %d: with jitter=1.0, backoff should be %d ms" , attemptNumber , expected ));
64+ }
65+ } finally {
66+ ExponentialBackoff .clearTestJitterSupplier ();
6467 }
6568
66- // With jitter = 0, all delays should be 0
67- jitter = 0 ;
68- for (int retry = 0 ; retry < 10 ; retry ++) {
69- long delay = backoff .calculateDelayBeforeNextRetryMs (retry , jitter );
70- assertEquals (0 , delay , "With jitter=0, delay should always be 0 ms" );
69+ // Test with jitter = 0, all backoffs should be 0
70+ ExponentialBackoff .setTestJitterSupplier (() -> 0.0 );
71+ try {
72+ for (int attemptNumber = 0 ; attemptNumber < 10 ; attemptNumber ++) {
73+ long backoff = ExponentialBackoff .calculateTransactionBackoffMs (attemptNumber );
74+ assertEquals (0 , backoff , "With jitter=0, backoff should always be 0 ms" );
75+ }
76+ } finally {
77+ ExponentialBackoff .clearTestJitterSupplier ();
7178 }
7279 }
7380}
0 commit comments