Skip to content

Commit b561746

Browse files
Fix test in TransportGetAllocationStatsActionTests
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 49254b0 commit b561746

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

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

Lines changed: 37 additions & 26 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;
@@ -41,6 +42,7 @@
4142
import static org.hamcrest.Matchers.not;
4243
import static org.mockito.Mockito.mock;
4344
import static org.mockito.Mockito.never;
45+
import static org.mockito.Mockito.times;
4446
import static org.mockito.Mockito.verify;
4547
import static org.mockito.Mockito.when;
4648

@@ -87,33 +89,42 @@ public void tearDown() throws Exception {
8789
}
8890

8991
public void testReturnsOnlyRequestedStats() throws Exception {
92+
int expectedNumberOfStatsServiceCalls = 0;
93+
94+
for (final var metrics : List.of(
95+
EnumSet.of(Metric.ALLOCATIONS, Metric.FS),
96+
EnumSet.of(Metric.ALLOCATIONS),
97+
EnumSet.of(Metric.FS),
98+
EnumSet.noneOf(Metric.class),
99+
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)