1919import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
2020import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
2121import static com .github .tomakehurst .wiremock .client .WireMock .verify ;
22- import static org .junit .Assert .assertEquals ;
22+ import static org .junit .Assert .* ;
2323
2424import com .github .tomakehurst .wiremock .junit .WireMockRule ;
2525import com .google .common .io .Resources ;
3131import java .io .IOException ;
3232import java .nio .file .Files ;
3333import 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 ;
3538import org .junit .Before ;
3639import org .junit .Rule ;
3740import org .junit .Test ;
@@ -56,7 +59,7 @@ public void setup() throws IOException {
5659 }
5760
5861 @ Test
59- public void testPaginationForNamespaceListWithSuccess () throws IOException {
62+ public void testPaginationForNamespaceListWithSuccessThreadSafely () throws IOException {
6063 String namespaceListPage1Str = new String (Files .readAllBytes (Paths .get (LIST_PAGE1_FILE_PATH )));
6164 String namespaceListPage2Str = new String (Files .readAllBytes (Paths .get (LIST_PAGE2_FILE_PATH )));
6265 CoreV1Api api = new CoreV1Api (client );
@@ -79,6 +82,10 @@ public void testPaginationForNamespaceListWithSuccess() throws IOException {
7982 .withHeader ("Content-Type" , "application/json" )
8083 .withBody (namespaceListPage2Str )));
8184
85+ int threads = 10 ;
86+ CountDownLatch latch = new CountDownLatch (threads );
87+ ExecutorService service = Executors .newFixedThreadPool (threads );
88+
8289 Pager <V1Namespace , V1NamespaceList > pager =
8390 new Pager <V1Namespace , V1NamespaceList >(
8491 (Pager .PagerParams param ) -> {
@@ -103,15 +110,27 @@ public void testPaginationForNamespaceListWithSuccess() throws IOException {
103110 1 ,
104111 V1NamespaceList .class );
105112
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" );
110130 }
111- assertEquals (2 , size );
112131
113132 verify (
114- 2 ,
133+ 2 * threads ,
115134 getRequestedFor (urlPathEqualTo ("/api/v1/namespaces" ))
116135 .withQueryParam ("limit" , equalTo ("1" )));
117136 }
@@ -129,7 +148,7 @@ public void testPaginationForNamespaceListWithBadTokenFailure() throws IOExcepti
129148 .withStatus (400 )
130149 .withHeader ("Content-Type" , "application/json" )
131150 .withBody (status400Str )));
132- Pager pager =
151+ Pager < V1Namespace , V1NamespaceList > pager =
133152 new Pager <V1Namespace , V1NamespaceList >(
134153 (Pager .PagerParams param ) -> {
135154 try {
@@ -152,18 +171,16 @@ public void testPaginationForNamespaceListWithBadTokenFailure() throws IOExcepti
152171 client ,
153172 1 ,
154173 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 ++;
165179 }
180+ } catch (Exception e ) {
181+ assertEquals (status400Str , e .getMessage ());
166182 }
183+
167184 verify (
168185 getRequestedFor (urlPathEqualTo ("/api/v1/namespaces" ))
169186 .withQueryParam ("limit" , equalTo ("1" )));
@@ -183,7 +200,7 @@ public void testPaginationForNamespaceListWithFieldSelectorFailure() throws IOEx
183200 .withStatus (400 )
184201 .withHeader ("Content-Type" , "application/json" )
185202 .withBody (status400Str )));
186- Pager pager =
203+ Pager < V1Namespace , V1NamespaceList > pager =
187204 new Pager <V1Namespace , V1NamespaceList >(
188205 (Pager .PagerParams param ) -> {
189206 try {
@@ -206,18 +223,16 @@ public void testPaginationForNamespaceListWithFieldSelectorFailure() throws IOEx
206223 client ,
207224 1 ,
208225 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 ());
219231 }
232+ } catch (Exception e ) {
233+ assertEquals (status400Str , e .getMessage ());
220234 }
235+
221236 verify (
222237 getRequestedFor (urlPathEqualTo ("/api/v1/namespaces" ))
223238 .withQueryParam ("fieldSelector" , equalTo ("metadata.name=default" ))
0 commit comments