aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/test/java
diff options
context:
space:
mode:
authorBogdan Drutu <bdrutu@google.com>2018-08-14 17:28:24 -0700
committerGitHub <noreply@github.com>2018-08-14 17:28:24 -0700
commitf50596ac77fd0c4eb209ffbc8769b5025a3aa090 (patch)
tree38434828044be22f6eab76ac0d3d4756893775a1 /impl_core/src/test/java
parent8d01189b45122350d37b9685c1225bce6b3b20a2 (diff)
downloadopencensus-java-f50596ac77fd0c4eb209ffbc8769b5025a3aa090.tar.gz
Add implementation for gauges. (#1365)
* Add implementation for gauges. * Remove usage of internal class in implcore tests.
Diffstat (limited to 'impl_core/src/test/java')
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java113
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java335
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java9
3 files changed, 456 insertions, 1 deletions
diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java
new file mode 100644
index 00000000..1f6a1881
--- /dev/null
+++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * Licensed 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 io.opencensus.implcore.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import io.opencensus.common.Timestamp;
+import io.opencensus.common.ToDoubleFunction;
+import io.opencensus.common.ToLongFunction;
+import io.opencensus.implcore.metrics.Gauge.DoubleGauge;
+import io.opencensus.implcore.metrics.Gauge.LongGauge;
+import io.opencensus.metrics.LabelKey;
+import io.opencensus.metrics.LabelValue;
+import io.opencensus.metrics.Metric;
+import io.opencensus.metrics.MetricDescriptor;
+import io.opencensus.metrics.MetricDescriptor.Type;
+import io.opencensus.metrics.Point;
+import io.opencensus.metrics.TimeSeriesGauge;
+import io.opencensus.metrics.TimeSeriesList;
+import io.opencensus.metrics.Value;
+import io.opencensus.testing.common.TestClock;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link Gauge}. */
+@RunWith(JUnit4.class)
+public class GaugeTest {
+ private static final String NAME = "name";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final List<LabelKey> LABEL_KEYS =
+ Collections.unmodifiableList(
+ Collections.singletonList(LabelKey.create("key", "key description")));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.unmodifiableList(Collections.singletonList(LabelValue.create("value")));
+ private static final Object OBJ = new Object();
+ private static final Timestamp TEST_TIME = Timestamp.create(1234, 123);
+
+ private final Gauge longGauge =
+ new LongGauge<Object>(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ LABEL_KEYS,
+ LABEL_VALUES,
+ OBJ,
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return value.hashCode();
+ }
+ });
+ private final Gauge doubleGauge =
+ new DoubleGauge<Object>(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ LABEL_KEYS,
+ LABEL_VALUES,
+ OBJ,
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return value.hashCode();
+ }
+ });
+ private final TestClock testClock = TestClock.create(TEST_TIME);
+
+ @Test
+ public void longGauge_GetMetric() {
+ assertThat(longGauge.getMetric(testClock))
+ .isEqualTo(
+ Metric.create(
+ MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_INT64, LABEL_KEYS),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ LABEL_VALUES,
+ Collections.singletonList(
+ Point.create(Value.longValue(OBJ.hashCode()), TEST_TIME)))))));
+ }
+
+ @Test
+ public void doubleGauge_GetMetric() {
+ assertThat(doubleGauge.getMetric(testClock))
+ .isEqualTo(
+ Metric.create(
+ MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, LABEL_KEYS),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ LABEL_VALUES,
+ Collections.singletonList(
+ Point.create(Value.doubleValue(OBJ.hashCode()), TEST_TIME)))))));
+ }
+}
diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
new file mode 100644
index 00000000..fdab47aa
--- /dev/null
+++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
@@ -0,0 +1,335 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * Licensed 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 io.opencensus.implcore.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import io.opencensus.common.Timestamp;
+import io.opencensus.common.ToDoubleFunction;
+import io.opencensus.common.ToLongFunction;
+import io.opencensus.metrics.LabelKey;
+import io.opencensus.metrics.LabelValue;
+import io.opencensus.metrics.Metric;
+import io.opencensus.metrics.MetricDescriptor;
+import io.opencensus.metrics.MetricDescriptor.Type;
+import io.opencensus.metrics.Point;
+import io.opencensus.metrics.TimeSeriesGauge;
+import io.opencensus.metrics.TimeSeriesList;
+import io.opencensus.metrics.Value;
+import io.opencensus.testing.common.TestClock;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link MetricRegistryImpl}. */
+@RunWith(JUnit4.class)
+public class MetricRegistryImplTest {
+ private static final String NAME = "name";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final LabelKey LABEL_KEY = LabelKey.create("key", "key description");
+ private static final LabelValue LABEL_VALUES = LabelValue.create("value");
+ private static final Timestamp TEST_TIME = Timestamp.create(1234, 123);
+
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ private final TestClock testClock = TestClock.create(TEST_TIME);
+ private final MetricRegistryImpl metricRegistry = new MetricRegistryImpl(testClock);
+ private final LinkedHashMap<LabelKey, LabelValue> labels =
+ new LinkedHashMap<LabelKey, LabelValue>();
+
+ @Before
+ public void setUp() {
+ labels.put(LABEL_KEY, LABEL_VALUES);
+ }
+
+ @Test
+ public void addDoubleGauge_NullName() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addDoubleGauge(
+ null,
+ DESCRIPTION,
+ UNIT,
+ labels,
+ null,
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return 5.0;
+ }
+ });
+ }
+
+ @Test
+ public void addDoubleGauge_NullDescription() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addDoubleGauge(
+ NAME,
+ null,
+ UNIT,
+ labels,
+ null,
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return 5.0;
+ }
+ });
+ }
+
+ @Test
+ public void addDoubleGauge_NullUnit() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addDoubleGauge(
+ NAME,
+ DESCRIPTION,
+ null,
+ labels,
+ null,
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return 5.0;
+ }
+ });
+ }
+
+ @Test
+ public void addDoubleGauge_NullLabels() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addDoubleGauge(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ null,
+ null,
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return 5.0;
+ }
+ });
+ }
+
+ @Test
+ public void addDoubleGauge_NullFunction() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addDoubleGauge(NAME, DESCRIPTION, UNIT, labels, null, null);
+ }
+
+ @Test
+ public void addDoubleGauge_GetMetrics() {
+ metricRegistry.addDoubleGauge(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ labels,
+ null,
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return 5.0;
+ }
+ });
+ assertThat(metricRegistry.getMetrics())
+ .containsExactly(
+ Metric.create(
+ MetricDescriptor.create(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ Type.GAUGE_DOUBLE,
+ Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
+ Collections.singletonList(
+ Point.create(Value.doubleValue(5.0), TEST_TIME)))))));
+ }
+
+ @Test
+ public void addLongGauge_NullName() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addLongGauge(
+ null,
+ DESCRIPTION,
+ UNIT,
+ labels,
+ null,
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return 7;
+ }
+ });
+ }
+
+ @Test
+ public void addLongGauge_NullDescription() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addLongGauge(
+ NAME,
+ null,
+ UNIT,
+ labels,
+ null,
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return 5;
+ }
+ });
+ }
+
+ @Test
+ public void addLongGauge_NullUnit() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addLongGauge(
+ NAME,
+ DESCRIPTION,
+ null,
+ labels,
+ null,
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return 5;
+ }
+ });
+ }
+
+ @Test
+ public void addLongGauge_NullLabels() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addLongGauge(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ null,
+ null,
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return 5;
+ }
+ });
+ }
+
+ @Test
+ public void addLongGauge_NullFunction() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addLongGauge("name", DESCRIPTION, UNIT, labels, null, null);
+ }
+
+ @Test
+ public void addLongGauge_GetMetrics() {
+ metricRegistry.addLongGauge(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ labels,
+ null,
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return 7;
+ }
+ });
+ assertThat(metricRegistry.getMetrics())
+ .containsExactly(
+ Metric.create(
+ MetricDescriptor.create(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ Type.GAUGE_INT64,
+ Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
+ Collections.singletonList(
+ Point.create(Value.longValue(7), TEST_TIME)))))));
+ }
+
+ @Test
+ public void multipleMetrics_GetMetrics() {
+ metricRegistry.addLongGauge(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ labels,
+ null,
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return 7;
+ }
+ });
+ metricRegistry.addDoubleGauge(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ labels,
+ null,
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return 5.0;
+ }
+ });
+ assertThat(metricRegistry.getMetrics())
+ .containsExactly(
+ Metric.create(
+ MetricDescriptor.create(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ Type.GAUGE_INT64,
+ Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
+ Collections.singletonList(
+ Point.create(Value.longValue(7), TEST_TIME)))))),
+ Metric.create(
+ MetricDescriptor.create(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ Type.GAUGE_DOUBLE,
+ Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
+ Collections.singletonList(
+ Point.create(Value.doubleValue(5.0), TEST_TIME)))))));
+ }
+
+ @Test
+ public void empty_GetMetrics() {
+ assertThat(metricRegistry.getMetrics()).isEmpty();
+ }
+}
diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java
index a86a53d9..5050625b 100644
--- a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java
+++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java
@@ -19,6 +19,7 @@ package io.opencensus.implcore.metrics;
import static com.google.common.truth.Truth.assertThat;
import io.opencensus.implcore.metrics.export.ExportComponentImpl;
+import io.opencensus.testing.common.TestClock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -26,11 +27,17 @@ import org.junit.runners.JUnit4;
/** Unit tests for {@link MetricsComponentImplBase}. */
@RunWith(JUnit4.class)
public class MetricsComponentImplBaseTest {
+ private final MetricsComponentImplBase metricsComponentImplBase =
+ new MetricsComponentImplBase(TestClock.create());
@Test
public void getExportComponent() {
- MetricsComponentImplBase metricsComponentImplBase = new MetricsComponentImplBase();
assertThat(metricsComponentImplBase.getExportComponent())
.isInstanceOf(ExportComponentImpl.class);
}
+
+ @Test
+ public void getMetricRegistry() {
+ assertThat(metricsComponentImplBase.getMetricRegistry()).isInstanceOf(MetricRegistryImpl.class);
+ }
}