• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, 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.metrics;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 /** Unit tests for {@link DoubleGauge}. */
31 @RunWith(JUnit4.class)
32 public class DoubleGaugeTest {
33   @Rule public ExpectedException thrown = ExpectedException.none();
34 
35   private static final String NAME = "name";
36   private static final String DESCRIPTION = "description";
37   private static final String UNIT = "1";
38   private static final List<LabelKey> LABEL_KEY =
39       Collections.singletonList(LabelKey.create("key", "key description"));
40   private static final List<LabelValue> LABEL_VALUES =
41       Collections.singletonList(LabelValue.create("value"));
42   private static final List<LabelKey> EMPTY_LABEL_KEYS = new ArrayList<LabelKey>();
43   private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<LabelValue>();
44 
45   // TODO(mayurkale): Add more tests, once DoubleGauge plugs-in into the registry.
46 
47   @Test
noopGetOrCreateTimeSeries_WithNullLabelValues()48   public void noopGetOrCreateTimeSeries_WithNullLabelValues() {
49     DoubleGauge doubleGauge =
50         DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, EMPTY_LABEL_KEYS);
51     thrown.expect(NullPointerException.class);
52     thrown.expectMessage("labelValues");
53     doubleGauge.getOrCreateTimeSeries(null);
54   }
55 
56   @Test
noopGetOrCreateTimeSeries_WithNullElement()57   public void noopGetOrCreateTimeSeries_WithNullElement() {
58     List<LabelValue> labelValues = Collections.singletonList(null);
59     DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
60     thrown.expect(NullPointerException.class);
61     thrown.expectMessage("labelValue element should not be null.");
62     doubleGauge.getOrCreateTimeSeries(labelValues);
63   }
64 
65   @Test
noopGetOrCreateTimeSeries_WithInvalidLabelSize()66   public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() {
67     DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
68     thrown.expect(IllegalArgumentException.class);
69     thrown.expectMessage("Incorrect number of labels.");
70     doubleGauge.getOrCreateTimeSeries(EMPTY_LABEL_VALUES);
71   }
72 
73   @Test
noopRemoveTimeSeries_WithNullLabelValues()74   public void noopRemoveTimeSeries_WithNullLabelValues() {
75     DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
76     thrown.expect(NullPointerException.class);
77     thrown.expectMessage("labelValues");
78     doubleGauge.removeTimeSeries(null);
79   }
80 
81   @Test
noopSameAs()82   public void noopSameAs() {
83     DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
84     assertThat(doubleGauge.getDefaultTimeSeries()).isSameAs(doubleGauge.getDefaultTimeSeries());
85     assertThat(doubleGauge.getDefaultTimeSeries())
86         .isSameAs(doubleGauge.getOrCreateTimeSeries(LABEL_VALUES));
87   }
88 }
89