Skip to content

Commit

Permalink
Fix flaky test by preventing duplicate keys in random mapping fields (o…
Browse files Browse the repository at this point in the history
…pensearch-project#9184)

* Prevent duplicate keys in random mapping fields

Signed-off-by: Daniel Widdis <widdis@gmail.com>

* Aliases were already unique. Used while loop for indices to keep size

Signed-off-by: Daniel Widdis <widdis@gmail.com>

---------

Signed-off-by: Daniel Widdis <widdis@gmail.com>
Signed-off-by: Kaushal Kumar <ravi.kaushal97@gmail.com>
  • Loading branch information
dbwiddis authored and kaushalmahi12 committed Sep 12, 2023
1 parent 1ffd7ee commit 74c9e82
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> uniqueFields = new HashSet<>();
while (uniqueFields.size() < fieldsNo) {
uniqueFields.add(randomAlphaOfLength(5));
}
for (String uniqueField : uniqueFields) {
builder.startObject(uniqueField);

if (allowObjectField && randomBoolean()) {
randomMappingFields(builder, false);
Expand Down

0 comments on commit 74c9e82

Please sign in to comment.