1919
2020package org .elasticsearch .common .io .stream ;
2121
22- import org .elasticsearch .Version ;
23- import org .elasticsearch .test .ESTestCase ;
24- import org .elasticsearch .test .VersionUtils ;
22+ import static java .util .Collections .singletonList ;
23+ import static org .hamcrest .Matchers .equalTo ;
2524
2625import java .io .IOException ;
2726
28- import static java .util .Collections .singletonList ;
29- import static org .hamcrest .Matchers .equalTo ;
27+ import org .elasticsearch .Version ;
28+ import org .elasticsearch .test .ESTestCase ;
29+ import org .elasticsearch .test .VersionUtils ;
3030
3131public class DelayableWriteableTests extends ESTestCase {
3232 // NOTE: we don't use AbstractWireSerializingTestCase because we don't implement equals and hashCode.
33- public static class Example implements NamedWriteable {
33+ private static class Example implements NamedWriteable {
3434 private final String s ;
3535
36- public Example (String s ) {
36+ Example (String s ) {
3737 this .s = s ;
3838 }
3939
40- public Example (StreamInput in ) throws IOException {
40+ Example (StreamInput in ) throws IOException {
4141 s = in .readString ();
4242 }
4343
@@ -66,14 +66,14 @@ public int hashCode() {
6666 }
6767 }
6868
69- public static class NamedHolder implements Writeable {
69+ private static class NamedHolder implements Writeable {
7070 private final Example e ;
7171
72- public NamedHolder (Example e ) {
72+ NamedHolder (Example e ) {
7373 this .e = e ;
7474 }
7575
76- public NamedHolder (StreamInput in ) throws IOException {
76+ NamedHolder (StreamInput in ) throws IOException {
7777 e = in .readNamedWriteable (Example .class );
7878 }
7979
@@ -97,6 +97,23 @@ public int hashCode() {
9797 }
9898 }
9999
100+ private static class SneakOtherSideVersionOnWire implements Writeable {
101+ private final Version version ;
102+
103+ SneakOtherSideVersionOnWire () {
104+ version = Version .CURRENT ;
105+ }
106+
107+ SneakOtherSideVersionOnWire (StreamInput in ) throws IOException {
108+ version = Version .readVersion (in );
109+ }
110+
111+ @ Override
112+ public void writeTo (StreamOutput out ) throws IOException {
113+ Version .writeVersion (out .getVersion (), out );
114+ }
115+ }
116+
100117 public void testRoundTripFromReferencing () throws IOException {
101118 Example e = new Example (randomAlphaOfLength (5 ));
102119 DelayableWriteable <Example > original = DelayableWriteable .referencing (e );
@@ -139,6 +156,12 @@ public void testRoundTripFromDelayedFromOldVersionWithNamedWriteable() throws IO
139156 roundTripTestCase (original , NamedHolder ::new );
140157 }
141158
159+ public void testSerializesWithRemoteVersion () throws IOException {
160+ Version remoteVersion = VersionUtils .randomCompatibleVersion (random (), Version .CURRENT );
161+ DelayableWriteable <SneakOtherSideVersionOnWire > original = DelayableWriteable .referencing (new SneakOtherSideVersionOnWire ());
162+ assertThat (roundTrip (original , SneakOtherSideVersionOnWire ::new , remoteVersion ).get ().version , equalTo (remoteVersion ));
163+ }
164+
142165 private <T extends Writeable > void roundTripTestCase (DelayableWriteable <T > original , Writeable .Reader <T > reader ) throws IOException {
143166 DelayableWriteable <T > roundTripped = roundTrip (original , reader , Version .CURRENT );
144167 assertTrue (roundTripped .isDelayed ());
0 commit comments