• 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.trace;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Matchers.eq;
21 import static org.mockito.Matchers.same;
22 import static org.mockito.Mockito.verify;
23 
24 import java.util.Collections;
25 import java.util.EnumSet;
26 import java.util.Random;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 import org.mockito.Mockito;
32 
33 /** Unit tests for {@link Span}. */
34 @RunWith(JUnit4.class)
35 public class SpanTest {
36   private Random random;
37   private SpanContext spanContext;
38   private SpanContext notSampledSpanContext;
39   private EnumSet<Span.Options> spanOptions;
40 
41   @Before
setUp()42   public void setUp() {
43     random = new Random(1234);
44     spanContext =
45         SpanContext.create(
46             TraceId.generateRandomId(random),
47             SpanId.generateRandomId(random),
48             TraceOptions.builder().setIsSampled(true).build());
49     notSampledSpanContext =
50         SpanContext.create(
51             TraceId.generateRandomId(random),
52             SpanId.generateRandomId(random),
53             TraceOptions.DEFAULT);
54     spanOptions = EnumSet.of(Span.Options.RECORD_EVENTS);
55   }
56 
57   @Test(expected = NullPointerException.class)
newSpan_WithNullContext()58   public void newSpan_WithNullContext() {
59     new NoopSpan(null, null);
60   }
61 
62   @Test(expected = IllegalArgumentException.class)
newSpan_SampledContextAndNullOptions()63   public void newSpan_SampledContextAndNullOptions() {
64     new NoopSpan(spanContext, null);
65   }
66 
67   @Test(expected = IllegalArgumentException.class)
newSpan_SampledContextAndEmptyOptions()68   public void newSpan_SampledContextAndEmptyOptions() {
69     new NoopSpan(spanContext, EnumSet.noneOf(Span.Options.class));
70   }
71 
72   @Test
getOptions_WhenNullOptions()73   public void getOptions_WhenNullOptions() {
74     Span span = new NoopSpan(notSampledSpanContext, null);
75     assertThat(span.getOptions()).isEmpty();
76   }
77 
78   @Test
getContextAndOptions()79   public void getContextAndOptions() {
80     Span span = new NoopSpan(spanContext, spanOptions);
81     assertThat(span.getContext()).isEqualTo(spanContext);
82     assertThat(span.getOptions()).isEqualTo(spanOptions);
83   }
84 
85   @Test
putAttributeCallsAddAttributesByDefault()86   public void putAttributeCallsAddAttributesByDefault() {
87     Span span = Mockito.spy(new NoopSpan(spanContext, spanOptions));
88     span.putAttribute("MyKey", AttributeValue.booleanAttributeValue(true));
89     span.end();
90     verify(span)
91         .putAttributes(
92             eq(Collections.singletonMap("MyKey", AttributeValue.booleanAttributeValue(true))));
93   }
94 
95   @Test
endCallsEndWithDefaultOptions()96   public void endCallsEndWithDefaultOptions() {
97     Span span = Mockito.spy(new NoopSpan(spanContext, spanOptions));
98     span.end();
99     verify(span).end(same(EndSpanOptions.DEFAULT));
100   }
101 
102   @Test
addMessageEventDefaultImplementation()103   public void addMessageEventDefaultImplementation() {
104     Span mockSpan = Mockito.mock(Span.class);
105     MessageEvent messageEvent =
106         MessageEvent.builder(MessageEvent.Type.SENT, 123)
107             .setUncompressedMessageSize(456)
108             .setCompressedMessageSize(789)
109             .build();
110     NetworkEvent networkEvent =
111         NetworkEvent.builder(NetworkEvent.Type.SENT, 123)
112             .setUncompressedMessageSize(456)
113             .setCompressedMessageSize(789)
114             .build();
115     Mockito.doCallRealMethod().when(mockSpan).addMessageEvent(messageEvent);
116     mockSpan.addMessageEvent(messageEvent);
117     verify(mockSpan).addNetworkEvent(eq(networkEvent));
118   }
119 }
120