Skip to content

Commit 3b10a06

Browse files
authored
Fix flaky tests in CopyProcessorTests (opensearch-project#12885)
* 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>
1 parent f6d6fd3 commit 3b10a06

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

modules/ingest-common/src/test/java/org/opensearch/ingest/common/CopyProcessorTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class CopyProcessorTests extends OpenSearchTestCase {
2424
public void testCopyExistingField() throws Exception {
2525
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
2626
String sourceFieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
27-
String targetFieldName = RandomDocumentPicks.randomFieldName(random());
27+
String targetFieldName = RandomDocumentPicks.randomNonExistingFieldName(random(), ingestDocument);
2828
Processor processor = createCopyProcessor(sourceFieldName, targetFieldName, false, false, false);
2929
processor.execute(ingestDocument);
3030
assertThat(ingestDocument.hasField(targetFieldName), equalTo(true));
@@ -71,7 +71,8 @@ public void testCopyWithIgnoreMissing() throws Exception {
7171
public void testCopyWithRemoveSource() throws Exception {
7272
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
7373
String sourceFieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
74-
String targetFieldName = RandomDocumentPicks.randomFieldName(random());
74+
String targetFieldName = RandomDocumentPicks.randomNonExistingFieldName(random(), ingestDocument);
75+
7576
Object sourceValue = ingestDocument.getFieldValue(sourceFieldName, Object.class);
7677

7778
Processor processor = createCopyProcessor(sourceFieldName, targetFieldName, false, true, false);

test/framework/src/main/java/org/opensearch/ingest/RandomDocumentPicks.java

+11
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ public static String randomFieldName(Random random) {
7171
return fieldName.toString();
7272
}
7373

74+
/**
75+
* Returns a random field name that doesn't exist in the document.
76+
*/
77+
public static String randomNonExistingFieldName(Random random, IngestDocument ingestDocument) {
78+
String fieldName;
79+
do {
80+
fieldName = randomFieldName(random);
81+
} while (canAddField(fieldName, ingestDocument) == false);
82+
return fieldName;
83+
}
84+
7485
/**
7586
* Returns a random leaf field name.
7687
*/

0 commit comments

Comments
 (0)