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 <[email protected]>

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

Signed-off-by: Gao Binlong <[email protected]>

* Add a new randomFieldName method

Signed-off-by: Gao Binlong <[email protected]>

---------

Signed-off-by: Gao Binlong <[email protected]>
  • Loading branch information
gaobinlong authored and harshavamsi committed Apr 29, 2024
1 parent e6d7b02 commit ca31485
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 ca31485

Please sign in to comment.