|
29 | 29 | import org.junit.Before; |
30 | 30 |
|
31 | 31 | import java.util.EnumSet; |
| 32 | +import java.util.List; |
32 | 33 | import java.util.Map; |
33 | 34 | import java.util.Set; |
34 | 35 | import java.util.concurrent.CyclicBarrier; |
|
41 | 42 | import static org.hamcrest.Matchers.not; |
42 | 43 | import static org.mockito.Mockito.mock; |
43 | 44 | import static org.mockito.Mockito.never; |
| 45 | +import static org.mockito.Mockito.times; |
44 | 46 | import static org.mockito.Mockito.verify; |
45 | 47 | import static org.mockito.Mockito.when; |
46 | 48 |
|
@@ -87,33 +89,42 @@ public void tearDown() throws Exception { |
87 | 89 | } |
88 | 90 |
|
89 | 91 | 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 | + } |
90 | 122 |
|
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 | + } |
117 | 128 | } |
118 | 129 | } |
119 | 130 |
|
|
0 commit comments