Internal: Fix equality check of timevalue after serialization
closes #9218
This commit is contained in:
parent
583c4926c4
commit
e15d4d1124
|
@ -268,6 +268,9 @@ public class TimeValue implements Serializable, Streamable {
|
||||||
return timeValue;
|
return timeValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* serialization converts TimeValue internally to NANOSECONDS
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
public void readFrom(StreamInput in) throws IOException {
|
||||||
duration = in.readLong();
|
duration = in.readLong();
|
||||||
|
@ -285,17 +288,12 @@ public class TimeValue implements Serializable, Streamable {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
TimeValue timeValue = (TimeValue) o;
|
TimeValue timeValue = (TimeValue) o;
|
||||||
|
return timeUnit.toNanos(duration) == timeValue.timeUnit.toNanos(timeValue.duration);
|
||||||
if (duration != timeValue.duration) return false;
|
|
||||||
if (timeUnit != timeValue.timeUnit) return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int result = (int) (duration ^ (duration >>> 32));
|
long normalized = timeUnit.toNanos(duration);
|
||||||
result = 31 * result + (timeUnit != null ? timeUnit.hashCode() : 0);
|
return (int) (normalized ^ (normalized >>> 32));
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,13 +19,15 @@
|
||||||
|
|
||||||
package org.elasticsearch.common.unit;
|
package org.elasticsearch.common.unit;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.io.stream.BytesStreamInput;
|
||||||
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||||
import org.joda.time.PeriodType;
|
import org.joda.time.PeriodType;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.lessThan;
|
import static org.hamcrest.Matchers.lessThan;
|
||||||
|
|
||||||
|
@ -66,4 +68,22 @@ public class TimeValueTests extends ElasticsearchTestCase {
|
||||||
public void testMinusOne() {
|
public void testMinusOne() {
|
||||||
assertThat(new TimeValue(-1).nanos(), lessThan(0l));
|
assertThat(new TimeValue(-1).nanos(), lessThan(0l));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertEqualityAfterSerialize(TimeValue value) throws IOException {
|
||||||
|
BytesStreamOutput out = new BytesStreamOutput();
|
||||||
|
value.writeTo(out);
|
||||||
|
|
||||||
|
BytesStreamInput in = new BytesStreamInput(out.bytes());
|
||||||
|
TimeValue inValue = TimeValue.readTimeValue(in);
|
||||||
|
|
||||||
|
assertThat(inValue, equalTo(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSerialize() throws Exception {
|
||||||
|
assertEqualityAfterSerialize(new TimeValue(100, TimeUnit.DAYS));
|
||||||
|
assertEqualityAfterSerialize(new TimeValue(-1));
|
||||||
|
assertEqualityAfterSerialize(new TimeValue(1, TimeUnit.NANOSECONDS));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue