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.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import io.opencensus.common.Scope; 24 import io.opencensus.trace.Span.Kind; 25 import io.opencensus.trace.samplers.Samplers; 26 import java.util.Collections; 27 import java.util.concurrent.Callable; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 35 /** Unit tests for {@link SpanBuilder}. */ 36 @RunWith(JUnit4.class) 37 // Need to suppress warnings for MustBeClosed because Java-6 does not support try-with-resources. 38 @SuppressWarnings("MustBeClosedChecker") 39 public class SpanBuilderTest { 40 private final Tracer tracer = Tracing.getTracer(); 41 @Mock private SpanBuilder spanBuilder; 42 @Mock private Span span; 43 44 @Before setUp()45 public void setUp() { 46 MockitoAnnotations.initMocks(this); 47 when(spanBuilder.startSpan()).thenReturn(span); 48 } 49 50 @Test startScopedSpan()51 public void startScopedSpan() { 52 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 53 Scope scope = spanBuilder.startScopedSpan(); 54 try { 55 assertThat(tracer.getCurrentSpan()).isSameAs(span); 56 } finally { 57 scope.close(); 58 } 59 verify(span).end(EndSpanOptions.DEFAULT); 60 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 61 } 62 63 @Test startSpanAndRun()64 public void startSpanAndRun() { 65 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 66 spanBuilder.startSpanAndRun( 67 new Runnable() { 68 @Override 69 public void run() { 70 assertThat(tracer.getCurrentSpan()).isSameAs(span); 71 } 72 }); 73 verify(span).end(EndSpanOptions.DEFAULT); 74 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 75 } 76 77 @Test startSpanAndCall()78 public void startSpanAndCall() throws Exception { 79 final Object ret = new Object(); 80 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 81 assertThat( 82 spanBuilder.startSpanAndCall( 83 new Callable<Object>() { 84 @Override 85 public Object call() throws Exception { 86 assertThat(tracer.getCurrentSpan()).isSameAs(span); 87 return ret; 88 } 89 })) 90 .isEqualTo(ret); 91 verify(span).end(EndSpanOptions.DEFAULT); 92 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE); 93 } 94 95 @Test doNotCrash_NoopImplementation()96 public void doNotCrash_NoopImplementation() throws Exception { 97 SpanBuilder spanBuilder = tracer.spanBuilder("MySpanName"); 98 spanBuilder.setParentLinks(Collections.<Span>emptyList()); 99 spanBuilder.setRecordEvents(true); 100 spanBuilder.setSampler(Samplers.alwaysSample()); 101 spanBuilder.setSpanKind(Kind.SERVER); 102 assertThat(spanBuilder.startSpan()).isSameAs(BlankSpan.INSTANCE); 103 } 104 } 105