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) (#9406)

* Prevent duplicate keys in random mapping fields



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



---------


(cherry picked from commit e1c40b4)

Signed-off-by: Daniel Widdis <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 4cb5e66 commit d555d1d
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 d555d1d

Please sign in to comment.