spa77k commented on code in PR #14884:
URL: https://github.com/apache/gravitino/pull/14884#discussion_r4560749780


##########
plugins/idp-basic/src/test/java/org/apache/gravitino/idp/storage/service/TestIdpMetaServiceMetrics.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.idp.storage.service;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import com.codahale.metrics.MetricRegistry;
+import java.io.IOException;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.exceptions.NotFoundException;
+import org.apache.gravitino.metrics.MethodMonitorAspect;
+import org.apache.gravitino.metrics.MetricsSystem;
+import org.apache.gravitino.metrics.source.MetricsSource;
+import org.aspectj.lang.Aspects;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/** Verifies that {@code @Monitored} methods in this plugin are woven and 
record metrics. */
+class TestIdpMetaServiceMetrics extends AbstractIdpMetaServiceTest {
+
+  private MethodMonitorAspect aspect;
+  private Object originalMetricsSystem;
+  private MetricsSource metricsSource;
+
+  @BeforeEach
+  void installMetricsSystem() throws IllegalAccessException {
+    // The aspect reads the metrics system once when it is created, so replace 
it for this test.
+    aspect = Aspects.aspectOf(MethodMonitorAspect.class);
+    originalMetricsSystem = FieldUtils.readField(aspect, "metricsSystem", 
true);
+    metricsSource = new 
MetricsSource(MetricsSource.GRAVITINO_RELATIONAL_STORE_METRIC_NAME) {};
+    MetricsSystem metricsSystem = new MetricsSystem();
+    metricsSystem.register(metricsSource);
+    FieldUtils.writeField(aspect, "metricsSystem", metricsSystem, true);

Review Comment:
   Thanks for the review. In 7288309ca I added `@VisibleForTesting public 
MetricsSystem replaceMetricsSystem(@Nullable MetricsSystem)` to 
`MethodMonitorAspect`. It returns the previous metrics system so the caller can 
restore it. It is public rather than package-private because the three tests 
live in different packages (`...storage.relational.service`, 
`...idp.storage.service`, `...maintenance.storage.service`). The Javadoc says 
not to call it from production code.
   
   The core, idp-basic and maintenance metric tests now use it instead of 
`FieldUtils`, and the restore stays in `@AfterEach`. I also moved the swap to 
the start of `@BeforeEach`, so a failure later in setup cannot make 
`@AfterEach` write null into the singleton.
   
   Related to the parallel-test concern, I made the field `volatile`, and 
`monitorMethod` now reads it once into a local, so a concurrent swap to null 
cannot cause an NPE between the null check and the use.
   
   The three tests pass locally on JDK 17 and `spotlessCheck` passes. CI for 
7288309ca has not run on my fork yet.
   



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to