|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package io.kubernetes.client.extended.leaderelection; |
| 15 | + |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | + |
| 22 | +/** |
| 23 | + * Leader Election tests using "simulated" locks created by {@link LockSmith} |
| 24 | + */ |
| 25 | +public class LeaderElectorTest |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Tests that when a leader candidate is stopped gracefully, second candidate immediately becomes |
| 29 | + * leader. |
| 30 | + */ |
| 31 | + @Test(timeout = 20000L) |
| 32 | + public void testLeaderGracefulShutdown() throws Exception |
| 33 | + { |
| 34 | + LockSmith lockSmith = new LockSmith(); |
| 35 | + |
| 36 | + CountDownLatch startBeingLeader1 = new CountDownLatch(1); |
| 37 | + CountDownLatch stopBeingLeader1 = new CountDownLatch(1); |
| 38 | + |
| 39 | + LeaderElector leaderElector1 = makeAndRunLeaderElectorAsync(lockSmith, "candidate1", startBeingLeader1, stopBeingLeader1); |
| 40 | + |
| 41 | + // wait for candidate1 to become leader |
| 42 | + startBeingLeader1.await(); |
| 43 | + |
| 44 | + CountDownLatch startBeingLeader2 = new CountDownLatch(1); |
| 45 | + CountDownLatch stopBeingLeader2 = new CountDownLatch(1); |
| 46 | + |
| 47 | + LeaderElector leaderElector2 = makeAndRunLeaderElectorAsync(lockSmith, "candidate2", startBeingLeader2, stopBeingLeader2); |
| 48 | + |
| 49 | + leaderElector1.close(); |
| 50 | + |
| 51 | + // ensure stopBeingLeader hook is called |
| 52 | + stopBeingLeader1.await(); |
| 53 | + |
| 54 | + // wait for candidate2 to become leader |
| 55 | + startBeingLeader2.await(); |
| 56 | + |
| 57 | + leaderElector2.close(); |
| 58 | + } |
| 59 | + |
| 60 | + private LeaderElector makeAndRunLeaderElectorAsync(LockSmith lockSmith, String lockIdentity, CountDownLatch startBeingLeader, CountDownLatch stopBeingLeader) |
| 61 | + { |
| 62 | + LeaderElectionConfig leaderElectionConfig = |
| 63 | + new LeaderElectionConfig( |
| 64 | + lockSmith.makeLock(lockIdentity), |
| 65 | + Duration.ofMillis(TimeUnit.MINUTES.toMillis(1)), |
| 66 | + Duration.ofMillis(TimeUnit.SECONDS.toMillis(51)), |
| 67 | + Duration.ofMillis(TimeUnit.SECONDS.toMillis(3)) |
| 68 | + ); |
| 69 | + LeaderElector leaderElector = new LeaderElector(leaderElectionConfig); |
| 70 | + |
| 71 | + Thread thread = new Thread( |
| 72 | + () -> leaderElector.run( |
| 73 | + () -> startBeingLeader.countDown(), () -> stopBeingLeader.countDown() |
| 74 | + ), |
| 75 | + String.format("%s-leader-elector-main", lockIdentity) |
| 76 | + ); |
| 77 | + thread.setDaemon(true); |
| 78 | + thread.start(); |
| 79 | + |
| 80 | + return leaderElector; |
| 81 | + } |
| 82 | +} |
0 commit comments