-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XContentTests : insert random fields at random positions #30867
Changes from 4 commits
690a878
c8d83ac
31de42c
d6d28f5
85b9b32
a854b23
ed83d40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.elasticsearch.test; | ||
|
||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class AbstractXContentTestCaseTests extends ESTestCase { | ||
|
||
public void testInsertRandomFieldsAndShuffle() throws IOException { | ||
TestInstance t = new TestInstance(); | ||
boolean randomFieldAtFirstPosition = false; | ||
for (int i = 0; i < 5; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Five repetitions is unfortunately not enough to always hit a case where the randomization kicks in. I ran the test with 100 repetitions and it failed 2 times out of that. Too much for out CI. I also wouldn't say the number should be increased. However, it would be great to "fix" the randomness in this particular case, since we are testing a test, but want it to behave in a specific way (do the shuffling). I did some digging in LuceneTestCase and fortunately found this little gem: RandomizedContext#runWithPrivateRandomness(). This seems to overwrite the test suite source of randomness for the time of invocation of a Callable, which in our case would be enough to make the shuffling always have some effect. We could fix the seed like this and avoid the loop alltogether:
Note that with differnt seed (like e.g. 0) the test consistently fails, so we should use a "good" one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cbuescher thanks for digging into this ;) |
||
BytesReference insertRandomFieldsAndShuffle = AbstractXContentTestCase.insertRandomFieldsAndShuffle(t, XContentType.JSON, true, | ||
new String[] {}, null, this::createParser, ToXContent.EMPTY_PARAMS); | ||
try (XContentParser parser = createParser(XContentType.JSON.xContent(), insertRandomFieldsAndShuffle)) { | ||
Map<String, Object> mapOrdered = parser.mapOrdered(); | ||
assertThat(mapOrdered.size(), equalTo(2)); | ||
if (false == "field".equals(mapOrdered.keySet().iterator().next())) { | ||
randomFieldAtFirstPosition = true; | ||
} | ||
} | ||
} | ||
assertThat(randomFieldAtFirstPosition, equalTo(true)); | ||
} | ||
|
||
private class TestInstance implements ToXContentObject { | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
{ | ||
builder.field("field", 1); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be package private please?