gnodet-bot commented on code in PR #29057:
URL: https://github.com/apache/camel/pull/29057#discussion_r4454741176


##########
components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampTest.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.camel.component.micrometer.eventnotifier;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import io.micrometer.core.instrument.Gauge;
+import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
+import io.micrometer.core.instrument.search.RequiredSearch;
+import io.micrometer.core.instrument.search.Search;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.micrometer.MicrometerConstants;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Proves that the current implementation does not perform a 
MeterRegistry.find() lookup on every exchange completion
+ * via setLastTimeExchange(). The test wraps the registry in a counting spy 
and asserts that after N completed exchanges
+ * the find() call count is 0 — i.e. the gauge is registered once at startup 
and never looked up again on the hot path.
+ */
+public class MicrometerExchangeEventNotifierLastTimestampTest extends 
AbstractMicrometerEventNotifierTest {
+
+    private static final String DIRECT_IN = "direct://in";
+    private static final String MOCK_OUT = "mock://out";
+    private static final String ROUTE_ID = "test";
+
+    /** Counts calls to find(CAMEL_EXCHANGE_LAST_TIME_METER_NAME) made after 
context startup. */
+    private final AtomicInteger findCallsAfterStart = new AtomicInteger(0);
+
+    @Override
+    public void addRegistry() {
+        meterRegistry = new 
CountingCompositeMeterRegistry(findCallsAfterStart);
+    }
+
+    @Override
+    protected AbstractMicrometerEventNotifier<?> getEventNotifier() {
+        return new MicrometerExchangeEventNotifier();
+    }
+
+    @Test
+    public void testLastTimestampGaugeIsNotLookedUpOnEveryExchange() throws 
Exception {
+        int count = 5;
+        MockEndpoint mock = getMockEndpoint(MOCK_OUT);
+        mock.expectedMessageCount(count);
+
+        // Reset counter after startup so we only measure hot-path lookups
+        findCallsAfterStart.set(0);
+
+        for (int i = 0; i < count; i++) {
+            template.sendBody(DIRECT_IN, "msg-" + i);
+        }
+        mock.assertIsSatisfied();
+
+        // The gauge should have been registered once at doStart() — never 
queried again per exchange.
+        assertThat(findCallsAfterStart.get())
+                .as("MeterRegistry.find() for the last-timestamp gauge must 
not be called on every exchange")
+                .isEqualTo(0);
+
+        // Sanity: the gauge itself must exist and hold a value > 0
+        Gauge gauge = 
meterRegistry.find(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME).gauge();
+        assertThat(gauge).as("last-timestamp gauge must be 
registered").isNotNull();

Review Comment:
   💡 **Nit (still open):** The `// Sanity:` comment explains the purpose of the 
`find()` call but not that it also increments the spy counter after the 
hot-path assertion already passed. Suggested clarification:
   
   ```suggestion
           // Sanity: the gauge itself must exist and hold a value > 0
           // (note: this find() call increments findCallsAfterStart, but the 
hot-path assertion above already passed)
           Gauge gauge = 
meterRegistry.find(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME).gauge();
   ```



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