Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -331,21 +336,20 @@ 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(
() -> {},
() -> {},
(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));
Expand Down Expand Up @@ -375,20 +379,19 @@ 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(
() -> {},
() -> {},
(id) -> {
notifications.add(id);
s.release();
cLatch.countDown();
});
});

s.acquire();
cLatch.await();
assertEquals(1, notifications.size());
assertEquals("foo1", notifications.get(0));
}
Expand Down
15 changes: 9 additions & 6 deletions util/src/test/java/io/kubernetes/client/ExecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -91,6 +93,8 @@ public void run() {
Streams.copy(is, os);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
cLatch.countDown();
}
}
});
Expand All @@ -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());
Expand Down