From e1c40b4310bd6475af162b504ea22fbd8de0e847 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Wed, 16 Aug 2023 20:59:48 -0700 Subject: [PATCH] Fix flaky test by preventing duplicate keys in random mapping fields (#9184) * Prevent duplicate keys in random mapping fields Signed-off-by: Daniel Widdis * Aliases were already unique. Used while loop for indices to keep size Signed-off-by: Daniel Widdis --------- Signed-off-by: Daniel Widdis --- .../opensearch/index/RandomCreateIndexGenerator.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/framework/src/main/java/org/opensearch/index/RandomCreateIndexGenerator.java b/test/framework/src/main/java/org/opensearch/index/RandomCreateIndexGenerator.java index 8e4824a9d9b3b..f016d9450425d 100644 --- a/test/framework/src/main/java/org/opensearch/index/RandomCreateIndexGenerator.java +++ b/test/framework/src/main/java/org/opensearch/index/RandomCreateIndexGenerator.java @@ -40,6 +40,8 @@ import org.opensearch.core.xcontent.XContentBuilder; import java.io.IOException; +import java.util.HashSet; +import java.util.Set; import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; @@ -112,8 +114,12 @@ public static void randomMappingFields(XContentBuilder builder, boolean allowObj builder.startObject("properties"); int fieldsNo = randomIntBetween(0, 5); - for (int i = 0; i < fieldsNo; i++) { - builder.startObject(randomAlphaOfLength(5)); + Set uniqueFields = new HashSet<>(); + while (uniqueFields.size() < fieldsNo) { + uniqueFields.add(randomAlphaOfLength(5)); + } + for (String uniqueField : uniqueFields) { + builder.startObject(uniqueField); if (allowObjectField && randomBoolean()) { randomMappingFields(builder, false);