Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

package org.elasticsearch.common.io.stream;

import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;

import java.io.IOException;
import java.util.function.Supplier;

import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;

/**
* A holder for {@link Writeable}s that can delays reading the underlying
* {@linkplain Writeable} when it is read from a remote node.
Expand Down Expand Up @@ -60,6 +60,7 @@ private static class Referencing<T extends Writeable> extends DelayableWriteable
@Override
public void writeTo(StreamOutput out) throws IOException {
try (BytesStreamOutput buffer = new BytesStreamOutput()) {
buffer.setVersion(out.getVersion());
reference.writeTo(buffer);
out.writeBytesReference(buffer.bytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@

package org.elasticsearch.common.io.stream;

import org.elasticsearch.Version;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.equalTo;

import java.io.IOException;

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.equalTo;
import org.elasticsearch.Version;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;

public class DelayableWriteableTests extends ESTestCase {
// NOTE: we don't use AbstractWireSerializingTestCase because we don't implement equals and hashCode.
public static class Example implements NamedWriteable {
private static class Example implements NamedWriteable {
private final String s;

public Example(String s) {
Example(String s) {
this.s = s;
}

public Example(StreamInput in) throws IOException {
Example(StreamInput in) throws IOException {
s = in.readString();
}

Expand Down Expand Up @@ -66,14 +66,14 @@ public int hashCode() {
}
}

public static class NamedHolder implements Writeable {
private static class NamedHolder implements Writeable {
private final Example e;

public NamedHolder(Example e) {
NamedHolder(Example e) {
this.e = e;
}

public NamedHolder(StreamInput in) throws IOException {
NamedHolder(StreamInput in) throws IOException {
e = in.readNamedWriteable(Example.class);
}

Expand All @@ -97,6 +97,23 @@ public int hashCode() {
}
}

private static class SneakOtherSideVersionOnWire implements Writeable {
private final Version version;

SneakOtherSideVersionOnWire() {
version = Version.CURRENT;
}

SneakOtherSideVersionOnWire(StreamInput in) throws IOException {
version = Version.readVersion(in);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
Version.writeVersion(out.getVersion(), out);
}
}

public void testRoundTripFromReferencing() throws IOException {
Example e = new Example(randomAlphaOfLength(5));
DelayableWriteable<Example> original = DelayableWriteable.referencing(e);
Expand Down Expand Up @@ -139,6 +156,12 @@ public void testRoundTripFromDelayedFromOldVersionWithNamedWriteable() throws IO
roundTripTestCase(original, NamedHolder::new);
}

public void testSerializesWithRemoteVersion() throws IOException {
Version remoteVersion = VersionUtils.randomCompatibleVersion(random(), Version.CURRENT);
DelayableWriteable<SneakOtherSideVersionOnWire> original = DelayableWriteable.referencing(new SneakOtherSideVersionOnWire());
assertThat(roundTrip(original, SneakOtherSideVersionOnWire::new, remoteVersion).get().version, equalTo(remoteVersion));
}

private <T extends Writeable> void roundTripTestCase(DelayableWriteable<T> original, Writeable.Reader<T> reader) throws IOException {
DelayableWriteable<T> roundTripped = roundTrip(original, reader, Version.CURRENT);
assertTrue(roundTripped.isDelayed());
Expand Down