edgent-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vdog...@apache.org
Subject incubator-quarks git commit: QUARKS-17 SingleMetricAbstractOplet always creates metric name
Date Wed, 09 Mar 2016 20:43:25 GMT
Repository: incubator-quarks
Updated Branches:
  refs/heads/master 5c4db58de -> 2ca0f8802


QUARKS-17 SingleMetricAbstractOplet always creates metric name


Project: http://git-wip-us.apache.org/repos/asf/incubator-quarks/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-quarks/commit/2ca0f880
Tree: http://git-wip-us.apache.org/repos/asf/incubator-quarks/tree/2ca0f880
Diff: http://git-wip-us.apache.org/repos/asf/incubator-quarks/diff/2ca0f880

Branch: refs/heads/master
Commit: 2ca0f8802533234a993b44ce33399054cff8febb
Parents: 5c4db58
Author: Victor Dogaru <vdogaru@apache.org>
Authored: Tue Mar 8 19:20:51 2016 -0800
Committer: Victor Dogaru <vdogaru@apache.org>
Committed: Tue Mar 8 19:23:21 2016 -0800

----------------------------------------------------------------------
 .../oplets/SingleMetricAbstractOplet.java       |   6 +-
 .../quarks/test/metrics/MetricsCommonTest.java  | 103 +++++++++++++++++++
 2 files changed, 108 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quarks/blob/2ca0f880/utils/metrics/src/main/java/quarks/metrics/oplets/SingleMetricAbstractOplet.java
----------------------------------------------------------------------
diff --git a/utils/metrics/src/main/java/quarks/metrics/oplets/SingleMetricAbstractOplet.java
b/utils/metrics/src/main/java/quarks/metrics/oplets/SingleMetricAbstractOplet.java
index 21b5e95..bd88f60 100644
--- a/utils/metrics/src/main/java/quarks/metrics/oplets/SingleMetricAbstractOplet.java
+++ b/utils/metrics/src/main/java/quarks/metrics/oplets/SingleMetricAbstractOplet.java
@@ -26,6 +26,10 @@ public abstract class SingleMetricAbstractOplet<T> extends Peek<T>
{
     /**
      * Returns the name of the metric used by this oplet for registration.
      * The name uniquely identifies the metric in the {@link MetricRegistry}.
+     * <p>
+     * The name of the metric is {@code null} prior to oplet initialization,
+     * or if this oplet has not been initialized with a 
+     * {@code MetricRegistry}.
      * 
      * @return the name of the metric used by this oplet.
      */
@@ -39,9 +43,9 @@ public abstract class SingleMetricAbstractOplet<T> extends Peek<T>
{
     public final void initialize(OpletContext<T, T> context) {
         super.initialize(context);
 
-        this.metricName = context.uniquify(shortMetricName);
         MetricRegistry registry = context.getService(MetricRegistry.class);
         if (registry != null) {
+            this.metricName = context.uniquify(shortMetricName);
             registry.register(getMetricName(), getMetric());
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-quarks/blob/2ca0f880/utils/metrics/src/test/java/quarks/test/metrics/MetricsCommonTest.java
----------------------------------------------------------------------
diff --git a/utils/metrics/src/test/java/quarks/test/metrics/MetricsCommonTest.java b/utils/metrics/src/test/java/quarks/test/metrics/MetricsCommonTest.java
index 17d86c1..0bf6c81 100644
--- a/utils/metrics/src/test/java/quarks/test/metrics/MetricsCommonTest.java
+++ b/utils/metrics/src/test/java/quarks/test/metrics/MetricsCommonTest.java
@@ -4,13 +4,25 @@
 */
 package quarks.test.metrics;
 
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.junit.Test;
 
+import com.codahale.metrics.MetricRegistry;
+
+import quarks.function.Consumer;
 import quarks.metrics.oplets.CounterOp;
 import quarks.metrics.oplets.RateMeter;
+import quarks.oplet.JobContext;
 import quarks.oplet.Oplet;
+import quarks.oplet.OpletContext;
 import quarks.oplet.core.AbstractOplet;
 import quarks.oplet.core.Peek;
 
@@ -28,4 +40,95 @@ public class MetricsCommonTest {
         assertTrue(AbstractOplet.class.isAssignableFrom(RateMeter.class));
         assertTrue(Peek.class.isAssignableFrom(RateMeter.class));
     }
+    
+    @Test
+    public void metricNameRateMeter() throws Exception {
+        Context<Object,Object> ctx = new Context<>();
+        ctx.addService(MetricRegistry.class, new MetricRegistry());
+        
+        RateMeter<Object> op = new RateMeter<>();
+        op.initialize(ctx);
+        assertNotNull(op.getMetricName());
+        op.close();
+    }
+
+    @Test
+    public void metricNullNameRateMeter() throws Exception {
+        Context<Object,Object> ctx = new Context<>();
+        RateMeter<Object> op = new RateMeter<>();
+        
+        op.initialize(ctx);
+        assertNull(op.getMetricName());
+        op.close();
+    }
+
+    @Test
+    public void metricNameCounter() throws Exception {
+        Context<Object,Object> ctx = new Context<>();
+        ctx.addService(MetricRegistry.class, new MetricRegistry());
+        
+        CounterOp<Object> op = new CounterOp<>();
+        op.initialize(ctx);
+        assertNotNull(op.getMetricName());
+        op.close();
+    }
+
+    @Test
+    public void metricNullNameCounter() throws Exception {
+        Context<Object,Object> ctx = new Context<>();
+        CounterOp<Object> op = new CounterOp<>();
+        
+        op.initialize(ctx);
+        assertNull(op.getMetricName());
+        op.close();
+    }
+
+    private static class Context<I, O> implements OpletContext<I, O> {
+        private final Map<Class<?>, Object> services = new HashMap<>();
+
+        public <T> T addService(Class<T> serviceClass, T service) {
+            return serviceClass.cast(services.put(serviceClass, service));
+        }
+
+        @Override
+        public String getId() {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public <T> T getService(Class<T> serviceClass) {
+            return serviceClass.cast(services.get(serviceClass));
+        }
+
+        @Override
+        public int getInputCount() {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public int getOutputCount() {
+            throw new UnsupportedOperationException();
+        }
+
+        @SuppressWarnings("serial")
+        @Override
+        public List<? extends Consumer<O>> getOutputs() {
+            List<Consumer<O>> outputs = new ArrayList<>();
+            outputs.add(0, new Consumer<O>() {
+                @Override
+                public void accept(O value) {}
+            });
+            return outputs;
+        }
+
+        @Override
+        public JobContext getJobContext() {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public String uniquify(String name) {
+            return "unique." + name;
+        }
+    }
 }


Mime
View raw message