21
21
22
22
public final class CustomerGroupsTest {
23
23
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
+
24
28
private SquareClient client ;
25
29
private String groupId ;
26
30
@@ -32,78 +36,150 @@ public void before() {
32
36
33
37
@ AfterEach
34
38
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
+ }
38
48
}
39
49
40
50
@ 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
+
43
58
Assertions .assertNotNull (response );
44
59
List <CustomerGroup > groups = response .getItems ();
45
60
Assertions .assertFalse (groups .isEmpty ());
46
61
}
47
62
48
63
@ 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
+
53
73
Assertions .assertTrue (response .getGroup ().isPresent ());
54
74
Assertions .assertEquals (groupId , response .getGroup ().get ().getId ().get ());
55
75
}
56
76
57
77
@ Test
58
- public void testUpdateCustomerGroup () {
78
+ public void testUpdateCustomerGroup () throws InterruptedException {
79
+ // Add delay before test
80
+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
81
+
59
82
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
+
66
91
Assertions .assertTrue (response .getGroup ().isPresent ());
67
92
Assertions .assertEquals (newName , response .getGroup ().get ().getName ());
68
93
}
69
94
70
95
@ Test
71
- public void testRetrieveNonExistentGroup () {
96
+ public void testRetrieveNonExistentGroup () throws InterruptedException {
97
+ // Add delay before test
98
+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
99
+
72
100
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 ()));
76
105
});
77
106
}
78
107
79
108
@ Test
80
- public void testCreateGroupWithInvalidData () {
109
+ public void testCreateGroupWithInvalidData () throws InterruptedException {
110
+ // Add delay before test
111
+ Thread .sleep (DELAY_BETWEEN_OPERATIONS_MS );
112
+
81
113
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 (() ->
82
129
client .customers ()
83
130
.groups ()
84
131
.create (CreateCustomerGroupRequest .builder ()
85
132
.group (CustomerGroup .builder ()
86
- // Empty name should be invalid
87
- .name ("" )
133
+ .name ("Default-" + UUID .randomUUID ())
88
134
.build ())
89
135
.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
+
103
138
Optional <CustomerGroup > group = response .getGroup ();
104
139
if (!group .isPresent ()) {
105
140
throw new RuntimeException ("Failed to create test customer group." );
106
141
}
107
142
return group .get ().getId ().get ();
108
143
}
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 .statusCode () == 429 ) {
169
+ System .out .printf ("Rate limited on attempt %d%n" , attempt + 1 );
170
+ attempt ++;
171
+ continue ;
172
+ }
173
+
174
+ // For other API errors, throw immediately
175
+ throw e ;
176
+ } catch (InterruptedException e ) {
177
+ Thread .currentThread ().interrupt ();
178
+ throw new RuntimeException ("Interrupted during retry delay" , e );
179
+ }
180
+ }
181
+
182
+ // If we've exhausted all retries, throw the last exception
183
+ throw lastException ;
184
+ }
185
+ }
0 commit comments