• 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.Collections;
22 import java.util.List;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 /** Unit tests for {@link MetricRegistry}. */
30 @RunWith(JUnit4.class)
31 public class MetricRegistryTest {
32   @Rule public ExpectedException thrown = ExpectedException.none();
33 
34   private static final String NAME = "name";
35   private static final String NAME_2 = "name2";
36   private static final String NAME_3 = "name3";
37   private static final String NAME_4 = "name4";
38   private static final String DESCRIPTION = "description";
39   private static final String UNIT = "1";
40   private static final List<LabelKey> LABEL_KEY =
41       Collections.singletonList(LabelKey.create("key", "key description"));
42   private static final List<LabelValue> LABEL_VALUES =
43       Collections.singletonList(LabelValue.create("value"));
44   private final MetricRegistry metricRegistry =
45       MetricsComponent.newNoopMetricsComponent().getMetricRegistry();
46 
47   @Test
noopAddLongGauge_NullName()48   public void noopAddLongGauge_NullName() {
49     thrown.expect(NullPointerException.class);
50     thrown.expectMessage("name");
51     metricRegistry.addLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
52   }
53 
54   @Test
noopAddLongGauge_NullDescription()55   public void noopAddLongGauge_NullDescription() {
56     thrown.expect(NullPointerException.class);
57     thrown.expectMessage("description");
58     metricRegistry.addLongGauge(NAME, null, UNIT, LABEL_KEY);
59   }
60 
61   @Test
noopAddLongGauge_NullUnit()62   public void noopAddLongGauge_NullUnit() {
63     thrown.expect(NullPointerException.class);
64     thrown.expectMessage("unit");
65     metricRegistry.addLongGauge(NAME, DESCRIPTION, null, LABEL_KEY);
66   }
67 
68   @Test
noopAddLongGauge_NullLabels()69   public void noopAddLongGauge_NullLabels() {
70     thrown.expect(NullPointerException.class);
71     thrown.expectMessage("labelKeys");
72     metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, null);
73   }
74 
75   @Test
noopAddLongGauge_WithNullElement()76   public void noopAddLongGauge_WithNullElement() {
77     List<LabelKey> labelKeys = Collections.singletonList(null);
78     thrown.expect(NullPointerException.class);
79     thrown.expectMessage("labelKey element should not be null.");
80     metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, labelKeys);
81   }
82 
83   @Test
noopAddDoubleGauge_NullName()84   public void noopAddDoubleGauge_NullName() {
85     thrown.expect(NullPointerException.class);
86     thrown.expectMessage("name");
87     metricRegistry.addDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
88   }
89 
90   @Test
noopAddDoubleGauge_NullDescription()91   public void noopAddDoubleGauge_NullDescription() {
92     thrown.expect(NullPointerException.class);
93     thrown.expectMessage("description");
94     metricRegistry.addDoubleGauge(NAME_2, null, UNIT, LABEL_KEY);
95   }
96 
97   @Test
noopAddDoubleGauge_NullUnit()98   public void noopAddDoubleGauge_NullUnit() {
99     thrown.expect(NullPointerException.class);
100     thrown.expectMessage("unit");
101     metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, null, LABEL_KEY);
102   }
103 
104   @Test
noopAddDoubleGauge_NullLabels()105   public void noopAddDoubleGauge_NullLabels() {
106     thrown.expect(NullPointerException.class);
107     thrown.expectMessage("labelKeys");
108     metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, null);
109   }
110 
111   @Test
noopAddDoubleGauge_WithNullElement()112   public void noopAddDoubleGauge_WithNullElement() {
113     List<LabelKey> labelKeys = Collections.singletonList(null);
114     thrown.expect(NullPointerException.class);
115     thrown.expectMessage("labelKey element should not be null.");
116     metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, labelKeys);
117   }
118 
119   @Test
noopAddDerivedLongGauge_NullName()120   public void noopAddDerivedLongGauge_NullName() {
121     thrown.expect(NullPointerException.class);
122     thrown.expectMessage("name");
123     metricRegistry.addDerivedLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
124   }
125 
126   @Test
noopAddDerivedLongGauge_NullDescription()127   public void noopAddDerivedLongGauge_NullDescription() {
128     thrown.expect(NullPointerException.class);
129     thrown.expectMessage("description");
130     metricRegistry.addDerivedLongGauge(NAME_3, null, UNIT, LABEL_KEY);
131   }
132 
133   @Test
noopAddDerivedLongGauge_NullUnit()134   public void noopAddDerivedLongGauge_NullUnit() {
135     thrown.expect(NullPointerException.class);
136     thrown.expectMessage("unit");
137     metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, null, LABEL_KEY);
138   }
139 
140   @Test
noopAddDerivedLongGauge_NullLabels()141   public void noopAddDerivedLongGauge_NullLabels() {
142     thrown.expect(NullPointerException.class);
143     thrown.expectMessage("labelKeys");
144     metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, null);
145   }
146 
147   @Test
noopAddDerivedLongGauge_WithNullElement()148   public void noopAddDerivedLongGauge_WithNullElement() {
149     List<LabelKey> labelKeys = Collections.singletonList(null);
150     thrown.expect(NullPointerException.class);
151     thrown.expectMessage("labelKey element should not be null.");
152     metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, labelKeys);
153   }
154 
155   @Test
noopAddDerivedDoubleGauge_NullName()156   public void noopAddDerivedDoubleGauge_NullName() {
157     thrown.expect(NullPointerException.class);
158     thrown.expectMessage("name");
159     metricRegistry.addDerivedDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
160   }
161 
162   @Test
noopAddDerivedDoubleGauge_NullDescription()163   public void noopAddDerivedDoubleGauge_NullDescription() {
164     thrown.expect(NullPointerException.class);
165     thrown.expectMessage("description");
166     metricRegistry.addDerivedDoubleGauge(NAME_4, null, UNIT, LABEL_KEY);
167   }
168 
169   @Test
noopAddDerivedDoubleGauge_NullUnit()170   public void noopAddDerivedDoubleGauge_NullUnit() {
171     thrown.expect(NullPointerException.class);
172     thrown.expectMessage("unit");
173     metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, null, LABEL_KEY);
174   }
175 
176   @Test
noopAddDerivedDoubleGauge_NullLabels()177   public void noopAddDerivedDoubleGauge_NullLabels() {
178     thrown.expect(NullPointerException.class);
179     thrown.expectMessage("labelKeys");
180     metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, null);
181   }
182 
183   @Test
noopAddDerivedDoubleGauge_WithNullElement()184   public void noopAddDerivedDoubleGauge_WithNullElement() {
185     List<LabelKey> labelKeys = Collections.singletonList(null);
186     thrown.expect(NullPointerException.class);
187     thrown.expectMessage("labelKey element should not be null.");
188     metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, labelKeys);
189   }
190 
191   @Test
noopSameAs()192   public void noopSameAs() {
193     LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
194     assertThat(longGauge.getDefaultTimeSeries()).isSameAs(longGauge.getDefaultTimeSeries());
195     assertThat(longGauge.getDefaultTimeSeries())
196         .isSameAs(longGauge.getOrCreateTimeSeries(LABEL_VALUES));
197 
198     DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY);
199     assertThat(doubleGauge.getDefaultTimeSeries()).isSameAs(doubleGauge.getDefaultTimeSeries());
200     assertThat(doubleGauge.getDefaultTimeSeries())
201         .isSameAs(doubleGauge.getOrCreateTimeSeries(LABEL_VALUES));
202   }
203 
204   @Test
noopInstanceOf()205   public void noopInstanceOf() {
206     assertThat(metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY))
207         .isInstanceOf(LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY).getClass());
208     assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY))
209         .isInstanceOf(
210             DoubleGauge.newNoopDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY).getClass());
211     assertThat(metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY))
212         .isInstanceOf(
213             DerivedLongGauge.newNoopDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY)
214                 .getClass());
215     assertThat(metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY))
216         .isInstanceOf(
217             DerivedDoubleGauge.newNoopDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY)
218                 .getClass());
219   }
220 }
221