2121
2222public final class CustomerGroupsTest {
2323
24+ private static final int MAX_RETRIES = 5 ;
25+ private static final long INITIAL_RETRY_DELAY_MS = 2000 ;
26+ private static final long DELAY_BETWEEN_OPERATIONS_MS = 2000 ;
27+
2428 private SquareClient client ;
2529 private String groupId ;
2630
@@ -32,78 +36,152 @@ public void before() {
3236
3337 @ AfterEach
3438 public void deleteTestCustomerGroup () {
35- client .customers ()
36- .groups ()
37- .delete (DeleteGroupsRequest .builder ().groupId (groupId ).build ());
39+ try {
40+ // Add delay before cleanup
41+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
42+ withRetry (() -> client .customers ()
43+ .groups ()
44+ .delete (DeleteGroupsRequest .builder ().groupId (groupId ).build ()));
45+ } catch (Exception e ) {
46+ System .out .println ("Warning: Failed to delete test customer group: " + e .getMessage ());
47+ }
3848 }
3949
4050 @ Test
41- public void testCreateAndListCustomerGroup () {
42- SyncPagingIterable <CustomerGroup > response = client .customers ().groups ().list ();
51+ public void testCreateAndListCustomerGroup () throws InterruptedException {
52+ // Add delay before test
53+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
54+
55+ SyncPagingIterable <CustomerGroup > response = withRetry (() ->
56+ client .customers ().groups ().list ());
57+
4358 Assertions .assertNotNull (response );
4459 List <CustomerGroup > groups = response .getItems ();
4560 Assertions .assertFalse (groups .isEmpty ());
4661 }
4762
4863 @ Test
49- public void testRetrieveCustomerGroup () {
50- GetCustomerGroupResponse response = client .customers ()
51- .groups ()
52- .get (GetGroupsRequest .builder ().groupId (groupId ).build ());
64+ public void testRetrieveCustomerGroup () throws InterruptedException {
65+ // Add delay before test
66+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
67+
68+ GetCustomerGroupResponse response = withRetry (() ->
69+ client .customers ()
70+ .groups ()
71+ .get (GetGroupsRequest .builder ().groupId (groupId ).build ()));
72+
5373 Assertions .assertTrue (response .getGroup ().isPresent ());
5474 Assertions .assertEquals (groupId , response .getGroup ().get ().getId ().get ());
5575 }
5676
5777 @ Test
58- public void testUpdateCustomerGroup () {
78+ public void testUpdateCustomerGroup () throws InterruptedException {
79+ // Add delay before test
80+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
81+
5982 String newName = "Updated-" + UUID .randomUUID ();
60- UpdateCustomerGroupResponse response = client .customers ()
61- .groups ()
62- .update (UpdateCustomerGroupRequest .builder ()
63- .groupId (groupId )
64- .group (CustomerGroup .builder ().name (newName ).build ())
65- .build ());
83+ UpdateCustomerGroupResponse response = withRetry (() ->
84+ client .customers ()
85+ .groups ()
86+ .update (UpdateCustomerGroupRequest .builder ()
87+ .groupId (groupId )
88+ .group (CustomerGroup .builder ().name (newName ).build ())
89+ .build ()));
90+
6691 Assertions .assertTrue (response .getGroup ().isPresent ());
6792 Assertions .assertEquals (newName , response .getGroup ().get ().getName ());
6893 }
6994
7095 @ Test
71- public void testRetrieveNonExistentGroup () {
96+ public void testRetrieveNonExistentGroup () throws InterruptedException {
97+ // Add delay before test
98+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
99+
72100 Assertions .assertThrows (SquareApiException .class , () -> {
73- client .customers ()
74- .groups ()
75- .get (GetGroupsRequest .builder ().groupId ("not existent id" ).build ());
101+ withRetry (() ->
102+ client .customers ()
103+ .groups ()
104+ .get (GetGroupsRequest .builder ().groupId ("not existent id" ).build ()));
76105 });
77106 }
78107
79108 @ Test
80- public void testCreateGroupWithInvalidData () {
109+ public void testCreateGroupWithInvalidData () throws InterruptedException {
110+ // Add delay before test
111+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
112+
81113 Assertions .assertThrows (SquareApiException .class , () -> {
114+ withRetry (() ->
115+ client .customers ()
116+ .groups ()
117+ .create (CreateCustomerGroupRequest .builder ()
118+ .group (CustomerGroup .builder ()
119+ // Empty name should be invalid
120+ .name ("" )
121+ .build ())
122+ .idempotencyKey (UUID .randomUUID ().toString ())
123+ .build ()));
124+ });
125+ }
126+
127+ private String createTestCustomerGroup () {
128+ CreateCustomerGroupResponse response = withRetry (() ->
82129 client .customers ()
83130 .groups ()
84131 .create (CreateCustomerGroupRequest .builder ()
85132 .group (CustomerGroup .builder ()
86- // Empty name should be invalid
87- .name ("" )
133+ .name ("Default-" + UUID .randomUUID ())
88134 .build ())
89135 .idempotencyKey (UUID .randomUUID ().toString ())
90- .build ());
91- });
92- }
93-
94- private String createTestCustomerGroup () {
95- CreateCustomerGroupResponse response = client .customers ()
96- .groups ()
97- .create (CreateCustomerGroupRequest .builder ()
98- .group (CustomerGroup .builder ()
99- .name ("Default-" + UUID .randomUUID ())
100- .build ())
101- .idempotencyKey (UUID .randomUUID ().toString ())
102- .build ());
136+ .build ()));
137+
103138 Optional <CustomerGroup > group = response .getGroup ();
104139 if (!group .isPresent ()) {
105140 throw new RuntimeException ("Failed to create test customer group." );
106141 }
107142 return group .get ().getId ().get ();
108143 }
109- }
144+
145+ private interface ApiCall <T > {
146+ T execute () throws SquareApiException ;
147+ }
148+
149+ private <T > T withRetry (ApiCall <T > apiCall ) {
150+ int attempt = 0 ;
151+ SquareApiException lastException = null ;
152+
153+ while (attempt < MAX_RETRIES ) {
154+ try {
155+ if (attempt > 0 ) {
156+ // Calculate exponential backoff delay
157+ long delayMs = INITIAL_RETRY_DELAY_MS * (long ) Math .pow (2 , attempt - 1 );
158+ System .out .printf ("Retry attempt %d after %d ms delay%n" , attempt + 1 , delayMs );
159+ Thread .sleep (delayMs );
160+ }
161+
162+ return apiCall .execute ();
163+
164+ } catch (SquareApiException e ) {
165+ lastException = e ;
166+
167+ // Check if it's a rate limit error
168+ if (e .getErrors () != null &&
169+ !e .getErrors ().isEmpty () &&
170+ "RATE_LIMIT_ERROR" .equals (e .getErrors ().get (0 ).getCategory ())) {
171+ System .out .printf ("Rate limited on attempt %d%n" , attempt + 1 );
172+ attempt ++;
173+ continue ;
174+ }
175+
176+ // For other API errors, throw immediately
177+ throw e ;
178+ } catch (InterruptedException e ) {
179+ Thread .currentThread ().interrupt ();
180+ throw new RuntimeException ("Interrupted during retry delay" , e );
181+ }
182+ }
183+
184+ // If we've exhausted all retries, throw the last exception
185+ throw lastException ;
186+ }
187+ }
0 commit comments