justinrsweeney commented on code in PR #948:
URL: https://github.com/apache/solr/pull/948#discussion_r930444571


##########
solr/core/src/test/org/apache/solr/metrics/DelegateRegistryTimerTest.java:
##########
@@ -0,0 +1,218 @@
+/*
+ * 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.solr.metrics;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import com.codahale.metrics.Clock;
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.Timer;
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DelegateRegistryTimerTest {
+
+  MetricRegistry.MetricSupplier<Timer> timerSupplier =
+      new MetricSuppliers.DefaultTimerSupplier(null);
+
+  @Test
+  public void update() {
+    DelegateRegistryTimer delegateRegistryTimer =
+        new DelegateRegistryTimer(
+            Clock.defaultClock(), timerSupplier.newMetric(), 
timerSupplier.newMetric());
+    delegateRegistryTimer.update(Duration.ofSeconds(10));
+    assertEquals(1, delegateRegistryTimer.getPrimaryTimer().getCount());
+    assertEquals(
+        10000000000.0, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMean(), 0.0);
+    assertEquals(
+        10000000000.0, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMedian(), 0.0);
+    assertEquals(10000000000L, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMax());
+    assertEquals(10000000000.0, delegateRegistryTimer.getSnapshot().getMean(), 
0.0);
+    assertEquals(10000000000.0, 
delegateRegistryTimer.getSnapshot().getMedian(), 0.0);
+    assertEquals(10000000000L, delegateRegistryTimer.getSnapshot().getMax());
+    assertEquals(1, delegateRegistryTimer.getDelegateTimer().getCount());
+    assertEquals(
+        10000000000.0, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMean(), 0.0);
+    assertEquals(
+        10000000000.0, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMedian(), 0.0);
+    assertEquals(10000000000L, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMax());
+
+    delegateRegistryTimer.update(Duration.ofSeconds(20));
+    delegateRegistryTimer.update(Duration.ofSeconds(30));
+    assertEquals(3, delegateRegistryTimer.getPrimaryTimer().getCount());
+    assertEquals(
+        20000000000.0, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMean(), 0.0);
+    assertEquals(
+        20000000000.0, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMedian(), 0.0);
+    assertEquals(30000000000L, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMax());
+    assertEquals(20000000000.0, delegateRegistryTimer.getSnapshot().getMean(), 
0.0);
+    assertEquals(20000000000.0, 
delegateRegistryTimer.getSnapshot().getMedian(), 0.0);
+    assertEquals(30000000000L, delegateRegistryTimer.getSnapshot().getMax());
+    assertEquals(3, delegateRegistryTimer.getDelegateTimer().getCount());
+    assertEquals(
+        20000000000.0, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMean(), 0.0);
+    assertEquals(
+        20000000000.0, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMedian(), 0.0);
+    assertEquals(30000000000L, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMax());
+  }
+
+  @Test
+  public void testUpdate() {
+    DelegateRegistryTimer delegateRegistryTimer =
+        new DelegateRegistryTimer(
+            Clock.defaultClock(), timerSupplier.newMetric(), 
timerSupplier.newMetric());
+    delegateRegistryTimer.update(10, TimeUnit.SECONDS);
+    assertEquals(1, delegateRegistryTimer.getPrimaryTimer().getCount());
+    assertEquals(
+        10000000000.0, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMean(), 0.0);
+    assertEquals(
+        10000000000.0, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMedian(), 0.0);
+    assertEquals(10000000000L, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMax());
+    assertEquals(10000000000.0, delegateRegistryTimer.getSnapshot().getMean(), 
0.0);
+    assertEquals(10000000000.0, 
delegateRegistryTimer.getSnapshot().getMedian(), 0.0);
+    assertEquals(10000000000L, delegateRegistryTimer.getSnapshot().getMax());
+    assertEquals(1, delegateRegistryTimer.getDelegateTimer().getCount());
+    assertEquals(
+        10000000000.0, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMean(), 0.0);
+    assertEquals(
+        10000000000.0, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMedian(), 0.0);
+    assertEquals(10000000000L, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMax());
+
+    delegateRegistryTimer.update(20, TimeUnit.SECONDS);
+    delegateRegistryTimer.update(30, TimeUnit.SECONDS);
+    assertEquals(3, delegateRegistryTimer.getPrimaryTimer().getCount());
+    assertEquals(
+        20000000000.0, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMean(), 0.0);
+    assertEquals(
+        20000000000.0, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMedian(), 0.0);
+    assertEquals(30000000000L, 
delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMax());
+    assertEquals(20000000000.0, delegateRegistryTimer.getSnapshot().getMean(), 
0.0);
+    assertEquals(20000000000.0, 
delegateRegistryTimer.getSnapshot().getMedian(), 0.0);
+    assertEquals(30000000000L, delegateRegistryTimer.getSnapshot().getMax());
+    assertEquals(3, delegateRegistryTimer.getDelegateTimer().getCount());
+    assertEquals(
+        20000000000.0, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMean(), 0.0);
+    assertEquals(
+        20000000000.0, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMedian(), 0.0);
+    assertEquals(30000000000L, 
delegateRegistryTimer.getDelegateTimer().getSnapshot().getMax());
+  }
+
+  @Test
+  public void timeContext() throws InterruptedException {
+    DelegateRegistryTimer delegateRegistryTimer =
+        new DelegateRegistryTimer(
+            Clock.defaultClock(), timerSupplier.newMetric(), 
timerSupplier.newMetric());
+    Timer.Context time = delegateRegistryTimer.time();
+    Thread.sleep(100);
+    time.close();
+    assertTrue(delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMean() 
> 100000);
+    
assertTrue(delegateRegistryTimer.getDelegateTimer().getSnapshot().getMean() > 
100000);
+    assertEquals(
+        delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMean(),
+        delegateRegistryTimer.getDelegateTimer().getSnapshot().getMean(),
+        0.0);
+    assertEquals(
+        delegateRegistryTimer.getPrimaryTimer().getSnapshot().getMean(),
+        delegateRegistryTimer.getSnapshot().getMean(),
+        0.0);
+  }
+
+  @Test
+  public void timeSupplier() {
+    DelegateRegistryTimer delegateRegistryTimer =
+        new DelegateRegistryTimer(
+            Clock.defaultClock(), timerSupplier.newMetric(), 
timerSupplier.newMetric());
+    Long supplierResult =
+        delegateRegistryTimer.timeSupplier(
+            () -> {
+              try {
+                Thread.sleep(100);
+              } catch (InterruptedException e) {
+                Assert.fail("Thread was interrupted while sleeping");

Review Comment:
   I took a different approach here that should just avoid that potential.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to