diff --git a/extended/src/test/java/io/kubernetes/client/extended/leaderelection/LeaderElectionTest.java b/extended/src/test/java/io/kubernetes/client/extended/leaderelection/LeaderElectionTest.java index 7c42684163..ca5c302eba 100644 --- a/extended/src/test/java/io/kubernetes/client/extended/leaderelection/LeaderElectionTest.java +++ b/extended/src/test/java/io/kubernetes/client/extended/leaderelection/LeaderElectionTest.java @@ -13,8 +13,8 @@ package io.kubernetes.client.extended.leaderelection; import static java.util.concurrent.TimeUnit.SECONDS; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; import io.kubernetes.client.openapi.ApiException; import java.net.HttpURLConnection; @@ -25,7 +25,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; @@ -285,16 +284,22 @@ public void testLeaderElectionCaptureException() throws ApiException, Interrupte leaderElectionConfig.setLeaseDuration(Duration.ofMillis(1000)); leaderElectionConfig.setRetryPeriod(Duration.ofMillis(200)); leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700)); + final CountDownLatch cLatch = new CountDownLatch(1); LeaderElector leaderElector = - new LeaderElector(leaderElectionConfig, (t) -> actualException.set(t)); + new LeaderElector( + leaderElectionConfig, + (t) -> { + actualException.set(t); + cLatch.countDown(); + }); ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1); leaderElectionWorker.submit( () -> { leaderElector.run(() -> {}, () -> {}); }); - // TODO: Remove this sleep - Thread.sleep(Duration.ofSeconds(2).toMillis()); + cLatch.await(); + assertEquals(expectedException, actualException.get().getCause()); } @@ -331,8 +336,7 @@ public void testLeaderElectionReportLeaderOnStart() throws ApiException, Interru leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700)); LeaderElector leaderElector = new LeaderElector(leaderElectionConfig); ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1); - final Semaphore s = new Semaphore(2); - s.acquire(2); + final CountDownLatch cLatch = new CountDownLatch(2); leaderElectionWorker.submit( () -> { leaderElector.run( @@ -340,12 +344,12 @@ public void testLeaderElectionReportLeaderOnStart() throws ApiException, Interru () -> {}, (id) -> { notifications.add(id); - s.release(); + cLatch.countDown(); }); }); // wait for two notifications to occur. - s.acquire(2); + cLatch.await(); assertEquals(2, notifications.size()); assertEquals("foo2", notifications.get(0)); @@ -375,8 +379,7 @@ public void testLeaderElectionShouldReportLeaderItAcquiresOnStart() leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700)); LeaderElector leaderElector = new LeaderElector(leaderElectionConfig); ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1); - Semaphore s = new Semaphore(1); - s.acquire(); + final CountDownLatch cLatch = new CountDownLatch(1); leaderElectionWorker.submit( () -> { leaderElector.run( @@ -384,11 +387,11 @@ public void testLeaderElectionShouldReportLeaderItAcquiresOnStart() () -> {}, (id) -> { notifications.add(id); - s.release(); + cLatch.countDown(); }); }); - s.acquire(); + cLatch.await(); assertEquals(1, notifications.size()); assertEquals("foo1", notifications.get(0)); } diff --git a/util/src/test/java/io/kubernetes/client/ExecTest.java b/util/src/test/java/io/kubernetes/client/ExecTest.java index 1fcbabda72..8075e9de51 100644 --- a/util/src/test/java/io/kubernetes/client/ExecTest.java +++ b/util/src/test/java/io/kubernetes/client/ExecTest.java @@ -34,6 +34,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.util.concurrent.CountDownLatch; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -82,7 +83,8 @@ public static InputStream makeStream(byte[] prefix, byte[] data) { return new ByteArrayInputStream(out); } - public static Thread asyncCopy(final InputStream is, final OutputStream os) { + public static Thread asyncCopy( + final InputStream is, final OutputStream os, CountDownLatch cLatch) { Thread t = new Thread( new Runnable() { @@ -91,6 +93,8 @@ public void run() { Streams.copy(is, os); } catch (IOException ex) { ex.printStackTrace(); + } finally { + cLatch.countDown(); } } }); @@ -110,14 +114,13 @@ public void testExecProcess() throws IOException, InterruptedException { final ByteArrayOutputStream stdout = new ByteArrayOutputStream(); final ByteArrayOutputStream stderr = new ByteArrayOutputStream(); - - Thread t1 = asyncCopy(process.getInputStream(), stdout); - Thread t2 = asyncCopy(process.getErrorStream(), stderr); + CountDownLatch cLatch = new CountDownLatch(2); + Thread t1 = asyncCopy(process.getInputStream(), stdout, cLatch); + Thread t2 = asyncCopy(process.getErrorStream(), stderr, cLatch); process.getHandler().bytesMessage(makeStream(3, OUTPUT_EXIT0.getBytes(StandardCharsets.UTF_8))); - // TODO: Fix this asap! - Thread.sleep(1000); + cLatch.await(); process.destroy(); assertEquals(msgData, stdout.toString());