|
| 1 | +/* |
| 2 | + * Copyright 2019, 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.exporter.metrics.util; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import io.opencensus.common.Timestamp; |
| 22 | +import io.opencensus.exporter.metrics.util.QueueMetricProducer.Options; |
| 23 | +import io.opencensus.metrics.LabelKey; |
| 24 | +import io.opencensus.metrics.LabelValue; |
| 25 | +import io.opencensus.metrics.export.Metric; |
| 26 | +import io.opencensus.metrics.export.MetricDescriptor; |
| 27 | +import io.opencensus.metrics.export.Point; |
| 28 | +import io.opencensus.metrics.export.Value; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import org.junit.Rule; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.rules.ExpectedException; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.JUnit4; |
| 36 | + |
| 37 | +/** Unit tests for {@link QueueMetricProducer}. */ |
| 38 | +@RunWith(JUnit4.class) |
| 39 | +public class QueueMetricProducerTest { |
| 40 | + |
| 41 | + private static final String METRIC_NAME = "test_metric"; |
| 42 | + private static final String METRIC_DESCRIPTION = "test_description"; |
| 43 | + private static final String METRIC_UNIT = "us"; |
| 44 | + private static final List<LabelKey> LABEL_KEY = |
| 45 | + Collections.singletonList(LabelKey.create("test_key", "test_description")); |
| 46 | + private static final List<LabelValue> LABEL_VALUE = |
| 47 | + Collections.singletonList(LabelValue.create("test_value")); |
| 48 | + private static final io.opencensus.metrics.export.MetricDescriptor METRIC_DESCRIPTOR = |
| 49 | + io.opencensus.metrics.export.MetricDescriptor.create( |
| 50 | + METRIC_NAME, |
| 51 | + METRIC_DESCRIPTION, |
| 52 | + METRIC_UNIT, |
| 53 | + MetricDescriptor.Type.CUMULATIVE_INT64, |
| 54 | + LABEL_KEY); |
| 55 | + |
| 56 | + private static final Value VALUE_LONG = Value.longValue(12345678); |
| 57 | + private static final Value VALUE_LONG_2 = Value.longValue(23456789); |
| 58 | + private static final Timestamp TIMESTAMP = Timestamp.fromMillis(3000); |
| 59 | + private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(4000); |
| 60 | + private static final Timestamp TIMESTAMP_3 = Timestamp.fromMillis(4000); |
| 61 | + private static final Point POINT = Point.create(VALUE_LONG, TIMESTAMP); |
| 62 | + private static final Point POINT_2 = Point.create(VALUE_LONG_2, TIMESTAMP); |
| 63 | + |
| 64 | + private static final io.opencensus.metrics.export.TimeSeries CUMULATIVE_TIME_SERIES = |
| 65 | + io.opencensus.metrics.export.TimeSeries.createWithOnePoint(LABEL_VALUE, POINT, TIMESTAMP_2); |
| 66 | + private static final io.opencensus.metrics.export.TimeSeries CUMULATIVE_TIME_SERIES_2 = |
| 67 | + io.opencensus.metrics.export.TimeSeries.createWithOnePoint(LABEL_VALUE, POINT_2, TIMESTAMP_3); |
| 68 | + |
| 69 | + private static final Metric METRIC_1 = |
| 70 | + Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR, CUMULATIVE_TIME_SERIES); |
| 71 | + private static final Metric METRIC_2 = |
| 72 | + Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR, CUMULATIVE_TIME_SERIES_2); |
| 73 | + |
| 74 | + @Rule public final ExpectedException thrown = ExpectedException.none(); |
| 75 | + |
| 76 | + @Test |
| 77 | + public void createWithNegativeBufferSize() { |
| 78 | + Options options = Options.builder().setBufferSize(-1).build(); |
| 79 | + thrown.expect(IllegalArgumentException.class); |
| 80 | + QueueMetricProducer.create(options); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void createWithZeroBufferSize() { |
| 85 | + Options options = Options.builder().setBufferSize(0).build(); |
| 86 | + thrown.expect(IllegalArgumentException.class); |
| 87 | + QueueMetricProducer.create(options); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void pushMetrics() { |
| 92 | + Options options = Options.builder().setBufferSize(1).build(); |
| 93 | + QueueMetricProducer producer = QueueMetricProducer.create(options); |
| 94 | + producer.pushMetrics(Collections.singleton(METRIC_1)); |
| 95 | + assertThat(producer.getMetrics()).containsExactly(METRIC_1); |
| 96 | + assertThat(producer.getMetrics()).isEmpty(); // Flush after each getMetrics(). |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void pushMetrics_ExceedBufferSize() { |
| 101 | + Options options = Options.builder().setBufferSize(1).build(); |
| 102 | + QueueMetricProducer producer = QueueMetricProducer.create(options); |
| 103 | + producer.pushMetrics(Collections.singleton(METRIC_1)); |
| 104 | + producer.pushMetrics(Collections.singleton(METRIC_2)); |
| 105 | + assertThat(producer.getMetrics()).containsExactly(METRIC_2); |
| 106 | + } |
| 107 | +} |
0 commit comments