From d96cdfc8e75a9a186f6203095a8c27e018b94fd1 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 15 Aug 2017 14:11:13 -0400 Subject: [PATCH] Fix document field equals and hash code test For the document field equals and hash code tests, we try to mutate the document field to intentionally produce a document field not equal to our provided one. We do this by randomly choosing a document field that has either - a randomly chosen field name and the same field value as the provided document field - a randomly chosen field value and the same field value as the provided document field If we are unlucky, it can be that the document field chosen by this method can be equal to the provided document field. In this case, our test will fail because the mutation really should be not equal. In this case, we should simply try the other mutation. Note that random document field produced by the second method can be equal to the provided document because it has the same field name and we can get unlucky with our randomly chosen field values. It is not the case that the random document field produced by the first method can be equal to the provided document field; this is because the current implementation guarantees that the field name length will be different guaranteeing that we have a different field name. Nevertheless, we fix the issue here by checking that our random choice gives us a non-equal document field, and assert that if we got unlucky the other one will work for us. --- .../elasticsearch/index/get/DocumentFieldTests.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java b/core/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java index 0b8549e005367..9d581054f46b8 100644 --- a/core/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java +++ b/core/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java @@ -86,7 +86,16 @@ private static DocumentField mutateDocumentField(DocumentField documentField) { List> mutations = new ArrayList<>(); mutations.add(() -> new DocumentField(randomUnicodeOfCodepointLength(15), documentField.getValues())); mutations.add(() -> new DocumentField(documentField.getName(), randomDocumentField(XContentType.JSON).v1().getValues())); - return randomFrom(mutations).get(); + final int index = randomFrom(0, 1); + final DocumentField randomCandidate = mutations.get(index).get(); + if (!documentField.equals(randomCandidate)) { + return randomCandidate; + } else { + // we are unlucky and our random mutation is equal to our mutation, try the other candidate + final DocumentField otherCandidate = mutations.get(1 - index).get(); + assert !documentField.equals(otherCandidate) : documentField; + return otherCandidate; + } } public static Tuple randomDocumentField(XContentType xContentType) {