Skip to content

Commit dbd45f4

Browse files
Merge pull request #4954 from yue9944882/fix/extended-leaderelectionrecord-equals-renewtime
[extended] Fix LeaderElectionRecord.equals null renewTime comparison
2 parents 62cfb2b + 1a1ccd9 commit dbd45f4

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎extended/src/main/java/io/kubernetes/client/extended/leaderelection/LeaderElectionRecord.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public boolean equals(Object o) {
139139
if (!acquireTime.equals(that.acquireTime)) return false;
140140
}
141141
if (renewTime == null) {
142-
return that.holderIdentity == null;
142+
return that.renewTime == null;
143143
}
144144
return renewTime.equals(that.renewTime);
145145
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)