|
13 | 13 | package io.kubernetes.client.extended.leaderelection;
|
14 | 14 |
|
15 | 15 | 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; |
18 | 18 |
|
19 | 19 | import io.kubernetes.client.openapi.ApiException;
|
20 | 20 | import java.net.HttpURLConnection;
|
|
25 | 25 | import java.util.concurrent.CountDownLatch;
|
26 | 26 | import java.util.concurrent.ExecutorService;
|
27 | 27 | import java.util.concurrent.Executors;
|
28 |
| -import java.util.concurrent.Semaphore; |
29 | 28 | import java.util.concurrent.atomic.AtomicReference;
|
30 | 29 | import java.util.concurrent.locks.ReentrantLock;
|
31 | 30 | import java.util.function.Consumer;
|
@@ -285,16 +284,22 @@ public void testLeaderElectionCaptureException() throws ApiException, Interrupte
|
285 | 284 | leaderElectionConfig.setLeaseDuration(Duration.ofMillis(1000));
|
286 | 285 | leaderElectionConfig.setRetryPeriod(Duration.ofMillis(200));
|
287 | 286 | leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700));
|
| 287 | + final CountDownLatch cLatch = new CountDownLatch(1); |
288 | 288 | 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 | + }); |
290 | 295 |
|
291 | 296 | ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1);
|
292 | 297 | leaderElectionWorker.submit(
|
293 | 298 | () -> {
|
294 | 299 | leaderElector.run(() -> {}, () -> {});
|
295 | 300 | });
|
296 |
| - // TODO: Remove this sleep |
297 |
| - Thread.sleep(Duration.ofSeconds(2).toMillis()); |
| 301 | + cLatch.await(); |
| 302 | + |
298 | 303 | assertEquals(expectedException, actualException.get().getCause());
|
299 | 304 | }
|
300 | 305 |
|
@@ -331,21 +336,20 @@ public void testLeaderElectionReportLeaderOnStart() throws ApiException, Interru
|
331 | 336 | leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700));
|
332 | 337 | LeaderElector leaderElector = new LeaderElector(leaderElectionConfig);
|
333 | 338 | ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1);
|
334 |
| - final Semaphore s = new Semaphore(2); |
335 |
| - s.acquire(2); |
| 339 | + final CountDownLatch cLatch = new CountDownLatch(2); |
336 | 340 | leaderElectionWorker.submit(
|
337 | 341 | () -> {
|
338 | 342 | leaderElector.run(
|
339 | 343 | () -> {},
|
340 | 344 | () -> {},
|
341 | 345 | (id) -> {
|
342 | 346 | notifications.add(id);
|
343 |
| - s.release(); |
| 347 | + cLatch.countDown(); |
344 | 348 | });
|
345 | 349 | });
|
346 | 350 |
|
347 | 351 | // wait for two notifications to occur.
|
348 |
| - s.acquire(2); |
| 352 | + cLatch.await(); |
349 | 353 |
|
350 | 354 | assertEquals(2, notifications.size());
|
351 | 355 | assertEquals("foo2", notifications.get(0));
|
@@ -375,20 +379,19 @@ public void testLeaderElectionShouldReportLeaderItAcquiresOnStart()
|
375 | 379 | leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700));
|
376 | 380 | LeaderElector leaderElector = new LeaderElector(leaderElectionConfig);
|
377 | 381 | ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1);
|
378 |
| - Semaphore s = new Semaphore(1); |
379 |
| - s.acquire(); |
| 382 | + final CountDownLatch cLatch = new CountDownLatch(1); |
380 | 383 | leaderElectionWorker.submit(
|
381 | 384 | () -> {
|
382 | 385 | leaderElector.run(
|
383 | 386 | () -> {},
|
384 | 387 | () -> {},
|
385 | 388 | (id) -> {
|
386 | 389 | notifications.add(id);
|
387 |
| - s.release(); |
| 390 | + cLatch.countDown(); |
388 | 391 | });
|
389 | 392 | });
|
390 | 393 |
|
391 |
| - s.acquire(); |
| 394 | + cLatch.await(); |
392 | 395 | assertEquals(1, notifications.size());
|
393 | 396 | assertEquals("foo1", notifications.get(0));
|
394 | 397 | }
|
|
0 commit comments