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