Skip to content

Commit

Permalink
Fix flaky tests in CopyProcessorTests (opensearch-project#12885)
Browse files Browse the repository at this point in the history
* Fix flaky test CopyProcessorTests#testCopyWithRemoveSource

Signed-off-by: Gao Binlong <gbinlong@amazon.com>

* Remove an existing field to get a non-existing field name

Signed-off-by: Gao Binlong <gbinlong@amazon.com>

* Add a new randomFieldName method

Signed-off-by: Gao Binlong <gbinlong@amazon.com>

---------

Signed-off-by: Gao Binlong <gbinlong@amazon.com>
Signed-off-by: Shivansh Arora <hishiv@amazon.com>
  • Loading branch information
gaobinlong authored and shiv0408 committed Apr 25, 2024
1 parent b0cae9c commit bd20612
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CopyProcessorTests extends OpenSearchTestCase {
public void testCopyExistingField() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
String sourceFieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
String targetFieldName = RandomDocumentPicks.randomFieldName(random());
String targetFieldName = RandomDocumentPicks.randomNonExistingFieldName(random(), ingestDocument);
Processor processor = createCopyProcessor(sourceFieldName, targetFieldName, false, false, false);
processor.execute(ingestDocument);
assertThat(ingestDocument.hasField(targetFieldName), equalTo(true));
Expand Down Expand Up @@ -71,7 +71,8 @@ public void testCopyWithIgnoreMissing() throws Exception {
public void testCopyWithRemoveSource() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
String sourceFieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
String targetFieldName = RandomDocumentPicks.randomFieldName(random());
String targetFieldName = RandomDocumentPicks.randomNonExistingFieldName(random(), ingestDocument);

Object sourceValue = ingestDocument.getFieldValue(sourceFieldName, Object.class);

Processor processor = createCopyProcessor(sourceFieldName, targetFieldName, false, true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public static String randomFieldName(Random random) {
return fieldName.toString();
}

/**
* Returns a random field name that doesn't exist in the document.
*/
public static String randomNonExistingFieldName(Random random, IngestDocument ingestDocument) {
String fieldName;
do {
fieldName = randomFieldName(random);
} while (canAddField(fieldName, ingestDocument) == false);
return fieldName;
}

/**
* Returns a random leaf field name.
*/
Expand Down

0 comments on commit bd20612

Please sign in to comment.