aboutsummaryrefslogtreecommitdiff
path: root/api/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/test')
-rw-r--r--api/src/test/java/io/opencensus/metrics/DistributionTest.java29
-rw-r--r--api/src/test/java/io/opencensus/metrics/PointTest.java2
-rw-r--r--api/src/test/java/io/opencensus/metrics/SummaryTest.java189
-rw-r--r--api/src/test/java/io/opencensus/metrics/ValueTest.java35
4 files changed, 236 insertions, 19 deletions
diff --git a/api/src/test/java/io/opencensus/metrics/DistributionTest.java b/api/src/test/java/io/opencensus/metrics/DistributionTest.java
index d511e317..5ee88dd6 100644
--- a/api/src/test/java/io/opencensus/metrics/DistributionTest.java
+++ b/api/src/test/java/io/opencensus/metrics/DistributionTest.java
@@ -32,7 +32,7 @@ import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
-/** Unit tests for {@link Value}. */
+/** Unit tests for {@link Distribution}. */
@RunWith(JUnit4.class)
public class DistributionTest {
@@ -40,6 +40,7 @@ public class DistributionTest {
private static final Timestamp TIMESTAMP = Timestamp.create(1, 0);
private static final Map<String, String> ATTACHMENTS = Collections.singletonMap("key", "value");
+ private static final double TOLERANCE = 1e-6;
@Test
public void createAndGet_Bucket() {
@@ -66,7 +67,7 @@ public class DistributionTest {
@Test
public void createAndGet_Exemplar() {
Exemplar exemplar = Exemplar.create(-9.9, TIMESTAMP, ATTACHMENTS);
- assertThat(exemplar.getValue()).isEqualTo(-9.9);
+ assertThat(exemplar.getValue()).isWithin(TOLERANCE).of(-9.9);
assertThat(exemplar.getTimestamp()).isEqualTo(TIMESTAMP);
assertThat(exemplar.getAttachments()).isEqualTo(ATTACHMENTS);
}
@@ -78,10 +79,10 @@ public class DistributionTest {
List<Bucket> buckets =
Arrays.asList(
Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4, exemplar));
- Distribution distribution = Distribution.create(6.6, 10, 678.54, bucketBounds, buckets);
- assertThat(distribution.getMean()).isEqualTo(6.6);
+ Distribution distribution = Distribution.create(10, 6.6, 678.54, bucketBounds, buckets);
assertThat(distribution.getCount()).isEqualTo(10);
- assertThat(distribution.getSumOfSquaredDeviations()).isEqualTo(678.54);
+ assertThat(distribution.getSum()).isWithin(TOLERANCE).of(6.6);
+ assertThat(distribution.getSumOfSquaredDeviations()).isWithin(TOLERANCE).of(678.54);
assertThat(distribution.getBucketBoundaries())
.containsExactlyElementsIn(bucketBounds)
.inOrder();
@@ -125,7 +126,7 @@ public class DistributionTest {
Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("count should be non-negative.");
- Distribution.create(6.6, -10, 678.54, bucketBounds, buckets);
+ Distribution.create(-10, 6.6, 678.54, bucketBounds, buckets);
}
@Test
@@ -135,7 +136,7 @@ public class DistributionTest {
Arrays.asList(Bucket.create(0), Bucket.create(0), Bucket.create(0), Bucket.create(0));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("sum of squared deviations should be non-negative.");
- Distribution.create(6.6, 0, -678.54, bucketBounds, buckets);
+ Distribution.create(0, 6.6, -678.54, bucketBounds, buckets);
}
@Test
@@ -144,8 +145,8 @@ public class DistributionTest {
List<Bucket> buckets =
Arrays.asList(Bucket.create(0), Bucket.create(0), Bucket.create(0), Bucket.create(0));
thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("mean should be 0 if count is 0.");
- Distribution.create(6.6, 0, 0, bucketBounds, buckets);
+ thrown.expectMessage("sum should be 0 if count is 0.");
+ Distribution.create(0, 6.6, 0, bucketBounds, buckets);
}
@Test
@@ -164,7 +165,7 @@ public class DistributionTest {
Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4));
thrown.expect(NullPointerException.class);
thrown.expectMessage("bucketBoundaries list should not be null.");
- Distribution.create(6.6, 10, 678.54, null, buckets);
+ Distribution.create(10, 6.6, 678.54, null, buckets);
}
@Test
@@ -174,7 +175,7 @@ public class DistributionTest {
Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("bucket boundaries not sorted.");
- Distribution.create(6.6, 10, 678.54, bucketBounds, buckets);
+ Distribution.create(10, 6.6, 678.54, bucketBounds, buckets);
}
@Test
@@ -182,7 +183,7 @@ public class DistributionTest {
List<Double> bucketBounds = Arrays.asList(-1.0, 0.0, 1.0);
thrown.expect(NullPointerException.class);
thrown.expectMessage("bucket list should not be null.");
- Distribution.create(6.6, 10, 678.54, bucketBounds, null);
+ Distribution.create(10, 6.6, 678.54, bucketBounds, null);
}
@Test
@@ -192,7 +193,7 @@ public class DistributionTest {
Arrays.asList(Bucket.create(3), Bucket.create(1), null, Bucket.create(4));
thrown.expect(NullPointerException.class);
thrown.expectMessage("bucket should not be null.");
- Distribution.create(6.6, 10, 678.54, bucketBounds, buckets);
+ Distribution.create(10, 6.6, 678.54, bucketBounds, buckets);
}
@Test
@@ -215,7 +216,7 @@ public class DistributionTest {
Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4))))
.addEqualityGroup(
Distribution.create(
- -7,
+ 7,
10,
23.456,
Arrays.asList(-5.0, 0.0, 5.0),
diff --git a/api/src/test/java/io/opencensus/metrics/PointTest.java b/api/src/test/java/io/opencensus/metrics/PointTest.java
index cb6175c1..708814c5 100644
--- a/api/src/test/java/io/opencensus/metrics/PointTest.java
+++ b/api/src/test/java/io/opencensus/metrics/PointTest.java
@@ -35,8 +35,8 @@ public class PointTest {
private static final Value DISTRIBUTION_VALUE =
Value.distributionValue(
Distribution.create(
- 6.6,
10,
+ 6.6,
678.54,
Arrays.asList(-1.0, 0.0, 1.0),
Arrays.asList(
diff --git a/api/src/test/java/io/opencensus/metrics/SummaryTest.java b/api/src/test/java/io/opencensus/metrics/SummaryTest.java
new file mode 100644
index 00000000..0b70d94e
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/SummaryTest.java
@@ -0,0 +1,189 @@
+/*
+ * 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.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import io.opencensus.metrics.Summary.Snapshot;
+import io.opencensus.metrics.Summary.Snapshot.ValueAtPercentile;
+import java.util.Collections;
+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 Summary}. */
+@RunWith(JUnit4.class)
+public class SummaryTest {
+
+ @Rule public final ExpectedException thrown = ExpectedException.none();
+ private static final double TOLERANCE = 1e-6;
+
+ @Test
+ public void createAndGet_ValueAtPercentile() {
+ ValueAtPercentile valueAtPercentile = ValueAtPercentile.create(99.5, 10.2);
+ assertThat(valueAtPercentile.getPercentile()).isWithin(TOLERANCE).of(99.5);
+ assertThat(valueAtPercentile.getValue()).isWithin(TOLERANCE).of(10.2);
+ }
+
+ @Test
+ public void createValueAtPercentile_InvalidValueAtPercentileInterval() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("percentile must be in the interval (0.0, 100.0]");
+ ValueAtPercentile.create(100.1, 10.2);
+ }
+
+ @Test
+ public void createValueAtPercentile_NegativeValue() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("value must be non-negative");
+ ValueAtPercentile.create(99.5, -10.2);
+ }
+
+ @Test
+ public void createAndGet_Snapshot() {
+ Snapshot snapshot =
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ assertThat(snapshot.getCount()).isEqualTo(10);
+ assertThat(snapshot.getSum()).isWithin(TOLERANCE).of(87.07);
+ assertThat(snapshot.getValueAtPercentiles())
+ .containsExactly(ValueAtPercentile.create(99.5, 10.2));
+ }
+
+ @Test
+ public void createAndGet_Snapshot_WithNullCountAndSum() {
+ Snapshot snapshot =
+ Snapshot.create(
+ null, null, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ assertThat(snapshot.getCount()).isNull();
+ assertThat(snapshot.getSum()).isNull();
+ assertThat(snapshot.getValueAtPercentiles())
+ .containsExactly(ValueAtPercentile.create(99.5, 10.2));
+ }
+
+ @Test
+ public void createSnapshot_NegativeCount() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("count must be non-negative");
+ Snapshot.create(-10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ }
+
+ @Test
+ public void createSnapshot_NegativeSum() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum must be non-negative");
+ Snapshot.create(10L, -87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ }
+
+ @Test
+ public void createSnapshot_ZeroCountAndNonZeroSum() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum must be 0 if count is 0");
+ Snapshot.create(0L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ }
+
+ @Test
+ public void createSnapshot_NullValueAtPercentilesList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("valueAtPercentiles");
+ Snapshot.create(10L, 87.07, null);
+ }
+
+ @Test
+ public void createSnapshot_OneNullValueAtPercentile() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("value in valueAtPercentiles");
+ Snapshot.create(10L, 87.07, Collections.<ValueAtPercentile>singletonList(null));
+ }
+
+ @Test
+ public void createAndGet_Summary() {
+ Snapshot snapshot =
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ Summary summary = Summary.create(10L, 6.6, snapshot);
+ assertThat(summary.getCount()).isEqualTo(10);
+ assertThat(summary.getSum()).isWithin(TOLERANCE).of(6.6);
+ assertThat(summary.getSnapshot()).isEqualTo(snapshot);
+ }
+
+ @Test
+ public void createSummary_NegativeCount() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("count must be non-negative");
+ Summary.create(
+ -10L, 6.6, Snapshot.create(null, null, Collections.<ValueAtPercentile>emptyList()));
+ }
+
+ @Test
+ public void createSummary_NegativeSum() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum must be non-negative");
+ Summary.create(
+ 10L, -6.6, Snapshot.create(null, null, Collections.<ValueAtPercentile>emptyList()));
+ }
+
+ @Test
+ public void createSummary_ZeroCountAndNonZeroSum() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum must be 0 if count is 0");
+ Summary.create(
+ 0L, 6.6, Snapshot.create(null, null, Collections.<ValueAtPercentile>emptyList()));
+ }
+
+ @Test
+ public void createSummary_NullSnapshot() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("snapshot");
+ Summary.create(10L, 6.6, null);
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(
+ Summary.create(
+ 10L,
+ 10.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)))),
+ Summary.create(
+ 10L,
+ 10.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)))))
+ .addEqualityGroup(
+ Summary.create(
+ 7L,
+ 10.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)))))
+ .addEqualityGroup(
+ Summary.create(
+ 10L,
+ 7.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)))))
+ .addEqualityGroup(
+ Summary.create(
+ 10L, 10.0, Snapshot.create(null, null, Collections.<ValueAtPercentile>emptyList())))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/ValueTest.java b/api/src/test/java/io/opencensus/metrics/ValueTest.java
index 63430b28..a65202a8 100644
--- a/api/src/test/java/io/opencensus/metrics/ValueTest.java
+++ b/api/src/test/java/io/opencensus/metrics/ValueTest.java
@@ -22,11 +22,15 @@ import com.google.common.testing.EqualsTester;
import io.opencensus.common.Function;
import io.opencensus.common.Functions;
import io.opencensus.metrics.Distribution.Bucket;
+import io.opencensus.metrics.Summary.Snapshot;
+import io.opencensus.metrics.Summary.Snapshot.ValueAtPercentile;
import io.opencensus.metrics.Value.ValueDistribution;
import io.opencensus.metrics.Value.ValueDouble;
import io.opencensus.metrics.Value.ValueLong;
+import io.opencensus.metrics.Value.ValueSummary;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -35,6 +39,7 @@ import org.junit.runners.JUnit4;
/** Unit tests for {@link Value}. */
@RunWith(JUnit4.class)
public class ValueTest {
+ private static final double TOLERANCE = 1e-6;
private static final Distribution DISTRIBUTION =
Distribution.create(
@@ -43,12 +48,18 @@ public class ValueTest {
1,
Arrays.asList(-5.0, 0.0, 5.0),
Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4)));
+ private static final Summary SUMMARY =
+ Summary.create(
+ 10L,
+ 10.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(0.98, 10.2))));
@Test
public void createAndGet_ValueDouble() {
Value value = Value.doubleValue(-34.56);
assertThat(value).isInstanceOf(ValueDouble.class);
- assertThat(((ValueDouble) value).getValue()).isEqualTo(-34.56);
+ assertThat(((ValueDouble) value).getValue()).isWithin(TOLERANCE).of(-34.56);
}
@Test
@@ -66,6 +77,13 @@ public class ValueTest {
}
@Test
+ public void createAndGet_ValueSummary() {
+ Value value = Value.summaryValue(SUMMARY);
+ assertThat(value).isInstanceOf(ValueSummary.class);
+ assertThat(((ValueSummary) value).getValue()).isEqualTo(SUMMARY);
+ }
+
+ @Test
public void testEquals() {
new EqualsTester()
.addEqualityGroup(Value.doubleValue(1.0), Value.doubleValue(1.0))
@@ -75,7 +93,7 @@ public class ValueTest {
.addEqualityGroup(
Value.distributionValue(
Distribution.create(
- -7,
+ 7,
10,
23.456,
Arrays.asList(-5.0, 0.0, 5.0),
@@ -88,7 +106,10 @@ public class ValueTest {
public void testMatch() {
List<Value> values =
Arrays.asList(
- ValueDouble.create(1.0), ValueLong.create(-1), ValueDistribution.create(DISTRIBUTION));
+ ValueDouble.create(1.0),
+ ValueLong.create(-1),
+ ValueDistribution.create(DISTRIBUTION),
+ ValueSummary.create(SUMMARY));
List<Number> expected =
Arrays.<Number>asList(1.0, -1L, 10.0, 10L, 1.0, -5.0, 0.0, 5.0, 3L, 1L, 2L, 4L);
final List<Number> actual = new ArrayList<Number>();
@@ -111,7 +132,7 @@ public class ValueTest {
new Function<Distribution, Object>() {
@Override
public Object apply(Distribution arg) {
- actual.add(arg.getMean());
+ actual.add(arg.getSum());
actual.add(arg.getCount());
actual.add(arg.getSumOfSquaredDeviations());
actual.addAll(arg.getBucketBoundaries());
@@ -121,6 +142,12 @@ public class ValueTest {
return null;
}
},
+ new Function<Summary, Object>() {
+ @Override
+ public Object apply(Summary arg) {
+ return null;
+ }
+ },
Functions.throwAssertionError());
}
assertThat(actual).containsExactlyElementsIn(expected).inOrder();