Skip to content

Commit 5ea750f

Browse files
authored
Clean up wire test case a bit (#50627)
* Adds JavaDoc to `AbstractWireTestCase` and `AbstractWireSerializingTestCase` so it is more obvious you should prefer the latter if you have a choice * Moves the `instanceReader` method out of `AbstractWireTestCase` becaue it is no longer used. * Marks a bunch of methods final so it is more obvious which classes are for what. * Cleans up the side effects of the above.
1 parent 51799dc commit 5ea750f

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed

libs/geo/src/test/java/org/elasticsearch/geometry/BaseGeometryTestCase.java

-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.elasticsearch.ElasticsearchException;
2323
import org.elasticsearch.Version;
24-
import org.elasticsearch.common.io.stream.Writeable;
2524
import org.elasticsearch.geometry.utils.GeographyValidator;
2625
import org.elasticsearch.geometry.utils.WellKnownText;
2726
import org.elasticsearch.test.AbstractWireTestCase;
@@ -42,12 +41,6 @@ protected final T createTestInstance() {
4241

4342
protected abstract T createTestInstance(boolean hasAlt);
4443

45-
@Override
46-
protected Writeable.Reader<T> instanceReader() {
47-
throw new IllegalStateException("shouldn't be called in this test");
48-
}
49-
50-
5144
@SuppressWarnings("unchecked")
5245
@Override
5346
protected T copyInstance(T instance, Version version) throws IOException {

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponseTests.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@
1919

2020
package org.elasticsearch.action.admin.indices.rollover;
2121

22-
import org.elasticsearch.Version;
2322
import org.elasticsearch.common.io.stream.Writeable;
2423
import org.elasticsearch.common.unit.ByteSizeValue;
2524
import org.elasticsearch.common.unit.TimeValue;
26-
import org.elasticsearch.test.AbstractWireTestCase;
25+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
2726

28-
import java.io.IOException;
2927
import java.util.ArrayList;
3028
import java.util.HashMap;
3129
import java.util.List;
3230
import java.util.Map;
3331
import java.util.function.Supplier;
3432

35-
public class RolloverResponseTests extends AbstractWireTestCase<RolloverResponse> {
33+
public class RolloverResponseTests extends AbstractWireSerializingTestCase<RolloverResponse> {
3634

3735
@Override
3836
protected RolloverResponse createTestInstance() {
@@ -65,11 +63,6 @@ protected Writeable.Reader<RolloverResponse> instanceReader() {
6563
return RolloverResponse::new;
6664
}
6765

68-
@Override
69-
protected RolloverResponse copyInstance(RolloverResponse instance, Version version) throws IOException {
70-
return copyWriteable(instance, writableRegistry(), RolloverResponse::new);
71-
}
72-
7366
@Override
7467
protected RolloverResponse mutateInstance(RolloverResponse response) {
7568
int i = randomIntBetween(0, 6);

test/framework/src/main/java/org/elasticsearch/test/AbstractWireSerializingTestCase.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@
1919
package org.elasticsearch.test;
2020

2121
import org.elasticsearch.Version;
22+
import org.elasticsearch.common.io.stream.StreamInput;
23+
import org.elasticsearch.common.io.stream.StreamOutput;
2224
import org.elasticsearch.common.io.stream.Writeable;
2325

2426
import java.io.IOException;
2527

28+
/**
29+
* Standard test case for testing the wire serialization of subclasses of {@linkplain Writeable}.
30+
*/
2631
public abstract class AbstractWireSerializingTestCase<T extends Writeable> extends AbstractWireTestCase<T> {
32+
/**
33+
* Returns a {@link Writeable.Reader} that can be used to de-serialize the instance
34+
*/
35+
protected abstract Writeable.Reader<T> instanceReader();
2736

37+
/**
38+
* Copy the {@link Writeable} by round tripping it through {@linkplain StreamInput} and {@linkplain StreamOutput}.
39+
*/
2840
@Override
29-
protected T copyInstance(T instance, Version version) throws IOException {
41+
protected final T copyInstance(T instance, Version version) throws IOException {
3042
return copyWriteable(instance, getNamedWriteableRegistry(), instanceReader(), version);
3143
}
3244
}

test/framework/src/main/java/org/elasticsearch/test/AbstractWireTestCase.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
import java.io.IOException;
2828
import java.util.Collections;
2929

30+
/**
31+
* Standard test case for testing wire serialization. If the class being tested
32+
* extends {@link Writeable} then prefer extending {@link AbstractWireSerializingTestCase}.
33+
*/
3034
public abstract class AbstractWireTestCase<T> extends ESTestCase {
3135

3236
protected static final int NUMBER_OF_TEST_RUNS = 20;
@@ -38,11 +42,6 @@ public abstract class AbstractWireTestCase<T> extends ESTestCase {
3842
*/
3943
protected abstract T createTestInstance();
4044

41-
/**
42-
* Returns a {@link Writeable.Reader} that can be used to de-serialize the instance
43-
*/
44-
protected abstract Writeable.Reader<T> instanceReader();
45-
4645
/**
4746
* Returns an instance which is mutated slightly so it should not be equal
4847
* to the given instance.
@@ -73,18 +72,26 @@ public final void testSerialization() throws IOException {
7372
}
7473

7574
/**
76-
* Serialize the given instance and asserts that both are equal
75+
* Serialize the given instance and asserts that both are equal.
7776
*/
78-
protected final T assertSerialization(T testInstance) throws IOException {
79-
return assertSerialization(testInstance, Version.CURRENT);
77+
protected final void assertSerialization(T testInstance) throws IOException {
78+
assertSerialization(testInstance, Version.CURRENT);
8079
}
8180

82-
protected final T assertSerialization(T testInstance, Version version) throws IOException {
81+
/**
82+
* Assert that instances copied at a particular version are equal. The version is useful
83+
* for sanity checking the backwards compatibility of the wire. It isn't a substitute for
84+
* real backwards compatibility tests but it is *so* much faster.
85+
*/
86+
protected final void assertSerialization(T testInstance, Version version) throws IOException {
8387
T deserializedInstance = copyInstance(testInstance, version);
8488
assertEqualInstances(testInstance, deserializedInstance);
85-
return deserializedInstance;
8689
}
8790

91+
/**
92+
* Assert that two instances are equal. This is intentionally not final so we can override
93+
* how equality is checked.
94+
*/
8895
protected void assertEqualInstances(T expectedInstance, T newInstance) {
8996
assertNotSame(newInstance, expectedInstance);
9097
assertEquals(expectedInstance, newInstance);
@@ -95,6 +102,11 @@ protected final T copyInstance(T instance) throws IOException {
95102
return copyInstance(instance, Version.CURRENT);
96103
}
97104

105+
/**
106+
* Copy the instance as by reading and writing using the code specific to the provided version.
107+
* The version is useful for sanity checking the backwards compatibility of the wire. It isn't
108+
* a substitute for real backwards compatibility tests but it is *so* much faster.
109+
*/
98110
protected abstract T copyInstance(T instance, Version version) throws IOException;
99111

100112
/**

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/AbstractSqlWireSerializingTestCase.java

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ protected T copyInstance(T instance, Version version) throws IOException {
3232
}
3333
}
3434
}
35+
36+
/**
37+
* Returns a {@link Writeable.Reader} that can be used to de-serialize the instance
38+
*/
39+
protected abstract Writeable.Reader<T> instanceReader();
3540

3641
protected ZoneId instanceZoneId(T instance) {
3742
return randomSafeZone();

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/session/ListCursorTests.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77

88
import org.elasticsearch.Version;
99
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
10-
import org.elasticsearch.common.io.stream.Writeable.Reader;
11-
import org.elasticsearch.test.AbstractWireSerializingTestCase;
10+
import org.elasticsearch.test.AbstractWireTestCase;
1211

1312
import java.io.IOException;
1413
import java.util.ArrayList;
1514
import java.util.Arrays;
1615
import java.util.List;
1716

18-
public class ListCursorTests extends AbstractWireSerializingTestCase<ListCursor> {
17+
public class ListCursorTests extends AbstractWireTestCase<ListCursor> {
1918
public static ListCursor randomPagingListCursor() {
2019
int size = between(1, 20);
2120
int depth = between(1, 20);
@@ -45,17 +44,12 @@ protected ListCursor createTestInstance() {
4544
return randomPagingListCursor();
4645
}
4746

48-
@Override
49-
protected Reader<ListCursor> instanceReader() {
50-
return ListCursor::new;
51-
}
52-
5347
@Override
5448
protected ListCursor copyInstance(ListCursor instance, Version version) throws IOException {
5549
/* Randomly choose between internal protocol round trip and String based
5650
* round trips used to toXContent. */
5751
if (randomBoolean()) {
58-
return super.copyInstance(instance, version);
52+
return copyWriteable(instance, getNamedWriteableRegistry(), ListCursor::new, version);
5953
}
6054
return (ListCursor) Cursors.decodeFromString(Cursors.encodeToString(instance, randomZone()));
6155
}

0 commit comments

Comments
 (0)