Skip to content

Commit a9e8bfe

Browse files
authored
Merge pull request #2012 from brendandburns/build-matrix
De-flake a couple tests.
2 parents 73ae39a + c9a7953 commit a9e8bfe

File tree

3 files changed

+90
-25
lines changed

3 files changed

+90
-25
lines changed

extended/src/test/java/io/kubernetes/client/extended/controller/builder/DefaultControllerBuilderTest.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.List;
3838
import java.util.concurrent.ExecutorService;
3939
import java.util.concurrent.Executors;
40+
import java.util.concurrent.Semaphore;
4041
import java.util.function.Function;
4142
import org.junit.After;
4243
import org.junit.Before;
@@ -54,16 +55,6 @@ public class DefaultControllerBuilderTest {
5455

5556
@Rule public WireMockRule wireMockRule = new WireMockRule(PORT);
5657

57-
private final int stepCooldownIntervalInMillis = 500;
58-
59-
private void cooldown() {
60-
try {
61-
Thread.sleep(stepCooldownIntervalInMillis);
62-
} catch (InterruptedException e) {
63-
e.printStackTrace();
64-
}
65-
}
66-
6758
@Before
6859
public void setUp() throws Exception {
6960
client = new ClientBuilder().setBasePath("http://localhost:" + PORT).build();
@@ -134,7 +125,7 @@ public Result reconcile(Request request) {
134125
}
135126

136127
@Test
137-
public void testBuildWatchEventNotificationShouldWork() {
128+
public void testBuildWatchEventNotificationShouldWork() throws InterruptedException {
138129
V1PodList podList =
139130
new V1PodList()
140131
.metadata(new V1ListMeta().resourceVersion("0"))
@@ -183,13 +174,17 @@ public void testBuildWatchEventNotificationShouldWork() {
183174
};
184175

185176
List<Request> controllerReceivingRequests = new ArrayList<>();
186-
Controller testController =
177+
final Semaphore latch = new Semaphore(1);
178+
latch.acquire();
179+
180+
final Controller testController =
187181
ControllerBuilder.defaultBuilder(informerFactory)
188182
.withReconciler(
189183
new Reconciler() {
190184
@Override
191185
public Result reconcile(Request request) {
192186
controllerReceivingRequests.add(request);
187+
latch.release();
193188
return new Result(false);
194189
}
195190
})
@@ -203,10 +198,10 @@ public Result reconcile(Request request) {
203198
controllerThead.submit(testController::run);
204199
informerFactory.startAllRegisteredInformers();
205200

206-
Request expectedRequest = new Request("hostname1/test-pod1");
207-
208-
cooldown();
201+
// Wait for the request to be processed.
202+
latch.acquire(1);
209203

204+
Request expectedRequest = new Request("hostname1/test-pod1");
210205
assertEquals(1, keyFuncReceivingRequests.size());
211206
assertEquals(expectedRequest, keyFuncReceivingRequests.get(0));
212207

extended/src/test/java/io/kubernetes/client/extended/workqueue/DefaultDelayingQueueTest.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,37 @@ public void testSimpleDelayingQueue() throws Exception {
5252

5353
@Test
5454
public void testDeduping() throws Exception {
55+
final Instant staticTime = Instant.now();
5556
DefaultDelayingQueue<String> queue = new DefaultDelayingQueue<>();
5657
String item = "foo";
5758

59+
// Hold time still
60+
queue.injectTimeSource(
61+
() -> {
62+
return staticTime;
63+
});
64+
5865
queue.addAfter(item, Duration.ofMillis(50));
5966
assertTrue(waitForWaitingQueueToFill(queue));
6067
queue.addAfter(item, Duration.ofMillis(70));
6168
assertTrue(waitForWaitingQueueToFill(queue));
6269
assertTrue("should not have added", queue.length() == 0);
6370

64-
// step past the first block, we should receive now
65-
Thread.sleep(60L);
71+
// Advance time
72+
queue.injectTimeSource(
73+
() -> {
74+
return staticTime.plusMillis(60);
75+
});
6676
assertTrue(waitForAdded(queue, 1));
6777
item = queue.get();
6878
queue.done(item);
6979

7080
// step past the second add
71-
Thread.sleep(20L);
81+
// Advance time
82+
queue.injectTimeSource(
83+
() -> {
84+
return staticTime.plusMillis(90);
85+
});
7286
assertTrue("should not have added", queue.length() == 0);
7387

7488
// test again, but this time the earlier should override
@@ -77,19 +91,33 @@ public void testDeduping() throws Exception {
7791
assertTrue(waitForWaitingQueueToFill(queue));
7892
assertTrue("should not have added", queue.length() == 0);
7993

80-
Thread.sleep(40L);
94+
// Advance time
95+
queue.injectTimeSource(
96+
() -> {
97+
return staticTime.plusMillis(150);
98+
});
8199
assertTrue(waitForAdded(queue, 1));
82100
item = queue.get();
83101
queue.done(item);
84102

85103
// step past the second add
86-
Thread.sleep(1L);
104+
// Advance time
105+
queue.injectTimeSource(
106+
() -> {
107+
return staticTime.plusMillis(190);
108+
});
87109
assertTrue("should not have added", queue.length() == 0);
88110
}
89111

90112
@Test
91113
public void testCopyShifting() throws Exception {
114+
final Instant staticTime = Instant.now();
92115
DefaultDelayingQueue<String> queue = new DefaultDelayingQueue<>();
116+
queue.injectTimeSource(
117+
() -> {
118+
return staticTime;
119+
});
120+
93121
final String first = "foo";
94122
final String second = "bar";
95123
final String third = "baz";
@@ -100,7 +128,10 @@ public void testCopyShifting() throws Exception {
100128
assertTrue(waitForWaitingQueueToFill(queue));
101129
assertTrue("should not have added", queue.length() == 0);
102130

103-
Thread.sleep(2000L);
131+
queue.injectTimeSource(
132+
() -> {
133+
return staticTime.plusMillis(2000);
134+
});
104135
assertTrue(waitForAdded(queue, 3));
105136
String actualFirst = queue.get();
106137
assertEquals(actualFirst, third);

spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesInformerCreatorTest.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
2020
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2121
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
22+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
2223
import static org.junit.Assert.assertEquals;
2324
import static org.junit.Assert.assertNotNull;
2425

26+
import com.github.tomakehurst.wiremock.core.Admin;
27+
import com.github.tomakehurst.wiremock.extension.Parameters;
28+
import com.github.tomakehurst.wiremock.extension.PostServeAction;
2529
import com.github.tomakehurst.wiremock.junit.WireMockRule;
30+
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
2631
import com.google.gson.Gson;
2732
import io.kubernetes.client.informer.SharedIndexInformer;
2833
import io.kubernetes.client.informer.SharedInformerFactory;
@@ -37,7 +42,8 @@
3742
import io.kubernetes.client.util.ClientBuilder;
3843
import io.kubernetes.client.util.generic.GenericKubernetesApi;
3944
import java.util.Arrays;
40-
import org.junit.Rule;
45+
import java.util.concurrent.Semaphore;
46+
import org.junit.ClassRule;
4147
import org.junit.Test;
4248
import org.junit.runner.RunWith;
4349
import org.springframework.beans.factory.annotation.Autowired;
@@ -50,14 +56,30 @@
5056
@SpringBootTest
5157
public class KubernetesInformerCreatorTest {
5258

53-
@Rule public WireMockRule wireMockRule = new WireMockRule(8188);
59+
public static class CountRequestAction extends PostServeAction {
60+
@Override
61+
public String getName() {
62+
return "semaphore";
63+
}
64+
65+
@Override
66+
public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
67+
Semaphore count = (Semaphore) parameters.get("semaphore");
68+
count.release();
69+
}
70+
}
71+
72+
@ClassRule
73+
public static WireMockRule wireMockRule =
74+
new WireMockRule(options().dynamicPort().extensions(new CountRequestAction()));
5475

5576
@SpringBootApplication
5677
static class App {
5778

5879
@Bean
5980
public ApiClient testingApiClient() {
60-
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8188).build();
81+
ApiClient apiClient =
82+
new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
6183
return apiClient;
6284
}
6385

@@ -91,6 +113,13 @@ public void testInformerInjection() throws InterruptedException {
91113
assertNotNull(podInformer);
92114
assertNotNull(configMapInformer);
93115

116+
Semaphore getCount = new Semaphore(2);
117+
Semaphore watchCount = new Semaphore(2);
118+
Parameters getParams = new Parameters();
119+
Parameters watchParams = new Parameters();
120+
getParams.put("semaphore", getCount);
121+
watchParams.put("semaphore", watchCount);
122+
94123
V1Pod foo1 =
95124
new V1Pod().kind("Pod").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
96125
V1ConfigMap bar1 =
@@ -100,6 +129,7 @@ public void testInformerInjection() throws InterruptedException {
100129

101130
wireMockRule.stubFor(
102131
get(urlMatching("^/api/v1/pods.*"))
132+
.withPostServeAction("semaphore", getParams)
103133
.withQueryParam("watch", equalTo("false"))
104134
.willReturn(
105135
aResponse()
@@ -112,11 +142,13 @@ public void testInformerInjection() throws InterruptedException {
112142
.items(Arrays.asList(foo1))))));
113143
wireMockRule.stubFor(
114144
get(urlMatching("^/api/v1/pods.*"))
145+
.withPostServeAction("semaphore", watchParams)
115146
.withQueryParam("watch", equalTo("true"))
116147
.willReturn(aResponse().withStatus(200).withBody("{}")));
117148

118149
wireMockRule.stubFor(
119150
get(urlMatching("^/api/v1/namespaces/default/configmaps.*"))
151+
.withPostServeAction("semaphore", getParams)
120152
.withQueryParam("watch", equalTo("false"))
121153
.willReturn(
122154
aResponse()
@@ -129,12 +161,19 @@ public void testInformerInjection() throws InterruptedException {
129161
.items(Arrays.asList(bar1))))));
130162
wireMockRule.stubFor(
131163
get(urlMatching("^/api/v1/namespaces/default/configmaps.*"))
164+
.withPostServeAction("semaphore", watchParams)
132165
.withQueryParam("watch", equalTo("true"))
133166
.willReturn(aResponse().withStatus(200).withBody("{}")));
134167

168+
// These will be released for each web call above.
169+
getCount.acquire(2);
170+
watchCount.acquire(2);
171+
135172
informerFactory.startAllRegisteredInformers();
136173

137-
Thread.sleep(200);
174+
// Wait for the GETs to complete and the watches to start.
175+
getCount.acquire(2);
176+
watchCount.acquire(2);
138177

139178
verify(
140179
1,

0 commit comments

Comments
 (0)