• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.stats;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import io.opencensus.metrics.data.AttachmentValue;
22 import io.opencensus.metrics.data.AttachmentValue.AttachmentValueString;
23 import io.opencensus.stats.Measure.MeasureDouble;
24 import io.opencensus.tags.Tag;
25 import io.opencensus.tags.TagContext;
26 import io.opencensus.tags.TagKey;
27 import io.opencensus.tags.TagValue;
28 import java.util.Collections;
29 import java.util.Iterator;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.JUnit4;
35 
36 /**
37  * Unit tests for {@link NoopStats}. Tests for {@link NoopStats#newNoopViewManager} are in {@link
38  * NoopViewManagerTest}
39  */
40 @RunWith(JUnit4.class)
41 public final class NoopStatsTest {
42   private static final Tag TAG = Tag.create(TagKey.create("key"), TagValue.create("value"));
43   private static final MeasureDouble MEASURE =
44       Measure.MeasureDouble.create("my measure", "description", "s");
45   private static final AttachmentValue ATTACHMENT_VALUE = AttachmentValueString.create("value");
46 
47   private final TagContext tagContext =
48       new TagContext() {
49 
50         @Override
51         protected Iterator<Tag> getIterator() {
52           return Collections.<Tag>singleton(TAG).iterator();
53         }
54       };
55 
56   @Rule public final ExpectedException thrown = ExpectedException.none();
57 
58   @Test
noopStatsComponent()59   public void noopStatsComponent() {
60     assertThat(NoopStats.newNoopStatsComponent().getStatsRecorder())
61         .isSameInstanceAs(NoopStats.getNoopStatsRecorder());
62     assertThat(NoopStats.newNoopStatsComponent().getViewManager())
63         .isInstanceOf(NoopStats.newNoopViewManager().getClass());
64   }
65 
66   @Test
noopStatsComponent_GetState()67   public void noopStatsComponent_GetState() {
68     assertThat(NoopStats.newNoopStatsComponent().getState())
69         .isEqualTo(StatsCollectionState.DISABLED);
70   }
71 
72   @Test
73   @SuppressWarnings("deprecation")
noopStatsComponent_SetState_IgnoresInput()74   public void noopStatsComponent_SetState_IgnoresInput() {
75     StatsComponent noopStatsComponent = NoopStats.newNoopStatsComponent();
76     noopStatsComponent.setState(StatsCollectionState.ENABLED);
77     assertThat(noopStatsComponent.getState()).isEqualTo(StatsCollectionState.DISABLED);
78   }
79 
80   @Test
81   @SuppressWarnings("deprecation")
noopStatsComponent_SetState_DisallowsNull()82   public void noopStatsComponent_SetState_DisallowsNull() {
83     StatsComponent noopStatsComponent = NoopStats.newNoopStatsComponent();
84     thrown.expect(NullPointerException.class);
85     noopStatsComponent.setState(null);
86   }
87 
88   @Test
89   @SuppressWarnings("deprecation")
noopStatsComponent_DisallowsSetStateAfterGetState()90   public void noopStatsComponent_DisallowsSetStateAfterGetState() {
91     StatsComponent noopStatsComponent = NoopStats.newNoopStatsComponent();
92     noopStatsComponent.setState(StatsCollectionState.DISABLED);
93     noopStatsComponent.getState();
94     thrown.expect(IllegalStateException.class);
95     thrown.expectMessage("State was already read, cannot set state.");
96     noopStatsComponent.setState(StatsCollectionState.ENABLED);
97   }
98 
99   @Test
noopStatsRecorder_PutAttachmentNullKey()100   public void noopStatsRecorder_PutAttachmentNullKey() {
101     MeasureMap measureMap = NoopStats.getNoopStatsRecorder().newMeasureMap();
102     thrown.expect(NullPointerException.class);
103     thrown.expectMessage("key");
104     measureMap.putAttachment(null, ATTACHMENT_VALUE);
105   }
106 
107   @Test
noopStatsRecorder_PutAttachmentNullValue()108   public void noopStatsRecorder_PutAttachmentNullValue() {
109     MeasureMap measureMap = NoopStats.getNoopStatsRecorder().newMeasureMap();
110     thrown.expect(NullPointerException.class);
111     thrown.expectMessage("value");
112     measureMap.putAttachment("key", (AttachmentValue) null);
113   }
114 
115   @Test
noopStatsRecorder_PutNegativeValue()116   public void noopStatsRecorder_PutNegativeValue() {
117     NoopStats.getNoopStatsRecorder().newMeasureMap().put(MEASURE, -5).record(tagContext);
118   }
119 
120   // The NoopStatsRecorder should do nothing, so this test just checks that record doesn't throw an
121   // exception.
122   @Test
noopStatsRecorder_Record()123   public void noopStatsRecorder_Record() {
124     NoopStats.getNoopStatsRecorder().newMeasureMap().put(MEASURE, 5).record(tagContext);
125   }
126 
127   // The NoopStatsRecorder should do nothing, so this test just checks that record doesn't throw an
128   // exception.
129   @Test
noopStatsRecorder_RecordWithCurrentContext()130   public void noopStatsRecorder_RecordWithCurrentContext() {
131     NoopStats.getNoopStatsRecorder().newMeasureMap().put(MEASURE, 6).record();
132   }
133 
134   @Test
noopStatsRecorder_Record_DisallowNullTagContext()135   public void noopStatsRecorder_Record_DisallowNullTagContext() {
136     MeasureMap measureMap = NoopStats.getNoopStatsRecorder().newMeasureMap();
137     thrown.expect(NullPointerException.class);
138     thrown.expectMessage("tags");
139     measureMap.record(null);
140   }
141 }
142