1
1
package org .opentripplanner .ext .siri .updater .azure ;
2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
4
5
import static org .junit .jupiter .api .Assertions .assertThrows ;
5
6
import static org .junit .jupiter .api .Assertions .assertTrue ;
6
- import static org .junit .jupiter .api .Assertions .assertFalse ;
7
7
import static org .mockito .Mockito .*;
8
8
9
9
import com .azure .core .util .ExpandableStringEnum ;
@@ -56,21 +56,22 @@ public void setUp() throws Exception {
56
56
when (mockConfig .isFuzzyTripMatching ()).thenReturn (true );
57
57
58
58
// Create a spy on AbstractAzureSiriUpdater with the mock configuration
59
- updater = spy (new AbstractAzureSiriUpdater (mockConfig ) {
60
- @ Override
61
- protected void messageConsumer (ServiceBusReceivedMessageContext messageContext ) {
62
- }
63
-
64
- @ Override
65
- protected void errorConsumer (ServiceBusErrorContext errorContext ) {
66
- }
67
-
68
- @ Override
69
- protected void initializeData (String url ,
70
- Consumer <ServiceBusReceivedMessageContext > consumer
71
- ) throws URISyntaxException {
72
- }
73
- });
59
+ updater =
60
+ spy (
61
+ new AbstractAzureSiriUpdater (mockConfig ) {
62
+ @ Override
63
+ protected void messageConsumer (ServiceBusReceivedMessageContext messageContext ) {}
64
+
65
+ @ Override
66
+ protected void errorConsumer (ServiceBusErrorContext errorContext ) {}
67
+
68
+ @ Override
69
+ protected void initializeData (
70
+ String url ,
71
+ Consumer <ServiceBusReceivedMessageContext > consumer
72
+ ) throws URISyntaxException {}
73
+ }
74
+ );
74
75
75
76
task = mock (AbstractAzureSiriUpdater .CheckedRunnable .class );
76
77
}
@@ -81,8 +82,8 @@ protected void initializeData(String url,
81
82
*/
82
83
@ Test
83
84
void testExecuteWithRetry_FullBackoffSequence () throws Throwable {
84
- final int totalRunCalls = 10 ; // 9 failures + 1 success
85
- final int totalSleepCalls = 9 ; // 9 retries
85
+ final int totalRunCalls = 10 ; // 9 failures + 1 success
86
+ final int totalSleepCalls = 9 ; // 9 retries
86
87
87
88
doNothing ().when (updater ).sleep (anyInt ());
88
89
@@ -97,7 +98,8 @@ void testExecuteWithRetry_FullBackoffSequence() throws Throwable {
97
98
.doThrow (createServiceBusException (ServiceBusFailureReason .SERVICE_BUSY ))
98
99
.doThrow (createServiceBusException (ServiceBusFailureReason .SERVICE_BUSY ))
99
100
.doNothing () // Succeed on the 10th attempt
100
- .when (task ).run ();
101
+ .when (task )
102
+ .run ();
101
103
102
104
updater .executeWithRetry (task , "Test Task" );
103
105
@@ -126,14 +128,20 @@ void testExecuteWithRetry_FullBackoffSequence() throws Throwable {
126
128
public void testExecuteWithRetry_NonRetryableException () throws Throwable {
127
129
doNothing ().when (updater ).sleep (anyInt ());
128
130
129
- ServiceBusException serviceBusException = createServiceBusException (ServiceBusFailureReason .MESSAGE_SIZE_EXCEEDED );
131
+ ServiceBusException serviceBusException = createServiceBusException (
132
+ ServiceBusFailureReason .MESSAGE_SIZE_EXCEEDED
133
+ );
130
134
131
135
doThrow (serviceBusException ).when (task ).run ();
132
136
133
137
try {
134
138
updater .executeWithRetry (task , "Test Task" );
135
139
} catch (ServiceBusException e ) {
136
- assertEquals (ServiceBusFailureReason .MESSAGE_SIZE_EXCEEDED , e .getReason (), "Exception should have reason MESSAGE_SIZE_EXCEEDED" );
140
+ assertEquals (
141
+ ServiceBusFailureReason .MESSAGE_SIZE_EXCEEDED ,
142
+ e .getReason (),
143
+ "Exception should have reason MESSAGE_SIZE_EXCEEDED"
144
+ );
137
145
}
138
146
139
147
verify (updater , never ()).sleep (anyInt ());
@@ -153,12 +161,15 @@ public void testExecuteWithRetry_MultipleRetriesThenSuccess() throws Throwable {
153
161
.doThrow (createServiceBusException (ServiceBusFailureReason .SERVICE_BUSY ))
154
162
.doThrow (createServiceBusException (ServiceBusFailureReason .SERVICE_BUSY ))
155
163
.doNothing ()
156
- .when (task ).run ();
164
+ .when (task )
165
+ .run ();
157
166
158
167
doAnswer (invocation -> {
159
- latch .countDown ();
160
- return null ;
161
- }).when (updater ).sleep (anyInt ());
168
+ latch .countDown ();
169
+ return null ;
170
+ })
171
+ .when (updater )
172
+ .sleep (anyInt ());
162
173
163
174
updater .executeWithRetry (task , "Test Task" );
164
175
@@ -169,11 +180,14 @@ public void testExecuteWithRetry_MultipleRetriesThenSuccess() throws Throwable {
169
180
verify (updater , times (retriesBeforeSuccess )).sleep (sleepCaptor .capture ());
170
181
171
182
var sleepDurations = sleepCaptor .getAllValues ();
172
- long [] expectedBackoffSequence = {1000 , 2000 , 4000 };
183
+ long [] expectedBackoffSequence = { 1000 , 2000 , 4000 };
173
184
174
185
for (int i = 0 ; i < expectedBackoffSequence .length ; i ++) {
175
- assertEquals (expectedBackoffSequence [i ], Long .valueOf (sleepDurations .get (i )),
176
- "Backoff duration mismatch at retry " + (i + 1 ));
186
+ assertEquals (
187
+ expectedBackoffSequence [i ],
188
+ Long .valueOf (sleepDurations .get (i )),
189
+ "Backoff duration mismatch at retry " + (i + 1 )
190
+ );
177
191
}
178
192
179
193
verify (task , times (retriesBeforeSuccess + 1 )).run ();
@@ -205,14 +219,17 @@ public void testExecuteWithRetry_OneRetryThenSuccess() throws Throwable {
205
219
206
220
doThrow (createServiceBusException (ServiceBusFailureReason .SERVICE_BUSY ))
207
221
.doNothing ()
208
- .when (task ).run ();
222
+ .when (task )
223
+ .run ();
209
224
210
225
doAnswer (invocation -> {
211
- if (invocation .getArgument (0 ).equals (1000 )) {
212
- latch .countDown ();
213
- }
214
- return null ;
215
- }).when (updater ).sleep (anyInt ());
226
+ if (invocation .getArgument (0 ).equals (1000 )) {
227
+ latch .countDown ();
228
+ }
229
+ return null ;
230
+ })
231
+ .when (updater )
232
+ .sleep (anyInt ());
216
233
217
234
updater .executeWithRetry (task , "Test Task" );
218
235
@@ -232,10 +249,17 @@ public void testExecuteWithRetry_OneRetryThenSuccess() throws Throwable {
232
249
@ ParameterizedTest (name = "shouldRetry with reason {0} should return {1}" )
233
250
@ MethodSource ("provideServiceBusFailureReasons" )
234
251
@ DisplayName ("Test shouldRetry for all ServiceBusFailureReason values" )
235
- void testShouldRetry_ServiceBusFailureReasons (ServiceBusFailureReason reason , boolean expectedRetry ) throws Exception {
252
+ void testShouldRetry_ServiceBusFailureReasons (
253
+ ServiceBusFailureReason reason ,
254
+ boolean expectedRetry
255
+ ) throws Exception {
236
256
ServiceBusException serviceBusException = createServiceBusException (reason );
237
257
boolean result = updater .shouldRetry (serviceBusException );
238
- assertEquals (expectedRetry , result , "shouldRetry should return " + expectedRetry + " for reason " + reason );
258
+ assertEquals (
259
+ expectedRetry ,
260
+ result ,
261
+ "shouldRetry should return " + expectedRetry + " for reason " + reason
262
+ );
239
263
}
240
264
241
265
/**
@@ -258,7 +282,11 @@ public void testShouldRetry_NonServiceBusException() {
258
282
public void testShouldRetry_CoversAllReasons () {
259
283
long enumCount = getExpandableStringEnumValues (ServiceBusFailureReason .class ).size ();
260
284
long testCaseCount = provideServiceBusFailureReasons ().count ();
261
- assertEquals (enumCount , testCaseCount , "All ServiceBusFailureReason values should be covered by tests." );
285
+ assertEquals (
286
+ enumCount ,
287
+ testCaseCount ,
288
+ "All ServiceBusFailureReason values should be covered by tests."
289
+ );
262
290
}
263
291
264
292
@ Test
@@ -268,15 +296,24 @@ void testExecuteWithRetry_InterruptedException() throws Throwable {
268
296
269
297
doThrow (createServiceBusException (ServiceBusFailureReason .SERVICE_BUSY ))
270
298
.doThrow (new InterruptedException ("Sleep interrupted" ))
271
- .when (task ).run ();
299
+ .when (task )
300
+ .run ();
272
301
273
302
doNothing ().when (updater ).sleep (1000 );
274
303
275
- InterruptedException thrownException = assertThrows (InterruptedException .class , () -> {
276
- updater .executeWithRetry (task , "Test Task" );
277
- }, "Expected executeWithRetry to throw InterruptedException" );
304
+ InterruptedException thrownException = assertThrows (
305
+ InterruptedException .class ,
306
+ () -> {
307
+ updater .executeWithRetry (task , "Test Task" );
308
+ },
309
+ "Expected executeWithRetry to throw InterruptedException"
310
+ );
278
311
279
- assertEquals ("Sleep interrupted" , thrownException .getMessage (), "Exception message should match" );
312
+ assertEquals (
313
+ "Sleep interrupted" ,
314
+ thrownException .getMessage (),
315
+ "Exception message should match"
316
+ );
280
317
verify (updater , times (expectedSleepCalls )).sleep (1000 );
281
318
verify (task , times (expectedRunCalls )).run ();
282
319
assertTrue (Thread .currentThread ().isInterrupted (), "Thread should be interrupted" );
@@ -291,7 +328,8 @@ void testExecuteWithRetry_OtpHttpClientException() throws Throwable {
291
328
.doThrow (new OtpHttpClientException ("could not get historical data" ))
292
329
.doThrow (new OtpHttpClientException ("could not get historical data" ))
293
330
.doNothing ()
294
- .when (task ).run ();
331
+ .when (task )
332
+ .run ();
295
333
296
334
doNothing ().when (updater ).sleep (anyInt ());
297
335
@@ -304,8 +342,11 @@ void testExecuteWithRetry_OtpHttpClientException() throws Throwable {
304
342
List <Integer > expectedBackoffSequence = Arrays .asList (1000 , 2000 , 4000 );
305
343
306
344
for (int i = 0 ; i < retryAttempts ; i ++) {
307
- assertEquals (expectedBackoffSequence .get (i ), sleepDurations .get (i ),
308
- "Backoff duration mismatch at retry " + (i + 1 ));
345
+ assertEquals (
346
+ expectedBackoffSequence .get (i ),
347
+ sleepDurations .get (i ),
348
+ "Backoff duration mismatch at retry " + (i + 1 )
349
+ );
309
350
}
310
351
311
352
verify (task , times (retryAttempts + 1 )).run ();
@@ -318,9 +359,13 @@ void testExecuteWithRetry_UnexpectedException() throws Throwable {
318
359
Exception unexpectedException = new NullPointerException ("Unexpected null value" );
319
360
doThrow (unexpectedException ).when (task ).run ();
320
361
321
- Exception thrown = assertThrows (NullPointerException .class , () -> {
322
- updater .executeWithRetry (task , "Test Task" );
323
- }, "Expected executeWithRetry to throw NullPointerException" );
362
+ Exception thrown = assertThrows (
363
+ NullPointerException .class ,
364
+ () -> {
365
+ updater .executeWithRetry (task , "Test Task" );
366
+ },
367
+ "Expected executeWithRetry to throw NullPointerException"
368
+ );
324
369
325
370
assertEquals ("Unexpected null value" , thrown .getMessage (), "Exception message should match" );
326
371
verify (updater , never ()).sleep (anyInt ());
@@ -344,7 +389,6 @@ private static Stream<Arguments> provideServiceBusFailureReasons() {
344
389
Arguments .of (ServiceBusFailureReason .QUOTA_EXCEEDED , true ),
345
390
Arguments .of (ServiceBusFailureReason .GENERAL_ERROR , true ),
346
391
Arguments .of (ServiceBusFailureReason .UNAUTHORIZED , true ),
347
-
348
392
// Non-Retryable Errors
349
393
Arguments .of (ServiceBusFailureReason .MESSAGING_ENTITY_NOT_FOUND , false ),
350
394
Arguments .of (ServiceBusFailureReason .MESSAGING_ENTITY_DISABLED , false ),
@@ -361,7 +405,10 @@ private static Stream<Arguments> provideServiceBusFailureReasons() {
361
405
* @return A ServiceBusException instance with the specified reason.
362
406
*/
363
407
private ServiceBusException createServiceBusException (ServiceBusFailureReason reason ) {
364
- ServiceBusException exception = new ServiceBusException (new Throwable (), ServiceBusErrorSource .RECEIVE );
408
+ ServiceBusException exception = new ServiceBusException (
409
+ new Throwable (),
410
+ ServiceBusErrorSource .RECEIVE
411
+ );
365
412
try {
366
413
Field reasonField = ServiceBusException .class .getDeclaredField ("reason" );
367
414
reasonField .setAccessible (true );
@@ -379,7 +426,9 @@ private ServiceBusException createServiceBusException(ServiceBusFailureReason re
379
426
* @param <T> The type parameter extending ExpandableStringEnum.
380
427
* @return A Collection of all registered instances.
381
428
*/
382
- private static <T extends ExpandableStringEnum <T >> Collection <T > getExpandableStringEnumValues (Class <T > clazz ) {
429
+ private static <T extends ExpandableStringEnum <T >> Collection <T > getExpandableStringEnumValues (
430
+ Class <T > clazz
431
+ ) {
383
432
try {
384
433
Method valuesMethod = ExpandableStringEnum .class .getDeclaredMethod ("values" , Class .class );
385
434
valuesMethod .setAccessible (true );
@@ -390,4 +439,4 @@ private static <T extends ExpandableStringEnum<T>> Collection<T> getExpandableSt
390
439
throw new RuntimeException ("Failed to retrieve values from ExpandableStringEnum." , e );
391
440
}
392
441
}
393
- }
442
+ }
0 commit comments