|
| 1 | +/* |
| 2 | + * Copyright 2017, OpenCensus Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.opencensus.implcore.stats; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import io.opencensus.common.Duration; |
| 22 | +import io.opencensus.common.Timestamp; |
| 23 | +import io.opencensus.implcore.stats.MutableAggregation.MutableCount; |
| 24 | +import io.opencensus.implcore.stats.MutableAggregation.MutableHistogram; |
| 25 | +import io.opencensus.implcore.stats.MutableAggregation.MutableSum; |
| 26 | +import io.opencensus.stats.Aggregation; |
| 27 | +import io.opencensus.stats.Aggregation.Count; |
| 28 | +import io.opencensus.stats.Aggregation.Histogram; |
| 29 | +import io.opencensus.stats.Aggregation.Sum; |
| 30 | +import io.opencensus.stats.BucketBoundaries; |
| 31 | +import io.opencensus.tags.TagValue; |
| 32 | +import io.opencensus.tags.TagValue.TagValueString; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.List; |
| 36 | +import org.junit.Rule; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.rules.ExpectedException; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.junit.runners.JUnit4; |
| 41 | + |
| 42 | +/** Tests for {@link IntervalBucket}. */ |
| 43 | +@RunWith(JUnit4.class) |
| 44 | +public class IntervalBucketTest { |
| 45 | + |
| 46 | + @Rule public final ExpectedException thrown = ExpectedException.none(); |
| 47 | + |
| 48 | + private static final double TOLERANCE = 1e-6; |
| 49 | + private static final Duration MINUTE = Duration.create(60, 0); |
| 50 | + private static final Timestamp START = Timestamp.create(60, 0); |
| 51 | + private static final BucketBoundaries BUCKET_BOUNDARIES = |
| 52 | + BucketBoundaries.create(Arrays.asList(-10.0, 0.0, 10.0)); |
| 53 | + private static final List<Aggregation> AGGREGATIONS_V1 = |
| 54 | + Collections.unmodifiableList(Arrays.asList( |
| 55 | + Sum.create(), Count.create(), Histogram.create(BUCKET_BOUNDARIES))); |
| 56 | + |
| 57 | + @Test |
| 58 | + public void preventNullStartTime() { |
| 59 | + thrown.expect(NullPointerException.class); |
| 60 | + new IntervalBucket(null, MINUTE, AGGREGATIONS_V1); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void preventNullDuration() { |
| 65 | + thrown.expect(NullPointerException.class); |
| 66 | + new IntervalBucket(START, null, AGGREGATIONS_V1); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void preventNullAggregationList() { |
| 71 | + thrown.expect(NullPointerException.class); |
| 72 | + new IntervalBucket(START, MINUTE, null); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testGetTagValueAggregationMap_empty() { |
| 77 | + assertThat(new IntervalBucket(START, MINUTE, AGGREGATIONS_V1).getTagValueAggregationMap()) |
| 78 | + .isEmpty(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testGetStart() { |
| 83 | + assertThat(new IntervalBucket(START, MINUTE, AGGREGATIONS_V1).getStart()).isEqualTo(START); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testRecord() { |
| 88 | + IntervalBucket bucket = new IntervalBucket(START, MINUTE, AGGREGATIONS_V1); |
| 89 | + List<TagValue> tagValues1 = Arrays.<TagValue>asList(TagValueString.create("VALUE1")); |
| 90 | + List<TagValue> tagValues2 = Arrays.<TagValue>asList(TagValueString.create("VALUE2")); |
| 91 | + bucket.record(tagValues1, 5.0); |
| 92 | + bucket.record(tagValues1, 15.0); |
| 93 | + bucket.record(tagValues2, 10.0); |
| 94 | + assertThat(bucket.getTagValueAggregationMap()).containsKey(tagValues1); |
| 95 | + verifyMutableAggregations(bucket.getTagValueAggregationMap().get(tagValues1), |
| 96 | + 20.0, 2, new long[]{0, 0, 1, 1}, TOLERANCE); |
| 97 | + assertThat(bucket.getTagValueAggregationMap()).containsKey(tagValues2); |
| 98 | + verifyMutableAggregations(bucket.getTagValueAggregationMap().get(tagValues2), |
| 99 | + 10.0, 1, new long[]{0, 0, 0, 1}, TOLERANCE); |
| 100 | + } |
| 101 | + |
| 102 | + private static void verifyMutableAggregations( |
| 103 | + List<MutableAggregation> aggregations, double sum, long count, long[] bucketCounts, |
| 104 | + double tolerance) { |
| 105 | + assertThat(aggregations).hasSize(3); |
| 106 | + assertThat(aggregations.get(0)).isInstanceOf(MutableSum.class); |
| 107 | + assertThat(((MutableSum) aggregations.get(0)).getSum()).isWithin(tolerance).of(sum); |
| 108 | + assertThat(aggregations.get(1)).isInstanceOf(MutableCount.class); |
| 109 | + assertThat(((MutableCount) aggregations.get(1)).getCount()).isEqualTo(count); |
| 110 | + assertThat(aggregations.get(2)).isInstanceOf(MutableHistogram.class); |
| 111 | + assertThat(((MutableHistogram) aggregations.get(2)).getBucketCounts()).isEqualTo(bucketCounts); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testGetFraction() { |
| 116 | + Timestamp thirtySecondsAfterStart = Timestamp.create(90, 0); |
| 117 | + assertThat( |
| 118 | + new IntervalBucket(START, MINUTE, AGGREGATIONS_V1).getFraction(thirtySecondsAfterStart)) |
| 119 | + .isWithin(TOLERANCE).of(0.5); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void preventCallingGetFractionOnPastBuckets() { |
| 124 | + IntervalBucket bucket = new IntervalBucket(START, MINUTE, AGGREGATIONS_V1); |
| 125 | + Timestamp twoMinutesAfterStart = Timestamp.create(180, 0); |
| 126 | + thrown.expect(IllegalArgumentException.class); |
| 127 | + bucket.getFraction(twoMinutesAfterStart); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void preventCallingGetFractionOnFutureBuckets() { |
| 132 | + IntervalBucket bucket = new IntervalBucket(START, MINUTE, AGGREGATIONS_V1); |
| 133 | + Timestamp thirtySecondsBeforeStart = Timestamp.create(30, 0); |
| 134 | + thrown.expect(IllegalArgumentException.class); |
| 135 | + bucket.getFraction(thirtySecondsBeforeStart); |
| 136 | + } |
| 137 | +} |
0 commit comments