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.same; 21 import static org.mockito.Mockito.verifyZeroInteractions; 22 import static org.mockito.Mockito.when; 23 24 import io.opencensus.common.Scope; 25 import java.util.concurrent.Callable; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 import org.mockito.Mock; 31 import org.mockito.MockitoAnnotations; 32 33 /** Unit tests for {@link Tracer}. */ 34 @RunWith(JUnit4.class) 35 // Need to suppress warnings for MustBeClosed because Java-6 does not support try-with-resources. 36 @SuppressWarnings("MustBeClosedChecker") 37 public class TracerTest { 38 private static final Tracer noopTracer = Tracer.getNoopTracer(); 39 private static final String SPAN_NAME = "MySpanName"; 40 @Mock private Tracer tracer; 41 @Mock private SpanBuilder spanBuilder; 42 @Mock private Span span; 43 44 @Before setUp()45 public void setUp() { 46 MockitoAnnotations.initMocks(this); 47 } 48 49 @Test defaultGetCurrentSpan()50 public void defaultGetCurrentSpan() { 51 assertThat(noopTracer.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE); 52 } 53 54 @Test(expected = NullPointerException.class) withSpan_NullSpan()55 public void withSpan_NullSpan() { 56 noopTracer.withSpan(null); 57 } 58 59 @Test getCurrentSpan_WithSpan()60 public void getCurrentSpan_WithSpan() { 61 assertThat(noopTracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 62 Scope ws = noopTracer.withSpan(span); 63 try { 64 assertThat(noopTracer.getCurrentSpan()).isSameAs(span); 65 } finally { 66 ws.close(); 67 } 68 assertThat(noopTracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 69 } 70 71 @Test wrapRunnable()72 public void wrapRunnable() { 73 Runnable runnable; 74 assertThat(noopTracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 75 runnable = 76 tracer.withSpan( 77 span, 78 new Runnable() { 79 @Override 80 public void run() { 81 assertThat(noopTracer.getCurrentSpan()).isSameAs(span); 82 } 83 }); 84 // When we run the runnable we will have the span in the current Context. 85 runnable.run(); 86 verifyZeroInteractions(span); 87 assertThat(noopTracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 88 } 89 90 @Test wrapCallable()91 public void wrapCallable() throws Exception { 92 final Object ret = new Object(); 93 Callable<Object> callable; 94 assertThat(noopTracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 95 callable = 96 tracer.withSpan( 97 span, 98 new Callable<Object>() { 99 @Override 100 public Object call() throws Exception { 101 assertThat(noopTracer.getCurrentSpan()).isSameAs(span); 102 return ret; 103 } 104 }); 105 // When we call the callable we will have the span in the current Context. 106 assertThat(callable.call()).isEqualTo(ret); 107 verifyZeroInteractions(span); 108 assertThat(noopTracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 109 } 110 111 @Test(expected = NullPointerException.class) spanBuilderWithName_NullName()112 public void spanBuilderWithName_NullName() { 113 noopTracer.spanBuilder(null); 114 } 115 116 @Test defaultSpanBuilderWithName()117 public void defaultSpanBuilderWithName() { 118 assertThat(noopTracer.spanBuilder(SPAN_NAME).startSpan()).isSameAs(BlankSpan.INSTANCE); 119 } 120 121 @Test(expected = NullPointerException.class) spanBuilderWithParentAndName_NullName()122 public void spanBuilderWithParentAndName_NullName() { 123 noopTracer.spanBuilderWithExplicitParent(null, null); 124 } 125 126 @Test defaultSpanBuilderWithParentAndName()127 public void defaultSpanBuilderWithParentAndName() { 128 assertThat(noopTracer.spanBuilderWithExplicitParent(SPAN_NAME, null).startSpan()) 129 .isSameAs(BlankSpan.INSTANCE); 130 } 131 132 @Test(expected = NullPointerException.class) spanBuilderWithRemoteParent_NullName()133 public void spanBuilderWithRemoteParent_NullName() { 134 noopTracer.spanBuilderWithRemoteParent(null, null); 135 } 136 137 @Test defaultSpanBuilderWithRemoteParent_NullParent()138 public void defaultSpanBuilderWithRemoteParent_NullParent() { 139 assertThat(noopTracer.spanBuilderWithRemoteParent(SPAN_NAME, null).startSpan()) 140 .isSameAs(BlankSpan.INSTANCE); 141 } 142 143 @Test defaultSpanBuilderWithRemoteParent()144 public void defaultSpanBuilderWithRemoteParent() { 145 assertThat(noopTracer.spanBuilderWithRemoteParent(SPAN_NAME, SpanContext.INVALID).startSpan()) 146 .isSameAs(BlankSpan.INSTANCE); 147 } 148 149 @Test startSpanWithParentFromContext()150 public void startSpanWithParentFromContext() { 151 Scope ws = tracer.withSpan(span); 152 try { 153 assertThat(tracer.getCurrentSpan()).isSameAs(span); 154 when(tracer.spanBuilderWithExplicitParent(same(SPAN_NAME), same(span))) 155 .thenReturn(spanBuilder); 156 assertThat(tracer.spanBuilder(SPAN_NAME)).isSameAs(spanBuilder); 157 } finally { 158 ws.close(); 159 } 160 } 161 162 @Test startSpanWithInvalidParentFromContext()163 public void startSpanWithInvalidParentFromContext() { 164 Scope ws = tracer.withSpan(BlankSpan.INSTANCE); 165 try { 166 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 167 when(tracer.spanBuilderWithExplicitParent(same(SPAN_NAME), same(BlankSpan.INSTANCE))) 168 .thenReturn(spanBuilder); 169 assertThat(tracer.spanBuilder(SPAN_NAME)).isSameAs(spanBuilder); 170 } finally { 171 ws.close(); 172 } 173 } 174 } 175