KarmaGYZ commented on a change in pull request #15975:
URL: https://github.com/apache/flink/pull/15975#discussion_r637706827



##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/resourcemanager/slotmanager/FineGrainedSlotManagerTest.java
##########
@@ -948,4 +955,42 @@ private void testMaxTotalResourceExceededRegisterResource(
             }
         };
     }
+
+    @Test
+    public void testMetricsUnregisteredWhenSuspending() throws Exception {
+        testAccessMetricValueDuringItsUnregister(SlotManager::suspend);
+    }
+
+    @Test
+    public void testMetricsUnregisteredWhenClosing() throws Exception {
+        testAccessMetricValueDuringItsUnregister(AutoCloseable::close);
+    }
+
+    private void testAccessMetricValueDuringItsUnregister(
+            ThrowingConsumer<SlotManager, Exception> closeFn) throws Exception 
{
+        final AtomicInteger registeredMetrics = new AtomicInteger();
+        final MetricRegistry metricRegistry =
+                TestingMetricRegistry.builder()
+                        .setRegisterConsumer((a, b, c) -> 
registeredMetrics.incrementAndGet())
+                        .setUnregisterConsumer((a, b, c) -> 
registeredMetrics.decrementAndGet())
+                        .build();
+
+        final Context context = new Context();
+        context.setSlotManagerMetricGroup(
+                SlotManagerMetricGroup.create(metricRegistry, "localhost"));
+
+        context.runTest(
+                () ->
+                        context.runInMainThreadAndWait(
+                                () -> {
+                                    try {
+                                        // sanity check to ensure metrics were 
actually registered
+                                        assertThat(registeredMetrics.get(), 
greaterThan(0));
+                                        
closeFn.accept(context.getSlotManager());
+                                        assertThat(registeredMetrics.get(), 
is(0));
+                                    } catch (Exception e) {
+                                        fail("Error when closing slot 
manager.");
+                                    }

Review comment:
       Not sure is it a good way to do the sanity check in the 
`context.runInMainThreadAndWait`. The error from `assertThat` will not be 
logged.
   
   I think we can leverage a `CompleteFuture` to check whether the close 
function is done successfully and do the sanity check out of the main thread. 
Also, we do not need the `runInMainThreadAndWait` in that case.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to