Skip to content

Commit a985167

Browse files
authored
Merge pull request #1786 from karthikkondapally/master
remove some of thread.sleeps in test cases
2 parents 98d0cbd + b8d8804 commit a985167

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

extended/src/test/java/io/kubernetes/client/extended/leaderelection/LeaderElectionTest.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
package io.kubernetes.client.extended.leaderelection;
1414

1515
import static java.util.concurrent.TimeUnit.SECONDS;
16-
import static org.junit.Assert.*;
17-
import static org.mockito.Mockito.*;
16+
import static org.junit.Assert.assertEquals;
17+
import static org.mockito.Mockito.when;
1818

1919
import io.kubernetes.client.openapi.ApiException;
2020
import java.net.HttpURLConnection;
@@ -25,7 +25,6 @@
2525
import java.util.concurrent.CountDownLatch;
2626
import java.util.concurrent.ExecutorService;
2727
import java.util.concurrent.Executors;
28-
import java.util.concurrent.Semaphore;
2928
import java.util.concurrent.atomic.AtomicReference;
3029
import java.util.concurrent.locks.ReentrantLock;
3130
import java.util.function.Consumer;
@@ -285,16 +284,22 @@ public void testLeaderElectionCaptureException() throws ApiException, Interrupte
285284
leaderElectionConfig.setLeaseDuration(Duration.ofMillis(1000));
286285
leaderElectionConfig.setRetryPeriod(Duration.ofMillis(200));
287286
leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700));
287+
final CountDownLatch cLatch = new CountDownLatch(1);
288288
LeaderElector leaderElector =
289-
new LeaderElector(leaderElectionConfig, (t) -> actualException.set(t));
289+
new LeaderElector(
290+
leaderElectionConfig,
291+
(t) -> {
292+
actualException.set(t);
293+
cLatch.countDown();
294+
});
290295

291296
ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1);
292297
leaderElectionWorker.submit(
293298
() -> {
294299
leaderElector.run(() -> {}, () -> {});
295300
});
296-
// TODO: Remove this sleep
297-
Thread.sleep(Duration.ofSeconds(2).toMillis());
301+
cLatch.await();
302+
298303
assertEquals(expectedException, actualException.get().getCause());
299304
}
300305

@@ -331,21 +336,20 @@ public void testLeaderElectionReportLeaderOnStart() throws ApiException, Interru
331336
leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700));
332337
LeaderElector leaderElector = new LeaderElector(leaderElectionConfig);
333338
ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1);
334-
final Semaphore s = new Semaphore(2);
335-
s.acquire(2);
339+
final CountDownLatch cLatch = new CountDownLatch(2);
336340
leaderElectionWorker.submit(
337341
() -> {
338342
leaderElector.run(
339343
() -> {},
340344
() -> {},
341345
(id) -> {
342346
notifications.add(id);
343-
s.release();
347+
cLatch.countDown();
344348
});
345349
});
346350

347351
// wait for two notifications to occur.
348-
s.acquire(2);
352+
cLatch.await();
349353

350354
assertEquals(2, notifications.size());
351355
assertEquals("foo2", notifications.get(0));
@@ -375,20 +379,19 @@ public void testLeaderElectionShouldReportLeaderItAcquiresOnStart()
375379
leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700));
376380
LeaderElector leaderElector = new LeaderElector(leaderElectionConfig);
377381
ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1);
378-
Semaphore s = new Semaphore(1);
379-
s.acquire();
382+
final CountDownLatch cLatch = new CountDownLatch(1);
380383
leaderElectionWorker.submit(
381384
() -> {
382385
leaderElector.run(
383386
() -> {},
384387
() -> {},
385388
(id) -> {
386389
notifications.add(id);
387-
s.release();
390+
cLatch.countDown();
388391
});
389392
});
390393

391-
s.acquire();
394+
cLatch.await();
392395
assertEquals(1, notifications.size());
393396
assertEquals("foo1", notifications.get(0));
394397
}

util/src/test/java/io/kubernetes/client/ExecTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.InputStream;
3535
import java.io.OutputStream;
3636
import java.nio.charset.StandardCharsets;
37+
import java.util.concurrent.CountDownLatch;
3738
import org.junit.Before;
3839
import org.junit.Rule;
3940
import org.junit.Test;
@@ -82,7 +83,8 @@ public static InputStream makeStream(byte[] prefix, byte[] data) {
8283
return new ByteArrayInputStream(out);
8384
}
8485

85-
public static Thread asyncCopy(final InputStream is, final OutputStream os) {
86+
public static Thread asyncCopy(
87+
final InputStream is, final OutputStream os, CountDownLatch cLatch) {
8688
Thread t =
8789
new Thread(
8890
new Runnable() {
@@ -91,6 +93,8 @@ public void run() {
9193
Streams.copy(is, os);
9294
} catch (IOException ex) {
9395
ex.printStackTrace();
96+
} finally {
97+
cLatch.countDown();
9498
}
9599
}
96100
});
@@ -110,14 +114,13 @@ public void testExecProcess() throws IOException, InterruptedException {
110114

111115
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
112116
final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
113-
114-
Thread t1 = asyncCopy(process.getInputStream(), stdout);
115-
Thread t2 = asyncCopy(process.getErrorStream(), stderr);
117+
CountDownLatch cLatch = new CountDownLatch(2);
118+
Thread t1 = asyncCopy(process.getInputStream(), stdout, cLatch);
119+
Thread t2 = asyncCopy(process.getErrorStream(), stderr, cLatch);
116120

117121
process.getHandler().bytesMessage(makeStream(3, OUTPUT_EXIT0.getBytes(StandardCharsets.UTF_8)));
118-
// TODO: Fix this asap!
119-
Thread.sleep(1000);
120122

123+
cLatch.await();
121124
process.destroy();
122125

123126
assertEquals(msgData, stdout.toString());

0 commit comments

Comments
 (0)