• 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.common.Duration;
22 import io.opencensus.common.Timestamp;
23 import io.opencensus.stats.Aggregation.Sum;
24 import io.opencensus.stats.Measure.MeasureDouble;
25 import io.opencensus.stats.View.AggregationWindow.Cumulative;
26 import io.opencensus.stats.View.AggregationWindow.Interval;
27 import io.opencensus.stats.View.Name;
28 import io.opencensus.stats.ViewData.AggregationWindowData.CumulativeData;
29 import io.opencensus.stats.ViewData.AggregationWindowData.IntervalData;
30 import io.opencensus.tags.TagKey;
31 import java.util.Arrays;
32 import java.util.Set;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.JUnit4;
38 
39 /** Unit tests for {@link NoopStats#newNoopViewManager}. */
40 @RunWith(JUnit4.class)
41 public final class NoopViewManagerTest {
42   private static final MeasureDouble MEASURE =
43       Measure.MeasureDouble.create("my measure", "description", "s");
44   private static final TagKey KEY = TagKey.create("KEY");
45   private static final Name VIEW_NAME = Name.create("my view");
46   private static final String VIEW_DESCRIPTION = "view description";
47   private static final Sum AGGREGATION = Sum.create();
48   private static final Cumulative CUMULATIVE = Cumulative.create();
49   private static final Duration TEN_SECONDS = Duration.create(10, 0);
50   private static final Interval INTERVAL = Interval.create(TEN_SECONDS);
51 
52   @Rule public final ExpectedException thrown = ExpectedException.none();
53 
54   @Test
noopViewManager_RegisterView_DisallowRegisteringDifferentViewWithSameName()55   public void noopViewManager_RegisterView_DisallowRegisteringDifferentViewWithSameName() {
56     final View view1 =
57         View.create(
58             VIEW_NAME, "description 1", MEASURE, AGGREGATION, Arrays.asList(KEY), CUMULATIVE);
59     final View view2 =
60         View.create(
61             VIEW_NAME, "description 2", MEASURE, AGGREGATION, Arrays.asList(KEY), CUMULATIVE);
62     ViewManager viewManager = NoopStats.newNoopViewManager();
63     viewManager.registerView(view1);
64 
65     try {
66       thrown.expect(IllegalArgumentException.class);
67       thrown.expectMessage("A different view with the same name already exists.");
68       viewManager.registerView(view2);
69     } finally {
70       assertThat(viewManager.getView(VIEW_NAME).getView()).isEqualTo(view1);
71     }
72   }
73 
74   @Test
noopViewManager_RegisterView_AllowRegisteringSameViewTwice()75   public void noopViewManager_RegisterView_AllowRegisteringSameViewTwice() {
76     View view =
77         View.create(
78             VIEW_NAME, VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY), CUMULATIVE);
79     ViewManager viewManager = NoopStats.newNoopViewManager();
80     viewManager.registerView(view);
81     viewManager.registerView(view);
82   }
83 
84   @Test
noopViewManager_RegisterView_DisallowNull()85   public void noopViewManager_RegisterView_DisallowNull() {
86     ViewManager viewManager = NoopStats.newNoopViewManager();
87     thrown.expect(NullPointerException.class);
88     viewManager.registerView(null);
89   }
90 
91   @Test
noopViewManager_GetView_GettingNonExistentViewReturnsNull()92   public void noopViewManager_GetView_GettingNonExistentViewReturnsNull() {
93     ViewManager viewManager = NoopStats.newNoopViewManager();
94     assertThat(viewManager.getView(VIEW_NAME)).isNull();
95   }
96 
97   @Test
noopViewManager_GetView_Cumulative()98   public void noopViewManager_GetView_Cumulative() {
99     View view =
100         View.create(
101             VIEW_NAME, VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY), CUMULATIVE);
102     ViewManager viewManager = NoopStats.newNoopViewManager();
103     viewManager.registerView(view);
104 
105     ViewData viewData = viewManager.getView(VIEW_NAME);
106     assertThat(viewData.getView()).isEqualTo(view);
107     assertThat(viewData.getAggregationMap()).isEmpty();
108     assertThat(viewData.getStart()).isEqualTo(Timestamp.create(0, 0));
109     assertThat(viewData.getEnd()).isEqualTo(Timestamp.create(0, 0));
110     assertThat(viewData.getWindowData())
111         .isEqualTo(CumulativeData.create(Timestamp.create(0, 0), Timestamp.create(0, 0)));
112   }
113 
114   @Test
noopViewManager_GetView_Interval()115   public void noopViewManager_GetView_Interval() {
116     View view =
117         View.create(
118             VIEW_NAME, VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY), INTERVAL);
119     ViewManager viewManager = NoopStats.newNoopViewManager();
120     viewManager.registerView(view);
121 
122     ViewData viewData = viewManager.getView(VIEW_NAME);
123     assertThat(viewData.getView()).isEqualTo(view);
124     assertThat(viewData.getAggregationMap()).isEmpty();
125     assertThat(viewData.getWindowData()).isEqualTo(IntervalData.create(Timestamp.create(0, 0)));
126   }
127 
128   @Test
noopViewManager_GetView_DisallowNull()129   public void noopViewManager_GetView_DisallowNull() {
130     ViewManager viewManager = NoopStats.newNoopViewManager();
131     thrown.expect(NullPointerException.class);
132     viewManager.getView(null);
133   }
134 
135   @Test
getAllExportedViews()136   public void getAllExportedViews() {
137     ViewManager viewManager = NoopStats.newNoopViewManager();
138     assertThat(viewManager.getAllExportedViews()).isEmpty();
139     View cumulativeView1 =
140         View.create(
141             View.Name.create("View 1"),
142             VIEW_DESCRIPTION,
143             MEASURE,
144             AGGREGATION,
145             Arrays.asList(KEY),
146             CUMULATIVE);
147     View cumulativeView2 =
148         View.create(
149             View.Name.create("View 2"),
150             VIEW_DESCRIPTION,
151             MEASURE,
152             AGGREGATION,
153             Arrays.asList(KEY),
154             CUMULATIVE);
155     View intervalView =
156         View.create(
157             View.Name.create("View 3"),
158             VIEW_DESCRIPTION,
159             MEASURE,
160             AGGREGATION,
161             Arrays.asList(KEY),
162             INTERVAL);
163     viewManager.registerView(cumulativeView1);
164     viewManager.registerView(cumulativeView2);
165     viewManager.registerView(intervalView);
166 
167     // Only cumulative views should be exported.
168     assertThat(viewManager.getAllExportedViews()).containsExactly(cumulativeView1, cumulativeView2);
169   }
170 
171   @Test
getAllExportedViews_ResultIsUnmodifiable()172   public void getAllExportedViews_ResultIsUnmodifiable() {
173     ViewManager viewManager = NoopStats.newNoopViewManager();
174     View view1 =
175         View.create(
176             View.Name.create("View 1"), VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY));
177     viewManager.registerView(view1);
178     Set<View> exported = viewManager.getAllExportedViews();
179 
180     View view2 =
181         View.create(
182             View.Name.create("View 2"), VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY));
183     thrown.expect(UnsupportedOperationException.class);
184     exported.add(view2);
185   }
186 }
187