Skip to content

Commit e15d4d1

Browse files
msbtrjernst
authored andcommitted
Internal: Fix equality check of timevalue after serialization
closes #9218
1 parent 583c492 commit e15d4d1

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/main/java/org/elasticsearch/common/unit/TimeValue.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ public static TimeValue readTimeValue(StreamInput in) throws IOException {
268268
return timeValue;
269269
}
270270

271+
/**
272+
* serialization converts TimeValue internally to NANOSECONDS
273+
*/
271274
@Override
272275
public void readFrom(StreamInput in) throws IOException {
273276
duration = in.readLong();
@@ -285,17 +288,12 @@ public boolean equals(Object o) {
285288
if (o == null || getClass() != o.getClass()) return false;
286289

287290
TimeValue timeValue = (TimeValue) o;
288-
289-
if (duration != timeValue.duration) return false;
290-
if (timeUnit != timeValue.timeUnit) return false;
291-
292-
return true;
291+
return timeUnit.toNanos(duration) == timeValue.timeUnit.toNanos(timeValue.duration);
293292
}
294293

295294
@Override
296295
public int hashCode() {
297-
int result = (int) (duration ^ (duration >>> 32));
298-
result = 31 * result + (timeUnit != null ? timeUnit.hashCode() : 0);
299-
return result;
296+
long normalized = timeUnit.toNanos(duration);
297+
return (int) (normalized ^ (normalized >>> 32));
300298
}
301299
}

src/test/java/org/elasticsearch/common/unit/TimeValueTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919

2020
package org.elasticsearch.common.unit;
2121

22+
import org.elasticsearch.common.io.stream.BytesStreamInput;
23+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2224
import org.elasticsearch.test.ElasticsearchTestCase;
2325
import org.joda.time.PeriodType;
2426
import org.junit.Test;
2527

28+
import java.io.IOException;
2629
import java.util.concurrent.TimeUnit;
2730

28-
import static org.hamcrest.MatcherAssert.assertThat;
2931
import static org.hamcrest.Matchers.equalTo;
3032
import static org.hamcrest.Matchers.lessThan;
3133

@@ -66,4 +68,22 @@ public void testFormat() {
6668
public void testMinusOne() {
6769
assertThat(new TimeValue(-1).nanos(), lessThan(0l));
6870
}
71+
72+
private void assertEqualityAfterSerialize(TimeValue value) throws IOException {
73+
BytesStreamOutput out = new BytesStreamOutput();
74+
value.writeTo(out);
75+
76+
BytesStreamInput in = new BytesStreamInput(out.bytes());
77+
TimeValue inValue = TimeValue.readTimeValue(in);
78+
79+
assertThat(inValue, equalTo(value));
80+
}
81+
82+
@Test
83+
public void testSerialize() throws Exception {
84+
assertEqualityAfterSerialize(new TimeValue(100, TimeUnit.DAYS));
85+
assertEqualityAfterSerialize(new TimeValue(-1));
86+
assertEqualityAfterSerialize(new TimeValue(1, TimeUnit.NANOSECONDS));
87+
88+
}
6989
}

0 commit comments

Comments
 (0)