[test] remove Streamable serde assertions (#29307)
Removes a set of assertions in the test framework that verified that Streamable objects could be serialized and deserialized across different versions. When this was discussed the consensus was that this approach has not caught many bugs in a long time and that serialization testing of objects was best left to their respective unit and integration tests. This commit also removes a transport interceptor that was used in ESIntegTestCase tests to make these assertions about objects coming in or off the wire.
This commit is contained in:
parent
199d131385
commit
b7e6fb9ac5
|
@ -81,7 +81,6 @@ import org.elasticsearch.snapshots.SnapshotId;
|
|||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.test.TestSearchContext;
|
||||
import org.elasticsearch.test.VersionUtils;
|
||||
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
|
||||
import org.elasticsearch.transport.ActionNotFoundTransportException;
|
||||
import org.elasticsearch.transport.ActionTransportException;
|
||||
import org.elasticsearch.transport.ConnectTransportException;
|
||||
|
@ -116,7 +115,6 @@ import static java.lang.reflect.Modifier.isInterface;
|
|||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.emptySet;
|
||||
import static java.util.Collections.singleton;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertVersionSerializable;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
|
||||
|
@ -233,7 +231,6 @@ public class ExceptionSerializationTests extends ESTestCase {
|
|||
}
|
||||
|
||||
private <T extends Exception> T serialize(T exception, Version version) throws IOException {
|
||||
ElasticsearchAssertions.assertVersionSerializable(version, exception);
|
||||
BytesStreamOutput out = new BytesStreamOutput();
|
||||
out.setVersion(version);
|
||||
out.writeException(exception);
|
||||
|
@ -578,9 +575,6 @@ public class ExceptionSerializationTests extends ESTestCase {
|
|||
}
|
||||
assertArrayEquals(deserialized.getStackTrace(), ex.getStackTrace());
|
||||
assertTrue(deserialized.getStackTrace().length > 1);
|
||||
assertVersionSerializable(VersionUtils.randomVersion(random()), cause);
|
||||
assertVersionSerializable(VersionUtils.randomVersion(random()), ex);
|
||||
assertVersionSerializable(VersionUtils.randomVersion(random()), deserialized);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -94,7 +94,6 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFail
|
|||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFirstHit;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSecondHit;
|
||||
|
@ -191,7 +190,7 @@ public class SearchQueryIT extends ESIntegTestCase {
|
|||
SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(matchQuery("field1", "quick"))).get();
|
||||
assertHitCount(searchResponse, 2L);
|
||||
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
|
||||
assertSearchHit(searchHit, hasScore(1.0f));
|
||||
assertThat(searchHit, hasScore(1.0f));
|
||||
}
|
||||
|
||||
searchResponse = client().prepareSearch("test").setQuery(
|
||||
|
@ -210,7 +209,7 @@ public class SearchQueryIT extends ESIntegTestCase {
|
|||
assertHitCount(searchResponse, 2L);
|
||||
assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
|
||||
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
|
||||
assertSearchHit(searchHit, hasScore(1.0f));
|
||||
assertThat(searchHit, hasScore(1.0f));
|
||||
}
|
||||
|
||||
int num = scaledRandomIntBetween(100, 200);
|
||||
|
@ -228,7 +227,7 @@ public class SearchQueryIT extends ESIntegTestCase {
|
|||
long totalHits = searchResponse.getHits().getTotalHits();
|
||||
SearchHits hits = searchResponse.getHits();
|
||||
for (SearchHit searchHit : hits) {
|
||||
assertSearchHit(searchHit, hasScore(1.0f));
|
||||
assertThat(searchHit, hasScore(1.0f));
|
||||
}
|
||||
searchResponse = client().prepareSearch("test_1").setQuery(
|
||||
boolQuery().must(matchAllQuery()).must(
|
||||
|
@ -238,7 +237,7 @@ public class SearchQueryIT extends ESIntegTestCase {
|
|||
if (totalHits > 1) {
|
||||
float expected = hits.getAt(0).getScore();
|
||||
for (SearchHit searchHit : hits) {
|
||||
assertSearchHit(searchHit, hasScore(expected));
|
||||
assertThat(searchHit, hasScore(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,7 +69,6 @@ import static org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList;
|
|||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAllSuccessful;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasScore;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
|
@ -245,8 +244,8 @@ public class CompletionSuggestSearchIT extends ESIntegTestCase {
|
|||
int id = numDocs;
|
||||
for (CompletionSuggestion.Entry.Option option : options) {
|
||||
assertThat(option.getText().toString(), equalTo("suggestion" + id));
|
||||
assertSearchHit(option.getHit(), hasId("" + id));
|
||||
assertSearchHit(option.getHit(), hasScore((id)));
|
||||
assertThat(option.getHit(), hasId("" + id));
|
||||
assertThat(option.getHit(), hasScore((id)));
|
||||
assertNotNull(option.getHit().getSourceAsMap());
|
||||
id--;
|
||||
}
|
||||
|
@ -280,8 +279,8 @@ public class CompletionSuggestSearchIT extends ESIntegTestCase {
|
|||
int id = numDocs;
|
||||
for (CompletionSuggestion.Entry.Option option : options) {
|
||||
assertThat(option.getText().toString(), equalTo("suggestion" + id));
|
||||
assertSearchHit(option.getHit(), hasId("" + id));
|
||||
assertSearchHit(option.getHit(), hasScore((id)));
|
||||
assertThat(option.getHit(), hasId("" + id));
|
||||
assertThat(option.getHit(), hasScore((id)));
|
||||
assertNull(option.getHit().getSourceAsMap());
|
||||
id--;
|
||||
}
|
||||
|
@ -317,8 +316,8 @@ public class CompletionSuggestSearchIT extends ESIntegTestCase {
|
|||
int id = numDocs;
|
||||
for (CompletionSuggestion.Entry.Option option : options) {
|
||||
assertThat(option.getText().toString(), equalTo("suggestion" + id));
|
||||
assertSearchHit(option.getHit(), hasId("" + id));
|
||||
assertSearchHit(option.getHit(), hasScore((id)));
|
||||
assertThat(option.getHit(), hasId("" + id));
|
||||
assertThat(option.getHit(), hasScore((id)));
|
||||
assertNotNull(option.getHit().getSourceAsMap());
|
||||
Set<String> sourceFields = option.getHit().getSourceAsMap().keySet();
|
||||
assertThat(sourceFields, contains("a"));
|
||||
|
|
|
@ -143,7 +143,6 @@ import org.elasticsearch.test.disruption.NetworkDisruption;
|
|||
import org.elasticsearch.test.disruption.ServiceDisruptionScheme;
|
||||
import org.elasticsearch.test.store.MockFSIndexStore;
|
||||
import org.elasticsearch.test.transport.MockTransportService;
|
||||
import org.elasticsearch.transport.AssertingTransportInterceptor;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
|
@ -1921,9 +1920,6 @@ public abstract class ESIntegTestCase extends ESTestCase {
|
|||
if (randomBoolean()) {
|
||||
mocks.add(MockSearchService.TestPlugin.class);
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
mocks.add(AssertingTransportInterceptor.TestPlugin.class);
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
mocks.add(MockFieldFilterPlugin.class);
|
||||
}
|
||||
|
|
|
@ -23,7 +23,6 @@ import org.apache.lucene.search.DisjunctionMaxQuery;
|
|||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.ExceptionsHelper;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionFuture;
|
||||
import org.elasticsearch.action.ActionRequest;
|
||||
import org.elasticsearch.action.ActionRequestBuilder;
|
||||
|
@ -49,13 +48,6 @@ import org.elasticsearch.cluster.block.ClusterBlockException;
|
|||
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.io.stream.Streamable;
|
||||
import org.elasticsearch.common.io.stream.Writeable;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.DeprecationHandler;
|
||||
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
||||
|
@ -65,18 +57,13 @@ import org.elasticsearch.common.xcontent.XContentParser;
|
|||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.rest.RestStatus;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchModule;
|
||||
import org.elasticsearch.search.suggest.Suggest;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.NotEqualMessageBuilder;
|
||||
import org.elasticsearch.test.VersionUtils;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
|
@ -88,9 +75,6 @@ import java.util.Locale;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.apache.lucene.util.LuceneTestCase.random;
|
||||
import static org.elasticsearch.test.VersionUtils.randomVersion;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
@ -124,7 +108,6 @@ public class ElasticsearchAssertions {
|
|||
|
||||
public static void assertAcked(AcknowledgedResponse response) {
|
||||
assertThat(response.getClass().getSimpleName() + " failed - not acked", response.isAcknowledged(), equalTo(true));
|
||||
assertVersionSerializable(response);
|
||||
}
|
||||
|
||||
public static void assertAcked(DeleteIndexRequestBuilder builder) {
|
||||
|
@ -133,7 +116,6 @@ public class ElasticsearchAssertions {
|
|||
|
||||
public static void assertAcked(DeleteIndexResponse response) {
|
||||
assertThat("Delete Index failed - not acked", response.isAcknowledged(), equalTo(true));
|
||||
assertVersionSerializable(response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -142,7 +124,6 @@ public class ElasticsearchAssertions {
|
|||
*/
|
||||
public static void assertAcked(CreateIndexResponse response) {
|
||||
assertThat(response.getClass().getSimpleName() + " failed - not acked", response.isAcknowledged(), equalTo(true));
|
||||
assertVersionSerializable(response);
|
||||
assertTrue(response.getClass().getSimpleName() + " failed - index creation acked but not all shards were started",
|
||||
response.isShardsAcknowledged());
|
||||
}
|
||||
|
@ -236,7 +217,6 @@ public class ElasticsearchAssertions {
|
|||
}
|
||||
assertThat("Some expected ids were not found in search results: " + Arrays.toString(idsSet.toArray(new String[idsSet.size()])) + "."
|
||||
+ shardStatus, idsSet.size(), equalTo(0));
|
||||
assertVersionSerializable(searchResponse);
|
||||
}
|
||||
|
||||
public static void assertSortValues(SearchResponse searchResponse, Object[]... sortValues) {
|
||||
|
@ -247,7 +227,6 @@ public class ElasticsearchAssertions {
|
|||
final Object[] hitsSortValues = hits[i].getSortValues();
|
||||
assertArrayEquals("Offset " + Integer.toString(i) + ", id " + hits[i].getId(), sortValues[i], hitsSortValues);
|
||||
}
|
||||
assertVersionSerializable(searchResponse);
|
||||
}
|
||||
|
||||
public static void assertOrderedSearchHits(SearchResponse searchResponse, String... ids) {
|
||||
|
@ -257,14 +236,12 @@ public class ElasticsearchAssertions {
|
|||
SearchHit hit = searchResponse.getHits().getHits()[i];
|
||||
assertThat("Expected id: " + ids[i] + " at position " + i + " but wasn't." + shardStatus, hit.getId(), equalTo(ids[i]));
|
||||
}
|
||||
assertVersionSerializable(searchResponse);
|
||||
}
|
||||
|
||||
public static void assertHitCount(SearchResponse countResponse, long expectedHitCount) {
|
||||
if (countResponse.getHits().getTotalHits() != expectedHitCount) {
|
||||
fail("Count is " + countResponse.getHits().getTotalHits() + " but " + expectedHitCount + " was expected. " + formatShardStatus(countResponse));
|
||||
}
|
||||
assertVersionSerializable(countResponse);
|
||||
}
|
||||
|
||||
public static void assertExists(GetResponse response) {
|
||||
|
@ -296,26 +273,22 @@ public class ElasticsearchAssertions {
|
|||
assertThat(number, greaterThan(0));
|
||||
assertThat("SearchHit number must be greater than 0", number, greaterThan(0));
|
||||
assertThat(searchResponse.getHits().getTotalHits(), greaterThanOrEqualTo((long) number));
|
||||
assertSearchHit(searchResponse.getHits().getAt(number - 1), matcher);
|
||||
assertVersionSerializable(searchResponse);
|
||||
assertThat(searchResponse.getHits().getAt(number - 1), matcher);
|
||||
}
|
||||
|
||||
public static void assertNoFailures(SearchResponse searchResponse) {
|
||||
assertThat("Unexpected ShardFailures: " + Arrays.toString(searchResponse.getShardFailures()),
|
||||
searchResponse.getShardFailures().length, equalTo(0));
|
||||
assertVersionSerializable(searchResponse);
|
||||
}
|
||||
|
||||
public static void assertFailures(SearchResponse searchResponse) {
|
||||
assertThat("Expected at least one shard failure, got none",
|
||||
searchResponse.getShardFailures().length, greaterThan(0));
|
||||
assertVersionSerializable(searchResponse);
|
||||
}
|
||||
|
||||
public static void assertNoFailures(BulkResponse response) {
|
||||
assertThat("Unexpected ShardFailures: " + response.buildFailureMessage(),
|
||||
response.hasFailures(), is(false));
|
||||
assertVersionSerializable(response);
|
||||
}
|
||||
|
||||
public static void assertFailures(SearchRequestBuilder searchRequestBuilder, RestStatus restStatus, Matcher<String> reasonMatcher) {
|
||||
|
@ -328,7 +301,6 @@ public class ElasticsearchAssertions {
|
|||
assertThat(shardSearchFailure.status(), equalTo(restStatus));
|
||||
assertThat(shardSearchFailure.reason(), reasonMatcher);
|
||||
}
|
||||
assertVersionSerializable(searchResponse);
|
||||
} catch (SearchPhaseExecutionException e) {
|
||||
assertThat(e.status(), equalTo(restStatus));
|
||||
assertThat(e.toString(), reasonMatcher);
|
||||
|
@ -343,26 +315,18 @@ public class ElasticsearchAssertions {
|
|||
|
||||
public static void assertNoFailures(BroadcastResponse response) {
|
||||
assertThat("Unexpected ShardFailures: " + Arrays.toString(response.getShardFailures()), response.getFailedShards(), equalTo(0));
|
||||
assertVersionSerializable(response);
|
||||
}
|
||||
|
||||
public static void assertAllSuccessful(BroadcastResponse response) {
|
||||
assertNoFailures(response);
|
||||
assertThat("Expected all shards successful",
|
||||
response.getSuccessfulShards(), equalTo(response.getTotalShards()));
|
||||
assertVersionSerializable(response);
|
||||
}
|
||||
|
||||
public static void assertAllSuccessful(SearchResponse response) {
|
||||
assertNoFailures(response);
|
||||
assertThat("Expected all shards successful",
|
||||
response.getSuccessfulShards(), equalTo(response.getTotalShards()));
|
||||
assertVersionSerializable(response);
|
||||
}
|
||||
|
||||
public static void assertSearchHit(SearchHit searchHit, Matcher<SearchHit> matcher) {
|
||||
assertThat(searchHit, matcher);
|
||||
assertVersionSerializable(searchHit);
|
||||
}
|
||||
|
||||
public static void assertHighlight(SearchResponse resp, int hit, String field, int fragment, Matcher<String> matcher) {
|
||||
|
@ -385,7 +349,6 @@ public class ElasticsearchAssertions {
|
|||
assertNoFailures(resp);
|
||||
assertThat("not enough hits", resp.getHits().getHits().length, greaterThan(hit));
|
||||
assertHighlight(resp.getHits().getHits()[hit], field, fragment, fragmentsMatcher, matcher);
|
||||
assertVersionSerializable(resp);
|
||||
}
|
||||
|
||||
private static void assertHighlight(SearchHit hit, String field, int fragment, Matcher<Integer> fragmentsMatcher, Matcher<String> matcher) {
|
||||
|
@ -407,7 +370,6 @@ public class ElasticsearchAssertions {
|
|||
assertThat(msg, searchSuggest.getSuggestion(key).getName(), equalTo(key));
|
||||
assertThat(msg, searchSuggest.getSuggestion(key).getEntries().size(), greaterThanOrEqualTo(entry));
|
||||
assertThat(msg, searchSuggest.getSuggestion(key).getEntries().get(entry).getOptions().size(), equalTo(size));
|
||||
assertVersionSerializable(searchSuggest);
|
||||
}
|
||||
|
||||
public static void assertSuggestionPhraseCollateMatchExists(Suggest searchSuggest, String key, int numberOfPhraseExists) {
|
||||
|
@ -434,7 +396,6 @@ public class ElasticsearchAssertions {
|
|||
assertThat(msg, searchSuggest.getSuggestion(key).getEntries().size(), greaterThanOrEqualTo(entry));
|
||||
assertThat(msg, searchSuggest.getSuggestion(key).getEntries().get(entry).getOptions().size(), greaterThan(ord));
|
||||
assertThat(msg, searchSuggest.getSuggestion(key).getEntries().get(entry).getOptions().get(ord).getText().string(), equalTo(text));
|
||||
assertVersionSerializable(searchSuggest);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -638,151 +599,6 @@ public class ElasticsearchAssertions {
|
|||
}
|
||||
}
|
||||
|
||||
private static BytesReference serialize(Version version, Streamable streamable) throws IOException {
|
||||
BytesStreamOutput output = new BytesStreamOutput();
|
||||
output.setVersion(version);
|
||||
streamable.writeTo(output);
|
||||
output.flush();
|
||||
return output.bytes();
|
||||
}
|
||||
|
||||
public static void assertVersionSerializable(Streamable streamable) {
|
||||
assertTrue(Version.CURRENT.after(VersionUtils.getPreviousVersion()));
|
||||
assertVersionSerializable(randomVersion(random()), streamable);
|
||||
}
|
||||
|
||||
public static void assertVersionSerializable(Version version, Streamable streamable) {
|
||||
/*
|
||||
* If possible we fetch the NamedWriteableRegistry from the test cluster. That is the only way to make sure that we properly handle
|
||||
* when plugins register names. If not possible we'll try and set up a registry based on whatever SearchModule registers. But that
|
||||
* is a hack at best - it only covers some things. If you end up with errors below and get to this comment I'm sorry. Please find
|
||||
* a way that sucks less.
|
||||
*/
|
||||
NamedWriteableRegistry registry;
|
||||
if (ESIntegTestCase.isInternalCluster() && ESIntegTestCase.internalCluster().size() > 0) {
|
||||
registry = ESIntegTestCase.internalCluster().getInstance(NamedWriteableRegistry.class);
|
||||
} else {
|
||||
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
|
||||
registry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
|
||||
}
|
||||
assertVersionSerializable(version, streamable, registry);
|
||||
}
|
||||
|
||||
public static void assertVersionSerializable(Version version, Streamable streamable, NamedWriteableRegistry namedWriteableRegistry) {
|
||||
try {
|
||||
Streamable newInstance = tryCreateNewInstance(streamable);
|
||||
if (newInstance == null) {
|
||||
return; // can't create a new instance - we never modify a
|
||||
// streamable that comes in.
|
||||
}
|
||||
if (streamable instanceof ActionRequest) {
|
||||
((ActionRequest) streamable).validate();
|
||||
}
|
||||
BytesReference orig;
|
||||
try {
|
||||
orig = serialize(version, streamable);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Can't serialize with this version so skip this test.
|
||||
return;
|
||||
}
|
||||
StreamInput input = orig.streamInput();
|
||||
if (namedWriteableRegistry != null) {
|
||||
input = new NamedWriteableAwareStreamInput(input, namedWriteableRegistry);
|
||||
}
|
||||
input.setVersion(version);
|
||||
// This is here since some Streamables are being converted into Writeables
|
||||
// and the readFrom method throws an exception if called
|
||||
Streamable newInstanceFromStream = tryCreateFromStream(streamable, input);
|
||||
if (newInstanceFromStream == null) {
|
||||
newInstance.readFrom(input);
|
||||
}
|
||||
assertThat("Stream should be fully read with version [" + version + "] for streamable [" + streamable + "]", input.available(),
|
||||
equalTo(0));
|
||||
BytesReference newBytes = serialize(version, streamable);
|
||||
if (false == orig.equals(newBytes)) {
|
||||
// The bytes are different. That is a failure. Lets try to throw a useful exception for debugging.
|
||||
String message = "Serialization failed with version [" + version + "] bytes should be equal for streamable [" + streamable
|
||||
+ "]";
|
||||
// If the bytes are different then comparing BytesRef's toStrings will show you *where* they are different
|
||||
assertEquals(message, orig.toBytesRef().toString(), newBytes.toBytesRef().toString());
|
||||
// They bytes aren't different. Very very weird.
|
||||
fail(message);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("failed to check serialization - version [" + version + "] for streamable [" + streamable + "]", ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void assertVersionSerializable(Version version, final Exception e) {
|
||||
ElasticsearchAssertions.assertVersionSerializable(version, new ExceptionWrapper(e));
|
||||
}
|
||||
|
||||
public static final class ExceptionWrapper implements Streamable {
|
||||
|
||||
private Exception exception;
|
||||
|
||||
public ExceptionWrapper(Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
|
||||
public ExceptionWrapper() {
|
||||
exception = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
exception = in.readException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
out.writeException(exception);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static Streamable tryCreateNewInstance(Streamable streamable) throws NoSuchMethodException, InstantiationException,
|
||||
IllegalAccessException, InvocationTargetException {
|
||||
try {
|
||||
Class<? extends Streamable> clazz = streamable.getClass();
|
||||
Constructor<? extends Streamable> constructor = clazz.getConstructor();
|
||||
assertThat(constructor, Matchers.notNullValue());
|
||||
Streamable newInstance = constructor.newInstance();
|
||||
return newInstance;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This attemps to construct a new {@link Streamable} object that is in the process of
|
||||
* being converted from {@link Streamable} to {@link Writeable}. Assuming this constructs
|
||||
* the object successfully, #readFrom should not be called on the constructed object.
|
||||
*
|
||||
* @param streamable the object to retrieve the type of class to construct the new instance from
|
||||
* @param in the stream to read the object from
|
||||
* @return the newly constructed object from reading the stream
|
||||
* @throws NoSuchMethodException if constuctor cannot be found
|
||||
* @throws InstantiationException if the class represents an abstract class
|
||||
* @throws IllegalAccessException if this {@code Constructor} object
|
||||
* is enforcing Java language access control and the underlying
|
||||
* constructor is inaccessible.
|
||||
* @throws InvocationTargetException if the underlying constructor
|
||||
* throws an exception.
|
||||
*/
|
||||
private static Streamable tryCreateFromStream(Streamable streamable, StreamInput in) throws NoSuchMethodException,
|
||||
InstantiationException, IllegalAccessException, InvocationTargetException {
|
||||
try {
|
||||
Class<? extends Streamable> clazz = streamable.getClass();
|
||||
Constructor<? extends Streamable> constructor = clazz.getConstructor(StreamInput.class);
|
||||
return constructor.newInstance(in);
|
||||
} catch (NoSuchMethodException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies basic assertions on the SearchResponse. This method checks if all shards were successful, if
|
||||
* any of the shards threw an exception and if the response is serializable.
|
||||
|
|
|
@ -1,130 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.transport;
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.Streamable;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.util.concurrent.ThreadContext;
|
||||
import org.elasticsearch.plugins.NetworkPlugin;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.tasks.Task;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.VersionUtils;
|
||||
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* A transport interceptor that applies {@link ElasticsearchAssertions#assertVersionSerializable(Streamable)}
|
||||
* to all requests and response objects send across the wire
|
||||
*/
|
||||
public final class AssertingTransportInterceptor implements TransportInterceptor {
|
||||
|
||||
private final Random random;
|
||||
private final NamedWriteableRegistry namedWriteableRegistry;
|
||||
|
||||
public static final class TestPlugin extends Plugin implements NetworkPlugin {
|
||||
|
||||
private final Settings settings;
|
||||
|
||||
public TestPlugin(Settings settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry,
|
||||
ThreadContext threadContext) {
|
||||
return Collections.singletonList(new AssertingTransportInterceptor(settings, namedWriteableRegistry));
|
||||
}
|
||||
}
|
||||
|
||||
public AssertingTransportInterceptor(Settings settings, NamedWriteableRegistry namedWriteableRegistry) {
|
||||
final long seed = ESIntegTestCase.INDEX_TEST_SEED_SETTING.get(settings);
|
||||
random = new Random(seed);
|
||||
this.namedWriteableRegistry = namedWriteableRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends TransportRequest> TransportRequestHandler<T> interceptHandler(String action, String executor,
|
||||
boolean forceExecution,
|
||||
TransportRequestHandler<T> actualHandler) {
|
||||
return new TransportRequestHandler<T>() {
|
||||
|
||||
@Override
|
||||
public void messageReceived(T request, TransportChannel channel, Task task) throws Exception {
|
||||
assertVersionSerializable(request);
|
||||
actualHandler.messageReceived(request, channel, task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(T request, TransportChannel channel) throws Exception {
|
||||
assertVersionSerializable(request);
|
||||
actualHandler.messageReceived(request, channel);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void assertVersionSerializable(Streamable streamable) {
|
||||
Version version = VersionUtils.randomVersionBetween(random, Version.CURRENT.minimumCompatibilityVersion(), Version.CURRENT);
|
||||
ElasticsearchAssertions.assertVersionSerializable(version, streamable, namedWriteableRegistry);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public AsyncSender interceptSender(final AsyncSender sender) {
|
||||
return new AsyncSender() {
|
||||
@Override
|
||||
public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request,
|
||||
TransportRequestOptions options,
|
||||
final TransportResponseHandler<T> handler) {
|
||||
assertVersionSerializable(request);
|
||||
sender.sendRequest(connection, action, request, options, new TransportResponseHandler<T>() {
|
||||
@Override
|
||||
public T read(StreamInput in) throws IOException {
|
||||
return handler.read(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleResponse(T response) {
|
||||
assertVersionSerializable(response);
|
||||
handler.handleResponse(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleException(TransportException exp) {
|
||||
handler.handleException(exp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executor() {
|
||||
return handler.executor();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -19,12 +19,7 @@
|
|||
|
||||
package org.elasticsearch.test.hamcrest;
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.io.stream.Streamable;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -34,32 +29,10 @@ import org.elasticsearch.test.RandomObjects;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.elasticsearch.test.VersionUtils.randomVersion;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertVersionSerializable;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
public class ElasticsearchAssertionsTests extends ESTestCase {
|
||||
public void testAssertVersionSerializableIsOkWithIllegalArgumentException() {
|
||||
Version version = randomVersion(random());
|
||||
NamedWriteableRegistry registry = new NamedWriteableRegistry(emptyList());
|
||||
Streamable testStreamable = new TestStreamable();
|
||||
|
||||
// Should catch the exception and do nothing.
|
||||
assertVersionSerializable(version, testStreamable, registry);
|
||||
}
|
||||
|
||||
public static class TestStreamable implements Streamable {
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
throw new IllegalArgumentException("Not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
public void testAssertXContentEquivalent() throws IOException {
|
||||
try (XContentBuilder original = JsonXContent.contentBuilder()) {
|
||||
|
|
Loading…
Reference in New Issue