|
7 | 7 | import io.kubernetes.client.openapi.ApiException;
|
8 | 8 | import java.time.Duration;
|
9 | 9 | import java.util.ArrayList;
|
| 10 | +import java.util.Date; |
10 | 11 | import java.util.List;
|
11 | 12 | import java.util.concurrent.CountDownLatch;
|
12 | 13 | import java.util.concurrent.ExecutorService;
|
@@ -282,6 +283,86 @@ public void testLeaderElectionCaptureException() throws ApiException, Interrupte
|
282 | 283 | assertEquals(expectedException, actualException.get().getCause());
|
283 | 284 | }
|
284 | 285 |
|
| 286 | + @Test |
| 287 | + public void testLeaderElectionReportLeaderOnStart() throws ApiException, InterruptedException { |
| 288 | + when(lock.identity()).thenReturn("foo1"); |
| 289 | + when(lock.get()) |
| 290 | + .thenReturn( |
| 291 | + new LeaderElectionRecord() { |
| 292 | + { |
| 293 | + setHolderIdentity("foo2"); |
| 294 | + setAcquireTime(new Date()); |
| 295 | + setRenewTime(new Date()); |
| 296 | + setLeaderTransitions(1); |
| 297 | + setLeaseDurationSeconds(60); |
| 298 | + } |
| 299 | + }); |
| 300 | + List<String> notifications = new ArrayList<>(); |
| 301 | + LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig(); |
| 302 | + leaderElectionConfig.setLock(lock); |
| 303 | + leaderElectionConfig.setLeaseDuration(Duration.ofMillis(1000)); |
| 304 | + leaderElectionConfig.setRetryPeriod(Duration.ofMillis(200)); |
| 305 | + leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700)); |
| 306 | + LeaderElector leaderElector = new LeaderElector(leaderElectionConfig); |
| 307 | + ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1); |
| 308 | + leaderElectionWorker.submit( |
| 309 | + () -> { |
| 310 | + leaderElector.run(() -> {}, () -> {}, (id) -> notifications.add(id)); |
| 311 | + }); |
| 312 | + |
| 313 | + Thread.sleep(Duration.ofSeconds(2).toMillis()); |
| 314 | + |
| 315 | + when(lock.get()) |
| 316 | + .thenReturn( |
| 317 | + new LeaderElectionRecord() { |
| 318 | + { |
| 319 | + setHolderIdentity("foo3"); |
| 320 | + setAcquireTime(new Date()); |
| 321 | + setRenewTime(new Date()); |
| 322 | + setLeaderTransitions(1); |
| 323 | + setLeaseDurationSeconds(60); |
| 324 | + } |
| 325 | + }); |
| 326 | + Thread.sleep(Duration.ofSeconds(2).toMillis()); |
| 327 | + |
| 328 | + assertEquals(2, notifications.size()); |
| 329 | + assertEquals("foo2", notifications.get(0)); |
| 330 | + assertEquals("foo3", notifications.get(1)); |
| 331 | + } |
| 332 | + |
| 333 | + @Test |
| 334 | + public void testLeaderElectionShouldReportLeaderItAcquiresOnStart() |
| 335 | + throws ApiException, InterruptedException { |
| 336 | + when(lock.identity()).thenReturn("foo1"); |
| 337 | + when(lock.get()) |
| 338 | + .thenReturn( |
| 339 | + new LeaderElectionRecord() { |
| 340 | + { |
| 341 | + setHolderIdentity("foo1"); |
| 342 | + setAcquireTime(new Date()); |
| 343 | + setRenewTime(new Date()); |
| 344 | + setLeaderTransitions(1); |
| 345 | + setLeaseDurationSeconds(60); |
| 346 | + } |
| 347 | + }); |
| 348 | + List<String> notifications = new ArrayList<>(); |
| 349 | + LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig(); |
| 350 | + leaderElectionConfig.setLock(lock); |
| 351 | + leaderElectionConfig.setLeaseDuration(Duration.ofMillis(1000)); |
| 352 | + leaderElectionConfig.setRetryPeriod(Duration.ofMillis(200)); |
| 353 | + leaderElectionConfig.setRenewDeadline(Duration.ofMillis(700)); |
| 354 | + LeaderElector leaderElector = new LeaderElector(leaderElectionConfig); |
| 355 | + ExecutorService leaderElectionWorker = Executors.newFixedThreadPool(1); |
| 356 | + leaderElectionWorker.submit( |
| 357 | + () -> { |
| 358 | + leaderElector.run(() -> {}, () -> {}, (id) -> notifications.add(id)); |
| 359 | + }); |
| 360 | + |
| 361 | + Thread.sleep(Duration.ofSeconds(2).toMillis()); |
| 362 | + assertEquals(1, notifications.size()); |
| 363 | + assertEquals("foo1", notifications.get(0)); |
| 364 | + } |
| 365 | + |
285 | 366 | public static class MockResourceLock implements Lock {
|
286 | 367 |
|
287 | 368 | public static ReentrantLock lock;
|
|
0 commit comments