Skip to content

Commit 92290aa

Browse files
Fix test in TransportGetAllocationStatsActionTests (#124902)
testReturnsOnlyRequestedStats() was using EnumSet.copyOf(randomSubsetOf(Metric.values().length, Metric.values())) which will always return all of the metrics. The code was expecting Metric.ALLOCATIONS and Metric.FS to sometimes not be in the returned set. This change uses an explicit list of EnumSets to cover the range of scenarios expected in the test code.
1 parent f0ee146 commit 92290aa

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetAllocationStatsActionTests.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.Before;
3030

3131
import java.util.EnumSet;
32+
import java.util.List;
3233
import java.util.Map;
3334
import java.util.Set;
3435
import java.util.concurrent.CyclicBarrier;
@@ -40,7 +41,7 @@
4041
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
4142
import static org.hamcrest.Matchers.not;
4243
import static org.mockito.Mockito.mock;
43-
import static org.mockito.Mockito.never;
44+
import static org.mockito.Mockito.times;
4445
import static org.mockito.Mockito.verify;
4546
import static org.mockito.Mockito.when;
4647

@@ -87,33 +88,43 @@ public void tearDown() throws Exception {
8788
}
8889

8990
public void testReturnsOnlyRequestedStats() throws Exception {
91+
int expectedNumberOfStatsServiceCalls = 0;
92+
93+
for (final var metrics : List.of(
94+
EnumSet.of(Metric.ALLOCATIONS, Metric.FS),
95+
EnumSet.of(Metric.ALLOCATIONS),
96+
EnumSet.of(Metric.FS),
97+
EnumSet.noneOf(Metric.class),
98+
EnumSet.allOf(Metric.class),
99+
EnumSet.copyOf(randomSubsetOf(between(1, Metric.values().length), EnumSet.allOf(Metric.class)))
100+
)) {
101+
var request = new TransportGetAllocationStatsAction.Request(
102+
TimeValue.ONE_MINUTE,
103+
new TaskId(randomIdentifier(), randomNonNegativeLong()),
104+
metrics
105+
);
106+
107+
when(allocationStatsService.stats()).thenReturn(
108+
Map.of(randomIdentifier(), NodeAllocationStatsTests.randomNodeAllocationStats())
109+
);
110+
111+
var future = new PlainActionFuture<TransportGetAllocationStatsAction.Response>();
112+
action.masterOperation(mock(Task.class), request, ClusterState.EMPTY_STATE, future);
113+
var response = future.get();
114+
115+
if (metrics.contains(Metric.ALLOCATIONS)) {
116+
assertThat(response.getNodeAllocationStats(), not(anEmptyMap()));
117+
verify(allocationStatsService, times(++expectedNumberOfStatsServiceCalls)).stats();
118+
} else {
119+
assertThat(response.getNodeAllocationStats(), anEmptyMap());
120+
verify(allocationStatsService, times(expectedNumberOfStatsServiceCalls)).stats();
121+
}
90122

91-
var metrics = EnumSet.copyOf(randomSubsetOf(Metric.values().length, Metric.values()));
92-
93-
var request = new TransportGetAllocationStatsAction.Request(
94-
TimeValue.ONE_MINUTE,
95-
new TaskId(randomIdentifier(), randomNonNegativeLong()),
96-
metrics
97-
);
98-
99-
when(allocationStatsService.stats()).thenReturn(Map.of(randomIdentifier(), NodeAllocationStatsTests.randomNodeAllocationStats()));
100-
101-
var future = new PlainActionFuture<TransportGetAllocationStatsAction.Response>();
102-
action.masterOperation(mock(Task.class), request, ClusterState.EMPTY_STATE, future);
103-
var response = future.get();
104-
105-
if (metrics.contains(Metric.ALLOCATIONS)) {
106-
assertThat(response.getNodeAllocationStats(), not(anEmptyMap()));
107-
verify(allocationStatsService).stats();
108-
} else {
109-
assertThat(response.getNodeAllocationStats(), anEmptyMap());
110-
verify(allocationStatsService, never()).stats();
111-
}
112-
113-
if (metrics.contains(Metric.FS)) {
114-
assertNotNull(response.getDiskThresholdSettings());
115-
} else {
116-
assertNull(response.getDiskThresholdSettings());
123+
if (metrics.contains(Metric.FS)) {
124+
assertNotNull(response.getDiskThresholdSettings());
125+
} else {
126+
assertNull(response.getDiskThresholdSettings());
127+
}
117128
}
118129
}
119130

0 commit comments

Comments
 (0)