Skip to content

Commit 535ba6b

Browse files
authored
Test utility for BytesReference equality (#138668)
Today if you assert two `BytesReference` objects are equal, and they aren't, then the test output is unhelpful because `BytesReference` instances do not include their contents in their default string representations. This commit introduces a new matcher specifically for testing `BytesReference` instances for equality which generates more useful output.
1 parent bb1cb40 commit 535ba6b

File tree

64 files changed

+370
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+370
-172
lines changed

modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.stream.IntStream;
5151

5252
import static org.apache.lucene.tests.util.TestUtil.randomSimpleString;
53+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
5354
import static org.elasticsearch.core.TimeValue.timeValueSeconds;
5455
import static org.hamcrest.Matchers.instanceOf;
5556

@@ -174,7 +175,7 @@ private SearchResponse createSearchResponse() {
174175
private void assertSameHits(List<? extends ScrollableHitSource.Hit> actual, SearchHit[] expected) {
175176
assertEquals(actual.size(), expected.length);
176177
for (int i = 0; i < actual.size(); ++i) {
177-
assertEquals(actual.get(i).getSource(), expected[i].getSourceRef());
178+
assertThat(expected[i].getSourceRef(), equalBytes(actual.get(i).getSource()));
178179
assertEquals(actual.get(i).getIndex(), expected[i].getIndex());
179180
assertEquals(actual.get(i).getVersion(), expected[i].getVersion());
180181
assertEquals(actual.get(i).getPrimaryTerm(), expected[i].getPrimaryTerm());

modules/reindex/src/test/java/org/elasticsearch/reindex/RoundTripTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Map;
3939

4040
import static org.apache.lucene.tests.util.TestUtil.randomSimpleString;
41+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
4142

4243
/**
4344
* Round trip tests for all {@link Writeable} things declared in this plugin.
@@ -144,7 +145,7 @@ private void assertRequestEquals(ReindexRequest request, ReindexRequest tripped)
144145
assertNotNull(tripped.getRemoteInfo());
145146
assertEquals(request.getRemoteInfo().getScheme(), tripped.getRemoteInfo().getScheme());
146147
assertEquals(request.getRemoteInfo().getHost(), tripped.getRemoteInfo().getHost());
147-
assertEquals(request.getRemoteInfo().getQuery(), tripped.getRemoteInfo().getQuery());
148+
assertThat(tripped.getRemoteInfo().getQuery(), equalBytes(request.getRemoteInfo().getQuery()));
148149
assertEquals(request.getRemoteInfo().getUsername(), tripped.getRemoteInfo().getUsername());
149150
assertEquals(request.getRemoteInfo().getPassword(), tripped.getRemoteInfo().getPassword());
150151
assertEquals(request.getRemoteInfo().getHeaders(), tripped.getRemoteInfo().getHeaders());

modules/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.util.Collections;
6060
import java.util.Map;
6161

62+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
6263
import static org.elasticsearch.common.io.Streams.readFully;
6364
import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose;
6465
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.CREDENTIALS_FILE_SETTING;
@@ -219,7 +220,7 @@ public void testWriteFileMultipleOfChunkSize() throws IOException {
219220
container.writeBlob(randomPurpose(), key, new BytesArray(initialValue), true);
220221

221222
BytesReference reference = readFully(container.readBlob(randomPurpose(), key));
222-
assertEquals(new BytesArray(initialValue), reference);
223+
assertThat(reference, equalBytes(new BytesArray(initialValue)));
223224

224225
container.deleteBlobsIgnoringIfNotExists(randomPurpose(), Iterators.single(key));
225226
}

modules/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979

8080
import static fixture.gcs.TestUtils.createServiceAccount;
8181
import static java.nio.charset.StandardCharsets.UTF_8;
82+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
8283
import static org.elasticsearch.common.io.Streams.readFully;
8384
import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose;
8485
import static org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase.randomBytes;
@@ -582,7 +583,7 @@ public void testCompareAndExchangeWhenThrottled() throws IOException {
582583
final OptionalBytesReference updateResult = safeAwait(
583584
l -> container.compareAndExchangeRegister(randomPurpose(), key, new BytesArray(data), new BytesArray(updatedData), l)
584585
);
585-
assertEquals(new BytesArray(data), updateResult.bytesReference());
586+
assertThat(updateResult.bytesReference(), equalBytes(new BytesArray(data)));
586587

587588
assertEquals(0, requestHandlers.size());
588589
container.delete(randomPurpose());
@@ -601,7 +602,7 @@ public void testContentsChangeWhileStreaming() throws IOException {
601602
container.writeBlob(randomPurpose(), key, new BytesArray(initialValue), true);
602603

603604
BytesReference reference = readFully(container.readBlob(randomPurpose(), key));
604-
assertEquals(new BytesArray(initialValue), reference);
605+
assertThat(reference, equalBytes(new BytesArray(initialValue)));
605606

606607
try (InputStream inputStream = container.readBlob(randomPurpose(), key)) {
607608
// Trigger the first chunk to load

modules/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerStatsTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.Map;
5151
import java.util.concurrent.TimeUnit;
5252

53+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
5354
import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose;
5455
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.APPLICATION_NAME_SETTING;
5556
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.CONNECT_TIMEOUT_SETTING;
@@ -138,7 +139,7 @@ public void testSingleMultipartWrite() throws Exception {
138139
final StatsMap wantStats = new StatsMap(purpose);
139140
assertStatsEquals(wantStats.add(INSERT, 1), store.stats());
140141
try (InputStream is = container.readBlob(purpose, blobName)) {
141-
assertEquals(blobContents, Streams.readFully(is));
142+
assertThat(Streams.readFully(is), equalBytes(blobContents));
142143
}
143144
assertStatsEquals(wantStats.add(GET, 1), store.stats());
144145
}
@@ -164,7 +165,7 @@ public void testResumableWrite() throws Exception {
164165
assertStatsEquals(wantStats.add(INSERT, 1, totalRequests), store.stats());
165166

166167
try (InputStream is = container.readBlob(purpose, blobName)) {
167-
assertEquals(blobContents, Streams.readFully(is));
168+
assertThat(Streams.readFully(is), equalBytes(blobContents));
168169
}
169170
assertStatsEquals(wantStats.add(GET, 1), store.stats());
170171
}

modules/repository-s3/qa/third-party/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryThirdPartyTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.concurrent.TimeUnit;
5050
import java.util.concurrent.atomic.AtomicLong;
5151

52+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
5253
import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose;
5354
import static org.hamcrest.Matchers.allOf;
5455
import static org.hamcrest.Matchers.blankOrNullString;
@@ -194,7 +195,7 @@ List<MultipartUpload> listMultipartUploads() {
194195
assertTrue(testHarness.tryCompareAndSet(BytesArray.EMPTY, bytes1));
195196

196197
// show we're looking at the right blob
197-
assertEquals(bytes1, testHarness.readRegister());
198+
assertThat(testHarness.readRegister(), equalBytes(bytes1));
198199
assertArrayEquals(
199200
bytes1.array(),
200201
client.getObject(GetObjectRequest.builder().bucket(bucketName).key(registerBlobPath).build()).readAllBytes()
@@ -217,7 +218,7 @@ List<MultipartUpload> listMultipartUploads() {
217218
timeOffsetMillis.addAndGet(blobStore.getCompareAndExchangeTimeToLive().millis() - Math.min(0, age));
218219
assertTrue(testHarness.tryCompareAndSet(bytes1, bytes2));
219220
assertThat(testHarness.listMultipartUploads(), hasSize(0));
220-
assertEquals(bytes2, testHarness.readRegister());
221+
assertThat(testHarness.readRegister(), equalBytes(bytes2));
221222
} finally {
222223
blobContainer.delete(randomPurpose());
223224
}

modules/rest-root/src/test/java/org/elasticsearch/rest/root/RestMainActionTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.HashMap;
2626
import java.util.Map;
2727

28+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
2829
import static org.hamcrest.Matchers.equalTo;
2930
import static org.hamcrest.Matchers.greaterThan;
3031

@@ -95,6 +96,6 @@ public void testGetResponse() throws Exception {
9596
}
9697
mainResponse.toXContent(responseBuilder, ToXContent.EMPTY_PARAMS);
9798
BytesReference xcontentBytes = BytesReference.bytes(responseBuilder);
98-
assertEquals(xcontentBytes, response.content());
99+
assertThat(response.content(), equalBytes(xcontentBytes));
99100
}
100101
}

modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandlerTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
7474
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
75+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
7576
import static org.hamcrest.Matchers.empty;
7677
import static org.hamcrest.Matchers.equalTo;
7778
import static org.hamcrest.Matchers.greaterThan;
@@ -548,7 +549,7 @@ private static void assertChunkedMessageAtIndex(List<Object> messagesSeen, int i
548549
}
549550

550551
private static void assertContentAtIndexEquals(List<Object> messagesSeen, int index, BytesReference single) {
551-
assertEquals(Netty4Utils.toBytesReference(((ByteBufHolder) messagesSeen.get(index)).content()), single);
552+
assertThat(single, equalBytes(Netty4Utils.toBytesReference(((ByteBufHolder) messagesSeen.get(index)).content())));
552553
}
553554

554555
private static void assertDoneWithClosedChannel(ChannelPromise chunkedWritePromise) {

server/src/internalClusterTest/java/org/elasticsearch/action/search/PointInTimeIT.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.concurrent.TimeUnit;
5555
import java.util.stream.Collectors;
5656

57+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
5758
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
5859
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFailures;
5960
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
@@ -95,7 +96,7 @@ public void testBasic() {
9596
refresh("test");
9697
BytesReference pitId = openPointInTime(new String[] { "test" }, TimeValue.timeValueMinutes(2)).getPointInTimeId();
9798
assertResponse(prepareSearch().setPointInTime(new PointInTimeBuilder(pitId)), resp1 -> {
98-
assertThat(resp1.pointInTimeId(), equalTo(pitId));
99+
assertThat(resp1.pointInTimeId(), equalBytes(pitId));
99100
assertHitCount(resp1, numDocs);
100101
});
101102
int deletedDocs = 0;
@@ -119,7 +120,7 @@ public void testBasic() {
119120
prepareSearch().setQuery(new MatchAllQueryBuilder()).setPointInTime(new PointInTimeBuilder(pitId)),
120121
resp3 -> {
121122
assertHitCount(resp3, numDocs);
122-
assertThat(resp3.pointInTimeId(), equalTo(pitId));
123+
assertThat(resp3.pointInTimeId(), equalBytes(pitId));
123124
}
124125
);
125126
} finally {
@@ -154,7 +155,7 @@ public void testIndexWithFilteredAlias() {
154155
.setSize(0)
155156
.setQuery(new MatchAllQueryBuilder()),
156157
resp1 -> {
157-
assertThat(resp1.pointInTimeId(), equalTo(pitId));
158+
assertThat(resp1.pointInTimeId(), equalBytes(pitId));
158159
assertHitCount(resp1, finalCountTagA);
159160
}
160161
);
@@ -181,7 +182,7 @@ public void testMultipleIndices() {
181182
assertNoFailuresAndResponse(prepareSearch().setPointInTime(new PointInTimeBuilder(pitId)), resp -> {
182183
assertHitCount(resp, numDocs);
183184
assertNotNull(resp.pointInTimeId());
184-
assertThat(resp.pointInTimeId(), equalTo(pitId));
185+
assertThat(resp.pointInTimeId(), equalBytes(pitId));
185186
for (int i = 0; i < moreDocs; i++) {
186187
String id = "more-" + i;
187188
String index = "index-" + randomIntBetween(1, numIndices);
@@ -193,7 +194,7 @@ public void testMultipleIndices() {
193194
assertNoFailuresAndResponse(prepareSearch().setPointInTime(new PointInTimeBuilder(pitId)), resp -> {
194195
assertHitCount(resp, numDocs);
195196
assertNotNull(resp.pointInTimeId());
196-
assertThat(resp.pointInTimeId(), equalTo(pitId));
197+
assertThat(resp.pointInTimeId(), equalBytes(pitId));
197198
});
198199
} finally {
199200
closePointInTime(pitId);
@@ -237,7 +238,7 @@ public void testIndexFilter() {
237238
assertNoFailuresAndResponse(prepareSearch().setPointInTime(new PointInTimeBuilder(pitId)).setSize(50), resp -> {
238239
assertHitCount(resp, numDocs);
239240
assertNotNull(resp.pointInTimeId());
240-
assertThat(resp.pointInTimeId(), equalTo(pitId));
241+
assertThat(resp.pointInTimeId(), equalBytes(pitId));
241242
for (SearchHit hit : resp.getHits()) {
242243
assertEquals("index-3", hit.getIndex());
243244
}
@@ -261,7 +262,7 @@ public void testRelocation() throws Exception {
261262
try {
262263
assertNoFailuresAndResponse(prepareSearch().setPointInTime(new PointInTimeBuilder(pitId)), resp -> {
263264
assertHitCount(resp, numDocs);
264-
assertThat(resp.pointInTimeId(), equalTo(pitId));
265+
assertThat(resp.pointInTimeId(), equalBytes(pitId));
265266
});
266267
final Set<String> dataNodes = clusterService().state()
267268
.nodes()
@@ -281,7 +282,7 @@ public void testRelocation() throws Exception {
281282
}
282283
assertNoFailuresAndResponse(prepareSearch().setPointInTime(new PointInTimeBuilder(pitId)), resp -> {
283284
assertHitCount(resp, numDocs);
284-
assertThat(resp.pointInTimeId(), equalTo(pitId));
285+
assertThat(resp.pointInTimeId(), equalBytes(pitId));
285286
});
286287
assertBusy(() -> {
287288
final Set<String> assignedNodes = clusterService().state()
@@ -294,7 +295,7 @@ public void testRelocation() throws Exception {
294295
}, 30, TimeUnit.SECONDS);
295296
assertNoFailuresAndResponse(prepareSearch().setPointInTime(new PointInTimeBuilder(pitId)), resp -> {
296297
assertHitCount(resp, numDocs);
297-
assertThat(resp.pointInTimeId(), equalTo(pitId));
298+
assertThat(resp.pointInTimeId(), equalBytes(pitId));
298299
});
299300
} finally {
300301
closePointInTime(pitId);
@@ -464,13 +465,13 @@ public void testPartialResults() throws Exception {
464465
try {
465466
assertNoFailuresAndResponse(prepareSearch().setPointInTime(new PointInTimeBuilder(pitId)), resp -> {
466467
assertHitCount(resp, numDocs1 + numDocs2);
467-
assertThat(resp.pointInTimeId(), equalTo(pitId));
468+
assertThat(resp.pointInTimeId(), equalBytes(pitId));
468469
});
469470

470471
internalCluster().restartNode(assignedNodeForIndex1);
471472
assertResponse(prepareSearch().setAllowPartialSearchResults(true).setPointInTime(new PointInTimeBuilder(pitId)), resp -> {
472473
assertFailures(resp);
473-
assertThat(resp.pointInTimeId(), equalTo(pitId));
474+
assertThat(resp.pointInTimeId(), equalBytes(pitId));
474475
assertHitCount(resp, numDocs2);
475476
});
476477
} finally {
@@ -619,7 +620,7 @@ public void testMissingShardsWithPointInTime() throws Exception {
619620
.setPointInTime(new PointInTimeBuilder(pointInTimeResponse.getPointInTimeId())),
620621
resp -> {
621622
// ensure that al docs are returned
622-
assertThat(resp.pointInTimeId(), equalTo(pointInTimeResponse.getPointInTimeId()));
623+
assertThat(resp.pointInTimeId(), equalBytes(pointInTimeResponse.getPointInTimeId()));
623624
assertHitCount(resp, numDocs);
624625
}
625626
);
@@ -672,7 +673,7 @@ public void testMissingShardsWithPointInTime() throws Exception {
672673
resp -> {
673674
assertThat(resp.getSuccessfulShards(), equalTo(numShards - shardsRemoved));
674675
assertThat(resp.getFailedShards(), equalTo(shardsRemoved));
675-
assertThat(resp.pointInTimeId(), equalTo(pointInTimeResponseOneNodeDown.getPointInTimeId()));
676+
assertThat(resp.pointInTimeId(), equalBytes(pointInTimeResponseOneNodeDown.getPointInTimeId()));
676677
assertNotNull(resp.getHits().getTotalHits());
677678
assertThat(resp.getHits().getTotalHits().value(), lessThan((long) numDocs));
678679
}
@@ -707,7 +708,7 @@ public void testMissingShardsWithPointInTime() throws Exception {
707708
prepareSearch().setQuery(new MatchAllQueryBuilder())
708709
.setPointInTime(new PointInTimeBuilder(pointInTimeResponseOneNodeDown.getPointInTimeId())),
709710
resp -> {
710-
assertThat(resp.pointInTimeId(), equalTo(pointInTimeResponseOneNodeDown.getPointInTimeId()));
711+
assertThat(resp.pointInTimeId(), equalBytes(pointInTimeResponseOneNodeDown.getPointInTimeId()));
711712
assertThat(resp.getTotalShards(), equalTo(numShards));
712713
assertThat(resp.getSuccessfulShards(), equalTo(numShards - shardsRemoved));
713714
assertThat(resp.getFailedShards(), equalTo(shardsRemoved));

server/src/internalClusterTest/java/org/elasticsearch/get/GetActionIT.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.function.UnaryOperator;
6161

6262
import static java.util.Collections.singleton;
63+
import static org.elasticsearch.common.bytes.BytesReferenceTestUtils.equalBytes;
6364
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
6465
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
6566
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
@@ -1026,9 +1027,9 @@ public void testRealTimeGetNestedFields() {
10261027
assertTrue(lucene1.isExists());
10271028
assertTrue(lucene2.isExists());
10281029
assertTrue(lucene3.isExists());
1029-
assertThat(translog1.getSourceAsBytesRef(), equalTo(lucene1.getSourceAsBytesRef()));
1030-
assertThat(translog2.getSourceAsBytesRef(), equalTo(lucene2.getSourceAsBytesRef()));
1031-
assertThat(translog3.getSourceAsBytesRef(), equalTo(lucene3.getSourceAsBytesRef()));
1030+
assertThat(translog1.getSourceAsBytesRef(), equalBytes(lucene1.getSourceAsBytesRef()));
1031+
assertThat(translog2.getSourceAsBytesRef(), equalBytes(lucene2.getSourceAsBytesRef()));
1032+
assertThat(translog3.getSourceAsBytesRef(), equalBytes(lucene3.getSourceAsBytesRef()));
10321033
}
10331034

10341035
private void assertGetFieldsAlwaysWorks(String index, String docId, String[] fields) {

0 commit comments

Comments
 (0)