|
| 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 | +package io.kubernetes.client.extended.leaderelection; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +import java.util.Date; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +class LeaderElectionRecordTest { |
| 21 | + |
| 22 | + // Two records that differ only by having a null renewTime must still be equal to each |
| 23 | + // other when every other field (including renewTime, which is null on both) matches. |
| 24 | + @Test |
| 25 | + void equalWhenRenewTimeIsNullOnBoth() { |
| 26 | + Date acquireTime = new Date(1000L); |
| 27 | + LeaderElectionRecord a = new LeaderElectionRecord("holder", 15, acquireTime, null, 3); |
| 28 | + LeaderElectionRecord b = new LeaderElectionRecord("holder", 15, acquireTime, null, 3); |
| 29 | + |
| 30 | + assertThat(a).isEqualTo(b); |
| 31 | + assertThat(b).isEqualTo(a); |
| 32 | + assertThat(a.hashCode()).isEqualTo(b.hashCode()); |
| 33 | + } |
| 34 | + |
| 35 | + // When one record has a null renewTime and the other does not, they must be unequal, |
| 36 | + // regardless of the holderIdentity value. |
| 37 | + @Test |
| 38 | + void notEqualWhenOnlyOneRenewTimeIsNull() { |
| 39 | + Date acquireTime = new Date(1000L); |
| 40 | + Date renewTime = new Date(2000L); |
| 41 | + LeaderElectionRecord nullRenew = new LeaderElectionRecord("holder", 15, acquireTime, null, 3); |
| 42 | + LeaderElectionRecord withRenew = |
| 43 | + new LeaderElectionRecord("holder", 15, acquireTime, renewTime, 3); |
| 44 | + |
| 45 | + assertThat(nullRenew).isNotEqualTo(withRenew); |
| 46 | + assertThat(withRenew).isNotEqualTo(nullRenew); |
| 47 | + } |
| 48 | +} |
0 commit comments