Skip to content

Commit f18dc7c

Browse files
authored
Merge pull request #1314 from brendandburns/iterator
Fix pager iterator and add test.
2 parents e5e6468 + 9835016 commit f18dc7c

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

extended/src/main/java/io/kubernetes/client/extended/pager/Pager.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public Pager(
5959
*/
6060
@Override
6161
public Iterator<ApiType> iterator() {
62-
return new PagerIterator();
62+
PagerIterator it = new PagerIterator();
63+
it.makeCall();
64+
return it;
6365
}
6466

6567
/** returns next list call by setting continue variable and limit */
@@ -117,37 +119,44 @@ private class PagerIterator implements Iterator<ApiType> {
117119
*/
118120
@Override
119121
public boolean hasNext() {
122+
if (listObjectCurrentPage.getItems() == null
123+
|| listObjectCurrentPage.getItems().size() == 0) {
124+
return false;
125+
}
120126
if (!started) {
121127
started = true;
122128
return Boolean.TRUE;
123129
}
124130
return !(continueToken == null && offsetCurrentPage >= currentPageSize);
125131
}
126132

127-
/**
128-
* returns next chunk of List. size of list depends on limit set in constructor.
129-
*
130-
* @return the next Object
131-
*/
132-
@Override
133-
public ApiType next() {
133+
protected void makeCall() {
134134
try {
135-
if (offsetCurrentPage >= currentPageSize) {
135+
call = getNextCall(limit, continueToken);
136136

137-
call = getNextCall(limit, continueToken);
137+
listObjectCurrentPage = executeRequest(call);
138+
continueToken = listObjectCurrentPage.getMetadata().getContinue();
138139

139-
listObjectCurrentPage = executeRequest(call);
140-
continueToken = listObjectCurrentPage.getMetadata().getContinue();
141-
142-
offsetCurrentPage = 0;
143-
currentPageSize = listObjectCurrentPage.getItems().size();
144-
}
145-
return (ApiType) listObjectCurrentPage.getItems().get(offsetCurrentPage++);
140+
offsetCurrentPage = 0;
141+
currentPageSize = listObjectCurrentPage.getItems().size();
146142
} catch (ApiException e) {
147143
throw new RuntimeException(e.getResponseBody());
148144
} catch (IOException e) {
149145
throw new RuntimeException(e);
150146
}
151147
}
148+
149+
/**
150+
* returns next chunk of List. size of list depends on limit set in constructor.
151+
*
152+
* @return the next Object
153+
*/
154+
@Override
155+
public ApiType next() {
156+
if (offsetCurrentPage >= currentPageSize) {
157+
makeCall();
158+
}
159+
return (ApiType) listObjectCurrentPage.getItems().get(offsetCurrentPage++);
160+
}
152161
}
153162
}

extended/src/test/java/io/kubernetes/client/extended/pager/PagerTest.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
2020
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2121
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
22-
import static org.junit.Assert.*;
22+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertFalse;
25+
import static org.junit.Assert.fail;
2326

2427
import com.github.tomakehurst.wiremock.junit.WireMockRule;
2528
import com.google.common.io.Resources;
@@ -31,6 +34,7 @@
3134
import java.io.IOException;
3235
import java.nio.file.Files;
3336
import java.nio.file.Paths;
37+
import java.util.Iterator;
3438
import java.util.concurrent.CountDownLatch;
3539
import java.util.concurrent.ExecutorService;
3640
import java.util.concurrent.Executors;
@@ -42,6 +46,8 @@
4246
public class PagerTest {
4347

4448
private ApiClient client;
49+
private static final String LIST_PAGE0_FILE_PATH =
50+
Resources.getResource("namespace-list-pager0.json").getPath();
4551
private static final String LIST_PAGE1_FILE_PATH =
4652
Resources.getResource("namespace-list-pager1.json").getPath();
4753
private static final String LIST_PAGE2_FILE_PATH =
@@ -50,12 +56,41 @@ public class PagerTest {
5056
Resources.getResource("status-400.json").getPath();
5157
private static final String STATUS_BAD_TOKEN_FILE_PATH =
5258
Resources.getResource("bad-token-status.json").getPath();
53-
private static final int PORT = 8087;
54-
@Rule public WireMockRule wireMockRule = new WireMockRule(PORT);
59+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
5560

5661
@Before
5762
public void setup() throws IOException {
58-
client = new ClientBuilder().setBasePath("http://localhost:" + PORT).build();
63+
client = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
64+
}
65+
66+
@Test
67+
public void testIteratorForEmptyList() throws IOException {
68+
String namespaceListPage0Str = new String(Files.readAllBytes(Paths.get(LIST_PAGE0_FILE_PATH)));
69+
CoreV1Api api = new CoreV1Api(client);
70+
71+
stubFor(
72+
get(urlPathEqualTo("/api/v1/namespaces"))
73+
.willReturn(
74+
aResponse()
75+
.withStatus(200)
76+
.withHeader("Content-Type", "application/json")
77+
.withBody(namespaceListPage0Str)));
78+
79+
Pager<V1Namespace, V1NamespaceList> pager =
80+
new Pager<V1Namespace, V1NamespaceList>(
81+
(Pager.PagerParams param) -> {
82+
try {
83+
return api.listNamespaceCall(
84+
null, null, null, null, null, null, null, null, null, null);
85+
} catch (Exception e) {
86+
throw new RuntimeException(e);
87+
}
88+
},
89+
client,
90+
1,
91+
V1NamespaceList.class);
92+
Iterator<V1Namespace> it = pager.iterator();
93+
assertFalse("Iterator should be empty.", it.hasNext());
5994
}
6095

6196
@Test
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"kind": "NamespaceList",
3+
"apiVersion": "v1",
4+
"metadata": {
5+
"selfLink": "/api/v1/namespaces",
6+
"resourceVersion": "61027"
7+
},
8+
"items": []
9+
}

0 commit comments

Comments
 (0)