44
44
import org .junit .jupiter .api .Test ;
45
45
import org .junit .jupiter .api .Timeout ;
46
46
47
+ import java .time .Duration ;
47
48
import java .util .HashMap ;
48
49
import java .util .List ;
49
50
import java .util .Map ;
50
51
import java .util .concurrent .CompletableFuture ;
51
52
import java .util .stream .Collectors ;
52
53
54
+ import static org .assertj .core .api .Assertions .assertThat ;
55
+ import static org .awaitility .Awaitility .await ;
53
56
import static org .junit .jupiter .api .Assertions .assertEquals ;
54
57
import static org .junit .jupiter .api .Assertions .assertThrows ;
55
58
import static org .junit .jupiter .api .Assertions .fail ;
56
59
57
60
/** Tests for {@link RestApiMetricsCollector}. */
58
- public class RestApiMetricsCollectorTest {
61
+ class RestApiMetricsCollectorTest {
59
62
60
63
private static final String GC_METRIC_NAME = "Status.JVM.GarbageCollector.All.TimeMsPerSecond" ;
61
64
private static final String HEAP_MAX_NAME = "Status.JVM.Memory.Heap.Max" ;
@@ -64,7 +67,7 @@ public class RestApiMetricsCollectorTest {
64
67
private static final String METASPACE_MEMORY_NAME = "Status.JVM.Memory.Metaspace.Used" ;
65
68
66
69
@ Test
67
- public void testAggregateMultiplePendingRecordsMetricsPerSource () throws Exception {
70
+ void testAggregateMultiplePendingRecordsMetricsPerSource () throws Exception {
68
71
var collector = new RestApiMetricsCollector <JobID , JobAutoScalerContext <JobID >>();
69
72
70
73
JobVertexID jobVertexID = new JobVertexID ();
@@ -145,7 +148,7 @@ CompletableFuture<P> sendRequest(
145
148
146
149
@ Test
147
150
@ Timeout (60 )
148
- public void testJmMetricCollection () throws Exception {
151
+ void testJmMetricCollection () throws Exception {
149
152
try (MiniCluster miniCluster =
150
153
new MiniCluster (
151
154
new MiniClusterConfiguration .Builder ()
@@ -160,36 +163,38 @@ public void testJmMetricCollection() throws Exception {
160
163
(c , e ) ->
161
164
new StandaloneClientHAServices (
162
165
miniCluster .getRestAddress ().get ().toString ()));
163
- do {
164
- var collector = new RestApiMetricsCollector <>();
165
- Map <FlinkMetric , Metric > flinkMetricMetricMap =
166
- collector .queryJmMetrics (
167
- client ,
168
- Map .of (
169
- "taskSlotsTotal" , FlinkMetric .NUM_TASK_SLOTS_TOTAL ,
170
- "taskSlotsAvailable" ,
171
- FlinkMetric .NUM_TASK_SLOTS_AVAILABLE ));
172
- try {
173
- assertEquals (
174
- "3" ,
175
- flinkMetricMetricMap .get (FlinkMetric .NUM_TASK_SLOTS_TOTAL ).getValue ());
176
- assertEquals (
177
- "3" ,
178
- flinkMetricMetricMap
179
- .get (FlinkMetric .NUM_TASK_SLOTS_AVAILABLE )
180
- .getValue ());
181
- break ;
182
- } catch (NullPointerException e ) {
183
- // Metrics might not be available yet (timeout above will eventually kill this
184
- // test)
185
- Thread .sleep (100 );
186
- }
187
- } while (true );
166
+ var collector = new RestApiMetricsCollector <>();
167
+ Map <FlinkMetric , Metric > flinkMetricMetricMap = new HashMap <>();
168
+ // Metrics might not be available yet so retry the query until it returns results or the
169
+ // timeout reached.
170
+ await ().atMost (Duration .ofSeconds (60 ))
171
+ .until (
172
+ () -> {
173
+ final Map <FlinkMetric , Metric > results =
174
+ collector .queryJmMetrics (
175
+ client ,
176
+ Map .of (
177
+ "taskSlotsTotal" ,
178
+ FlinkMetric .NUM_TASK_SLOTS_TOTAL ,
179
+ "taskSlotsAvailable" ,
180
+ FlinkMetric .NUM_TASK_SLOTS_AVAILABLE ));
181
+ flinkMetricMetricMap .putAll (results );
182
+ return !results .isEmpty ();
183
+ });
184
+
185
+ assertThat (flinkMetricMetricMap )
186
+ .hasSize (2 )
187
+ .hasEntrySatisfying (
188
+ FlinkMetric .NUM_TASK_SLOTS_TOTAL ,
189
+ metricValue -> assertMetricValueIs (metricValue , 3 ))
190
+ .hasEntrySatisfying (
191
+ FlinkMetric .NUM_TASK_SLOTS_AVAILABLE ,
192
+ metricValue -> assertMetricValueIs (metricValue , 3 ));
188
193
}
189
194
}
190
195
191
196
@ Test
192
- public void testTmMetricCollection () throws Exception {
197
+ void testTmMetricCollection () throws Exception {
193
198
194
199
var metricValues = new HashMap <String , AggregatedMetric >();
195
200
@@ -309,4 +314,8 @@ private static void assertMetricsEquals(
309
314
assertEquals (v .getSum (), a .getSum (), k .name ());
310
315
});
311
316
}
317
+
318
+ private static void assertMetricValueIs (Metric metricValue , int expected ) {
319
+ assertThat (metricValue .getValue ()).asInt ().isEqualTo (expected );
320
+ }
312
321
}
0 commit comments