Skip to content
Merged
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 @@ -24,6 +24,7 @@
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 @@ -291,6 +292,7 @@ public void testLeaderElectionCaptureException() throws ApiException, Interrupte
() -> {
leaderElector.run(() -> {}, () -> {});
});
// TODO: Remove this sleep
Thread.sleep(Duration.ofSeconds(2).toMillis());
assertEquals(expectedException, actualException.get().getCause());
}
Expand All @@ -308,7 +310,18 @@ public void testLeaderElectionReportLeaderOnStart() throws ApiException, Interru
setLeaderTransitions(1);
setLeaseDurationSeconds(60);
}
})
.thenReturn(
new LeaderElectionRecord() {
{
setHolderIdentity("foo3");
setAcquireTime(new Date());
setRenewTime(new Date());
setLeaderTransitions(1);
setLeaseDurationSeconds(60);
}
});

List<String> notifications = new ArrayList<>();
LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig();
leaderElectionConfig.setLock(lock);
Expand All @@ -317,25 +330,21 @@ 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);
leaderElectionWorker.submit(
() -> {
leaderElector.run(() -> {}, () -> {}, (id) -> notifications.add(id));
leaderElector.run(
() -> {},
() -> {},
(id) -> {
notifications.add(id);
s.release();
});
});

Thread.sleep(Duration.ofSeconds(2).toMillis());

when(lock.get())
.thenReturn(
new LeaderElectionRecord() {
{
setHolderIdentity("foo3");
setAcquireTime(new Date());
setRenewTime(new Date());
setLeaderTransitions(1);
setLeaseDurationSeconds(60);
}
});
Thread.sleep(Duration.ofSeconds(2).toMillis());
// wait for two notifications to occur.
s.acquire(2);

assertEquals(2, notifications.size());
assertEquals("foo2", notifications.get(0));
Expand Down Expand Up @@ -365,12 +374,20 @@ 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();
leaderElectionWorker.submit(
() -> {
leaderElector.run(() -> {}, () -> {}, (id) -> notifications.add(id));
leaderElector.run(
() -> {},
() -> {},
(id) -> {
notifications.add(id);
s.release();
});
});

Thread.sleep(Duration.ofSeconds(2).toMillis());
s.acquire();
assertEquals(1, notifications.size());
assertEquals("foo1", notifications.get(0));
}
Expand Down