Skip to content

Commit c838490

Browse files
committed
improving test coverage using wiremock
1 parent 29ed192 commit c838490

File tree

3 files changed

+412
-20
lines changed

3 files changed

+412
-20
lines changed

util/src/test/java/io/kubernetes/client/informer/cache/ControllerTest.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
import static org.junit.Assert.*;
44

5-
import io.kubernetes.client.ApiException;
65
import io.kubernetes.client.informer.EventType;
76
import io.kubernetes.client.informer.ListerWatcher;
87
import io.kubernetes.client.models.V1ListMeta;
98
import io.kubernetes.client.models.V1ObjectMeta;
109
import io.kubernetes.client.models.V1Pod;
1110
import io.kubernetes.client.models.V1PodList;
12-
import io.kubernetes.client.util.CallGeneratorParams;
1311
import io.kubernetes.client.util.Watch;
14-
import io.kubernetes.client.util.Watchable;
1512
import java.util.Arrays;
1613
import java.util.concurrent.atomic.AtomicBoolean;
1714
import java.util.concurrent.atomic.AtomicInteger;
@@ -37,24 +34,10 @@ public void testControllerProcessDeltas() throws InterruptedException {
3734
new DeltaFIFO<>(Cache::deletionHandlingMetaNamespaceKeyFunc, new Cache());
3835

3936
AtomicBoolean runOnce = new AtomicBoolean(false);
40-
ListerWatcher<V1Pod, V1PodList> listerWatcher =
41-
new ListerWatcher<V1Pod, V1PodList>() {
42-
@Override
43-
public V1PodList list(CallGeneratorParams params) throws ApiException {
44-
return podList;
45-
}
4637

47-
@Override
48-
public Watchable<V1Pod> watch(CallGeneratorParams params) throws ApiException {
49-
if (!runOnce.get()) {
50-
runOnce.set(true);
51-
return new MockWatch<V1Pod>(
52-
new Watch.Response<V1Pod>(EventType.MODIFIED.name(), foo3));
53-
} else {
54-
return new MockWatch<V1Pod>();
55-
}
56-
}
57-
};
38+
ListerWatcher<V1Pod, V1PodList> listerWatcher =
39+
new MockRunOnceListerWatcher<V1Pod, V1PodList>(
40+
podList, new Watch.Response<V1Pod>(EventType.MODIFIED.name(), foo3));
5841

5942
Controller<V1Pod, V1PodList> controller =
6043
new Controller<>(
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.kubernetes.client.informer.cache;
2+
3+
import io.kubernetes.client.ApiException;
4+
import io.kubernetes.client.informer.ListerWatcher;
5+
import io.kubernetes.client.util.CallGeneratorParams;
6+
import io.kubernetes.client.util.Watch;
7+
import io.kubernetes.client.util.Watchable;
8+
9+
public class MockRunOnceListerWatcher<ApiType, ApiListType>
10+
implements ListerWatcher<ApiType, ApiListType> {
11+
12+
private ApiListType list;
13+
private Watch.Response<ApiType>[] events;
14+
15+
private boolean listExecuted = false;
16+
private boolean watchExecuted = false;
17+
18+
public MockRunOnceListerWatcher(ApiListType list, Watch.Response<ApiType>... events) {
19+
this.list = list;
20+
this.events = events;
21+
}
22+
23+
@Override
24+
public ApiListType list(CallGeneratorParams params) throws ApiException {
25+
if (!listExecuted) {
26+
listExecuted = true;
27+
return list;
28+
}
29+
try {
30+
Thread.sleep(100000);
31+
} catch (InterruptedException e) {
32+
}
33+
return null;
34+
}
35+
36+
@Override
37+
public Watchable<ApiType> watch(CallGeneratorParams params) throws ApiException {
38+
if (!watchExecuted) {
39+
watchExecuted = true;
40+
return new MockWatch<>(events);
41+
}
42+
try {
43+
Thread.sleep(100000);
44+
} catch (InterruptedException e) {
45+
}
46+
return new MockWatch<>();
47+
}
48+
}

0 commit comments

Comments
 (0)