Skip to content

Commit

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

* Prevent duplicate keys in random mapping fields

Signed-off-by: Daniel Widdis <[email protected]>

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

Signed-off-by: Daniel Widdis <[email protected]>

---------

Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis authored Aug 17, 2023
1 parent f3957ea commit e1c40b4
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 e1c40b4

Please sign in to comment.