|
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,25 +284,21 @@ 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)); |
288 | | - final Semaphore sem = new Semaphore(1); |
| 287 | + final CountDownLatch cLatch = new CountDownLatch(1); |
289 | 288 | LeaderElector leaderElector = |
290 | 289 | new LeaderElector( |
291 | 290 | leaderElectionConfig, |
292 | 291 | (t) -> { |
293 | 292 | actualException.set(t); |
294 | | - sem.release(); |
| 293 | + cLatch.countDown(); |
295 | 294 | }); |
296 | 295 |
|
297 | 296 | ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1); |
298 | | - sem.acquire(); |
299 | 297 | leaderElectionWorker.submit( |
300 | 298 | () -> { |
301 | 299 | leaderElector.run(() -> {}, () -> {}); |
302 | 300 | }); |
303 | | - while (!sem.tryAcquire()) { |
304 | | - System.out.println( |
305 | | - "waiting for leaderElectionWorker to throw exception in LeaderElectionTest::testLeaderElectionCaptureException"); |
306 | | - } |
| 301 | + cLatch.await(); |
307 | 302 |
|
308 | 303 | assertEquals(expectedException, actualException.get().getCause()); |
309 | 304 | } |
@@ -341,21 +336,20 @@ public void testLeaderElectionReportLeaderOnStart() throws ApiException, Interru |
341 | 336 | leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700)); |
342 | 337 | LeaderElector leaderElector = new LeaderElector(leaderElectionConfig); |
343 | 338 | ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1); |
344 | | - final Semaphore s = new Semaphore(2); |
345 | | - s.acquire(2); |
| 339 | + final CountDownLatch cLatch = new CountDownLatch(2); |
346 | 340 | leaderElectionWorker.submit( |
347 | 341 | () -> { |
348 | 342 | leaderElector.run( |
349 | 343 | () -> {}, |
350 | 344 | () -> {}, |
351 | 345 | (id) -> { |
352 | 346 | notifications.add(id); |
353 | | - s.release(); |
| 347 | + cLatch.countDown(); |
354 | 348 | }); |
355 | 349 | }); |
356 | 350 |
|
357 | 351 | // wait for two notifications to occur. |
358 | | - s.acquire(2); |
| 352 | + cLatch.await(); |
359 | 353 |
|
360 | 354 | assertEquals(2, notifications.size()); |
361 | 355 | assertEquals("foo2", notifications.get(0)); |
@@ -385,20 +379,19 @@ public void testLeaderElectionShouldReportLeaderItAcquiresOnStart() |
385 | 379 | leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700)); |
386 | 380 | LeaderElector leaderElector = new LeaderElector(leaderElectionConfig); |
387 | 381 | ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1); |
388 | | - Semaphore s = new Semaphore(1); |
389 | | - s.acquire(); |
| 382 | + final CountDownLatch cLatch = new CountDownLatch(1); |
390 | 383 | leaderElectionWorker.submit( |
391 | 384 | () -> { |
392 | 385 | leaderElector.run( |
393 | 386 | () -> {}, |
394 | 387 | () -> {}, |
395 | 388 | (id) -> { |
396 | 389 | notifications.add(id); |
397 | | - s.release(); |
| 390 | + cLatch.countDown(); |
398 | 391 | }); |
399 | 392 | }); |
400 | 393 |
|
401 | | - s.acquire(); |
| 394 | + cLatch.await(); |
402 | 395 | assertEquals(1, notifications.size()); |
403 | 396 | assertEquals("foo1", notifications.get(0)); |
404 | 397 | } |
|
0 commit comments