• 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 
21 import com.google.common.testing.EqualsTester;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 /** Unit tests for {@link SpanContext}. */
27 @RunWith(JUnit4.class)
28 public class SpanContextTest {
29   private static final byte[] firstTraceIdBytes =
30       new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a'};
31   private static final byte[] secondTraceIdBytes =
32       new byte[] {0, 0, 0, 0, 0, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 0};
33   private static final byte[] firstSpanIdBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'};
34   private static final byte[] secondSpanIdBytes = new byte[] {'0', 0, 0, 0, 0, 0, 0, 0};
35   private static final Tracestate firstTracestate = Tracestate.builder().set("foo", "bar").build();
36   private static final Tracestate secondTracestate = Tracestate.builder().set("foo", "baz").build();
37   private static final SpanContext first =
38       SpanContext.create(
39           TraceId.fromBytes(firstTraceIdBytes),
40           SpanId.fromBytes(firstSpanIdBytes),
41           TraceOptions.DEFAULT,
42           firstTracestate);
43   private static final SpanContext second =
44       SpanContext.create(
45           TraceId.fromBytes(secondTraceIdBytes),
46           SpanId.fromBytes(secondSpanIdBytes),
47           TraceOptions.builder().setIsSampled(true).build(),
48           secondTracestate);
49 
50   @Test
invalidSpanContext()51   public void invalidSpanContext() {
52     assertThat(SpanContext.INVALID.getTraceId()).isEqualTo(TraceId.INVALID);
53     assertThat(SpanContext.INVALID.getSpanId()).isEqualTo(SpanId.INVALID);
54     assertThat(SpanContext.INVALID.getTraceOptions()).isEqualTo(TraceOptions.DEFAULT);
55   }
56 
57   @Test
isValid()58   public void isValid() {
59     assertThat(SpanContext.INVALID.isValid()).isFalse();
60     assertThat(
61             SpanContext.create(
62                     TraceId.fromBytes(firstTraceIdBytes), SpanId.INVALID, TraceOptions.DEFAULT)
63                 .isValid())
64         .isFalse();
65     assertThat(
66             SpanContext.create(
67                     TraceId.INVALID, SpanId.fromBytes(firstSpanIdBytes), TraceOptions.DEFAULT)
68                 .isValid())
69         .isFalse();
70     assertThat(first.isValid()).isTrue();
71     assertThat(second.isValid()).isTrue();
72   }
73 
74   @Test
getTraceId()75   public void getTraceId() {
76     assertThat(first.getTraceId()).isEqualTo(TraceId.fromBytes(firstTraceIdBytes));
77     assertThat(second.getTraceId()).isEqualTo(TraceId.fromBytes(secondTraceIdBytes));
78   }
79 
80   @Test
getSpanId()81   public void getSpanId() {
82     assertThat(first.getSpanId()).isEqualTo(SpanId.fromBytes(firstSpanIdBytes));
83     assertThat(second.getSpanId()).isEqualTo(SpanId.fromBytes(secondSpanIdBytes));
84   }
85 
86   @Test
getTraceOptions()87   public void getTraceOptions() {
88     assertThat(first.getTraceOptions()).isEqualTo(TraceOptions.DEFAULT);
89     assertThat(second.getTraceOptions())
90         .isEqualTo(TraceOptions.builder().setIsSampled(true).build());
91   }
92 
93   @Test
getTracestate()94   public void getTracestate() {
95     assertThat(first.getTracestate()).isEqualTo(firstTracestate);
96     assertThat(second.getTracestate()).isEqualTo(secondTracestate);
97   }
98 
99   @Test
spanContext_EqualsAndHashCode()100   public void spanContext_EqualsAndHashCode() {
101     EqualsTester tester = new EqualsTester();
102     tester.addEqualityGroup(
103         first,
104         SpanContext.create(
105             TraceId.fromBytes(firstTraceIdBytes),
106             SpanId.fromBytes(firstSpanIdBytes),
107             TraceOptions.DEFAULT),
108         SpanContext.create(
109             TraceId.fromBytes(firstTraceIdBytes),
110             SpanId.fromBytes(firstSpanIdBytes),
111             TraceOptions.builder().setIsSampled(false).build(),
112             firstTracestate));
113     tester.addEqualityGroup(
114         second,
115         SpanContext.create(
116             TraceId.fromBytes(secondTraceIdBytes),
117             SpanId.fromBytes(secondSpanIdBytes),
118             TraceOptions.builder().setIsSampled(true).build(),
119             secondTracestate));
120     tester.testEquals();
121   }
122 
123   @Test
spanContext_ToString()124   public void spanContext_ToString() {
125     assertThat(first.toString()).contains(TraceId.fromBytes(firstTraceIdBytes).toString());
126     assertThat(first.toString()).contains(SpanId.fromBytes(firstSpanIdBytes).toString());
127     assertThat(first.toString()).contains(TraceOptions.DEFAULT.toString());
128     assertThat(second.toString()).contains(TraceId.fromBytes(secondTraceIdBytes).toString());
129     assertThat(second.toString()).contains(SpanId.fromBytes(secondSpanIdBytes).toString());
130     assertThat(second.toString())
131         .contains(TraceOptions.builder().setIsSampled(true).build().toString());
132   }
133 }
134