19
19
import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
20
20
import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
21
21
import static com .github .tomakehurst .wiremock .client .WireMock .verify ;
22
- import static org .junit .Assert .assertEquals ;
22
+ import static org .junit .Assert .* ;
23
23
24
24
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
25
25
import com .google .common .io .Resources ;
31
31
import java .io .IOException ;
32
32
import java .nio .file .Files ;
33
33
import java .nio .file .Paths ;
34
- import java .util .List ;
34
+ import java .util .concurrent .CountDownLatch ;
35
+ import java .util .concurrent .ExecutorService ;
36
+ import java .util .concurrent .Executors ;
37
+ import java .util .concurrent .TimeUnit ;
35
38
import org .junit .Before ;
36
39
import org .junit .Rule ;
37
40
import org .junit .Test ;
@@ -56,7 +59,7 @@ public void setup() throws IOException {
56
59
}
57
60
58
61
@ Test
59
- public void testPaginationForNamespaceListWithSuccess () throws IOException {
62
+ public void testPaginationForNamespaceListWithSuccessThreadSafely () throws IOException {
60
63
String namespaceListPage1Str = new String (Files .readAllBytes (Paths .get (LIST_PAGE1_FILE_PATH )));
61
64
String namespaceListPage2Str = new String (Files .readAllBytes (Paths .get (LIST_PAGE2_FILE_PATH )));
62
65
CoreV1Api api = new CoreV1Api (client );
@@ -79,6 +82,10 @@ public void testPaginationForNamespaceListWithSuccess() throws IOException {
79
82
.withHeader ("Content-Type" , "application/json" )
80
83
.withBody (namespaceListPage2Str )));
81
84
85
+ int threads = 10 ;
86
+ CountDownLatch latch = new CountDownLatch (threads );
87
+ ExecutorService service = Executors .newFixedThreadPool (threads );
88
+
82
89
Pager <V1Namespace , V1NamespaceList > pager =
83
90
new Pager <V1Namespace , V1NamespaceList >(
84
91
(Pager .PagerParams param ) -> {
@@ -103,15 +110,27 @@ public void testPaginationForNamespaceListWithSuccess() throws IOException {
103
110
1 ,
104
111
V1NamespaceList .class );
105
112
106
- int size = 0 ;
107
- for (V1Namespace namespace : pager ) {
108
- assertEquals ("default" , namespace .getMetadata ().getName ());
109
- size ++;
113
+ for (int i = 0 ; i < threads ; i ++) {
114
+ service .submit (
115
+ () -> {
116
+ int size = 0 ;
117
+ for (V1Namespace namespace : pager ) {
118
+ assertEquals ("default" , namespace .getMetadata ().getName ());
119
+ size ++;
120
+ }
121
+ assertEquals (2 , size );
122
+ latch .countDown ();
123
+ });
124
+ }
125
+
126
+ try {
127
+ latch .await (5 , TimeUnit .SECONDS );
128
+ } catch (InterruptedException e ) {
129
+ fail ("timed out waiting for pager finished" );
110
130
}
111
- assertEquals (2 , size );
112
131
113
132
verify (
114
- 2 ,
133
+ 2 * threads ,
115
134
getRequestedFor (urlPathEqualTo ("/api/v1/namespaces" ))
116
135
.withQueryParam ("limit" , equalTo ("1" )));
117
136
}
@@ -129,7 +148,7 @@ public void testPaginationForNamespaceListWithBadTokenFailure() throws IOExcepti
129
148
.withStatus (400 )
130
149
.withHeader ("Content-Type" , "application/json" )
131
150
.withBody (status400Str )));
132
- Pager pager =
151
+ Pager < V1Namespace , V1NamespaceList > pager =
133
152
new Pager <V1Namespace , V1NamespaceList >(
134
153
(Pager .PagerParams param ) -> {
135
154
try {
@@ -152,18 +171,16 @@ public void testPaginationForNamespaceListWithBadTokenFailure() throws IOExcepti
152
171
client ,
153
172
1 ,
154
173
V1NamespaceList .class );
155
- while (pager .hasNext ()) {
156
- try {
157
- V1NamespaceList list = (V1NamespaceList ) pager .next ();
158
- List <V1Namespace > items = list .getItems ();
159
- assertEquals (1 , items .size ());
160
- for (V1Namespace namespace : items ) {
161
- assertEquals ("default" , namespace .getMetadata ().getName ());
162
- }
163
- } catch (Exception e ) {
164
- assertEquals (status400Str , e .getMessage ());
174
+ int count = 0 ;
175
+ try {
176
+ for (V1Namespace namespace : pager ) {
177
+ assertEquals ("default" , namespace .getMetadata ().getName ());
178
+ count ++;
165
179
}
180
+ } catch (Exception e ) {
181
+ assertEquals (status400Str , e .getMessage ());
166
182
}
183
+
167
184
verify (
168
185
getRequestedFor (urlPathEqualTo ("/api/v1/namespaces" ))
169
186
.withQueryParam ("limit" , equalTo ("1" )));
@@ -183,7 +200,7 @@ public void testPaginationForNamespaceListWithFieldSelectorFailure() throws IOEx
183
200
.withStatus (400 )
184
201
.withHeader ("Content-Type" , "application/json" )
185
202
.withBody (status400Str )));
186
- Pager pager =
203
+ Pager < V1Namespace , V1NamespaceList > pager =
187
204
new Pager <V1Namespace , V1NamespaceList >(
188
205
(Pager .PagerParams param ) -> {
189
206
try {
@@ -206,18 +223,16 @@ public void testPaginationForNamespaceListWithFieldSelectorFailure() throws IOEx
206
223
client ,
207
224
1 ,
208
225
V1NamespaceList .class );
209
- while (pager .hasNext ()) {
210
- try {
211
- V1NamespaceList list = (V1NamespaceList ) pager .next ();
212
- List <V1Namespace > items = list .getItems ();
213
- assertEquals (1 , items .size ());
214
- for (V1Namespace namespace : items ) {
215
- assertEquals ("default" , namespace .getMetadata ().getName ());
216
- }
217
- } catch (Exception e ) {
218
- assertEquals (status400Str , e .getMessage ());
226
+ int count = 0 ;
227
+ try {
228
+ for (V1Namespace namespace : pager ) {
229
+ count ++;
230
+ assertEquals ("default" , namespace .getMetadata ().getName ());
219
231
}
232
+ } catch (Exception e ) {
233
+ assertEquals (status400Str , e .getMessage ());
220
234
}
235
+
221
236
verify (
222
237
getRequestedFor (urlPathEqualTo ("/api/v1/namespaces" ))
223
238
.withQueryParam ("fieldSelector" , equalTo ("metadata.name=default" ))
0 commit comments