Skip to content

Commit

Permalink
Remove ImmutableOpenMap from some server tests (#89864)
Browse files Browse the repository at this point in the history
Remove ImmutableOpenMap from some server tests

relates #86239
  • Loading branch information
thecoop authored Sep 8, 2022
1 parent 72b2bd9 commit 6807b69
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.AliasMetadata.Builder;
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.test.AbstractWireSerializingTestCase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

public class GetAliasesResponseTests extends AbstractWireSerializingTestCase<GetAliasesResponse> {
Expand All @@ -44,23 +44,21 @@ protected GetAliasesResponse mutateInstance(GetAliasesResponse response) {

private static Map<String, List<AliasMetadata>> mutateAliases(Map<String, List<AliasMetadata>> aliases) {
if (aliases.isEmpty()) {
return createIndicesAliasesMap(1, 3).build();
return Collections.unmodifiableMap(createIndicesAliasesMap(1, 3));
}

if (randomBoolean()) {
ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder(aliases);
ImmutableOpenMap<String, List<AliasMetadata>> list = createIndicesAliasesMap(1, 2).build();
list.entrySet().forEach(e -> builder.put(e.getKey(), e.getValue()));
return builder.build();
Map<String, List<AliasMetadata>> builder = new HashMap<>(aliases);
builder.putAll(createIndicesAliasesMap(1, 2));
return Collections.unmodifiableMap(builder);
}

Set<String> indices = aliases.keySet();
List<String> indicesToBeModified = randomSubsetOf(randomIntBetween(1, indices.size()), indices);
ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder();
List<String> indicesToBeModified = randomSubsetOf(randomIntBetween(1, aliases.size()), aliases.keySet());
Map<String, List<AliasMetadata>> builder = new HashMap<>();

for (String index : indices) {
List<AliasMetadata> list = new ArrayList<>(aliases.get(index));
if (indicesToBeModified.contains(index)) {
for (var alias : aliases.entrySet()) {
List<AliasMetadata> list = new ArrayList<>(alias.getValue());
if (indicesToBeModified.contains(alias.getKey())) {
if (randomBoolean() || list.isEmpty()) {
list.add(createAliasMetadata());
} else {
Expand All @@ -69,20 +67,20 @@ private static Map<String, List<AliasMetadata>> mutateAliases(Map<String, List<A
list.add(aliasIndex, mutateAliasMetadata(aliasMetadata));
}
}
builder.put(index, list);
builder.put(alias.getKey(), Collections.unmodifiableList(list));
}
return builder.build();
return Collections.unmodifiableMap(builder);
}

private static GetAliasesResponse createTestItem() {
return new GetAliasesResponse(
mutateAliases(createIndicesAliasesMap(0, 5).build()),
mutateAliases(createIndicesAliasesMap(0, 5)),
randomMap(5, 5, () -> new Tuple<>(randomAlphaOfLength(4), randomList(5, DataStreamTestHelper::randomAliasInstance)))
);
}

private static ImmutableOpenMap.Builder<String, List<AliasMetadata>> createIndicesAliasesMap(int min, int max) {
ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder();
private static Map<String, List<AliasMetadata>> createIndicesAliasesMap(int min, int max) {
Map<String, List<AliasMetadata>> builder = new HashMap<>();
int indicesNum = randomIntBetween(min, max);
for (int i = 0; i < indicesNum; i++) {
String index = randomAlphaOfLength(5);
Expand All @@ -91,7 +89,7 @@ private static ImmutableOpenMap.Builder<String, List<AliasMetadata>> createIndic
for (int alias = 0; alias < aliasesNum; alias++) {
aliasMetadata.add(createAliasMetadata());
}
builder.put(index, aliasMetadata);
builder.put(index, Collections.unmodifiableList(aliasMetadata));
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.AtomicArray;
import org.elasticsearch.common.util.concurrent.EsExecutors;
Expand Down Expand Up @@ -192,10 +191,7 @@ public void setupAction() {
remoteNode1 = mock(DiscoveryNode.class);
remoteNode2 = mock(DiscoveryNode.class);
nodes = mock(DiscoveryNodes.class);
ImmutableOpenMap<String, DiscoveryNode> ingestNodes = ImmutableOpenMap.<String, DiscoveryNode>builder(2)
.fPut("node1", remoteNode1)
.fPut("node2", remoteNode2)
.build();
Map<String, DiscoveryNode> ingestNodes = Map.of("node1", remoteNode1, "node2", remoteNode2);
when(nodes.getIngestNodes()).thenReturn(ingestNodes);
when(nodes.getMinNodeVersion()).thenReturn(VersionUtils.randomCompatibleVersion(random(), Version.CURRENT));
ClusterState state = mock(ClusterState.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;

Expand All @@ -31,19 +35,15 @@ protected DataStreamMetadata createTestInstance() {
if (randomBoolean()) {
return new DataStreamMetadata(ImmutableOpenMap.of(), ImmutableOpenMap.of());
}
ImmutableOpenMap.Builder<String, DataStream> dataStreams = ImmutableOpenMap.builder();
for (int i = 0; i < randomIntBetween(1, 5); i++) {
dataStreams.put(randomAlphaOfLength(5), DataStreamTestHelper.randomInstance());
}
Map<String, DataStream> dataStreams = IntStream.range(0, randomIntBetween(1, 5))
.boxed()
.collect(Collectors.toUnmodifiableMap(i -> randomAlphaOfLength(5), i -> DataStreamTestHelper.randomInstance()));

ImmutableOpenMap.Builder<String, DataStreamAlias> dataStreamsAliases = ImmutableOpenMap.builder();
if (randomBoolean()) {
for (int i = 0; i < randomIntBetween(1, 5); i++) {
DataStreamAlias alias = DataStreamTestHelper.randomAliasInstance();
dataStreamsAliases.put(alias.getName(), alias);
}
}
return new DataStreamMetadata(dataStreams.build(), dataStreamsAliases.build());
Map<String, DataStreamAlias> dataStreamsAliases = IntStream.range(0, randomIntBetween(1, 5))
.mapToObj(i -> DataStreamTestHelper.randomAliasInstance())
.collect(Collectors.toUnmodifiableMap(DataStreamAlias::getName, Function.identity()));

return new DataStreamMetadata(ImmutableOpenMap.builder(dataStreams).build(), ImmutableOpenMap.builder(dataStreamsAliases).build());
}

@Override
Expand Down

0 comments on commit 6807b69

Please sign in to comment.