aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java
blob: 161ad348f93f9e59e30c989ab8825863a39a217c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * 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.export.Metric;
import io.opencensus.metrics.export.MetricDescriptor;
import io.opencensus.metrics.export.MetricDescriptor.Type;
import io.opencensus.metrics.export.Point;
import io.opencensus.metrics.export.TimeSeries;
import io.opencensus.metrics.export.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),
                Collections.singletonList(
                    TimeSeries.create(
                        LABEL_VALUES,
                        Collections.singletonList(
                            Point.create(Value.longValue(OBJ.hashCode()), TEST_TIME)),
                        null))));
  }

  @Test
  public void doubleGauge_GetMetric() {
    assertThat(doubleGauge.getMetric(testClock))
        .isEqualTo(
            Metric.create(
                MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, LABEL_KEYS),
                Collections.singletonList(
                    TimeSeries.create(
                        LABEL_VALUES,
                        Collections.singletonList(
                            Point.create(Value.doubleValue(OBJ.hashCode()), TEST_TIME)),
                        null))));
  }
}