MockTracer implements Tracer { @Override public SpanBuilder buildSpan(String operationName) { return new SpanBuilder(operationName); } MockTracer();  MockTracer(ScopeManager scopeManager);  MockTracer(ScopeManager scopeManager, Propagator propagator);  MockTracer(Propagator propagator); synchronized void reset(); synchronized List<MockSpan> finishedSpans(); synchronized Map<String, Map<String, MockSpan>> finishedTraces(); @Override ScopeManager scopeManager(); @Override SpanBuilder buildSpan(String operationName); @Override void inject(SpanContext spanContext, Format<C> format, C carrier); @Override SpanContext extract(Format<C> format, C carrier); @Override Span activeSpan(); @Override Scope activateSpan(Span span); @Override synchronized void close();  }
@Test public void testFollowFromReference() { MockTracer tracer = new MockTracer(MockTracer.Propagator.TEXT_MAP); final MockSpan precedent = tracer.buildSpan("precedent").start(); final MockSpan followingSpan = tracer.buildSpan("follows") .addReference(References.FOLLOWS_FROM, precedent.context()) .start(); assertEquals(precedent.context().spanId(), followingSpan.parentId()); assertEquals(1, followingSpan.references().size()); final MockSpan.Reference followsFromRef = followingSpan.references().get(0); assertEquals(new MockSpan.Reference(precedent.context(), References.FOLLOWS_FROM), followsFromRef); }
@Test public void testMultiReferences() { MockTracer tracer = new MockTracer(MockTracer.Propagator.TEXT_MAP); final MockSpan parent = tracer.buildSpan("parent").start(); final MockSpan precedent = tracer.buildSpan("precedent").start(); final MockSpan followingSpan = tracer.buildSpan("follows") .addReference(References.FOLLOWS_FROM, precedent.context()) .asChildOf(parent.context()) .start(); assertEquals(parent.context().spanId(), followingSpan.parentId()); assertEquals(2, followingSpan.references().size()); final MockSpan.Reference followsFromRef = followingSpan.references().get(0); final MockSpan.Reference parentRef = followingSpan.references().get(1); assertEquals(new MockSpan.Reference(precedent.context(), References.FOLLOWS_FROM), followsFromRef); assertEquals(new MockSpan.Reference(parent.context(), References.CHILD_OF), parentRef); }
@Test public void testMultiReferencesBaggage() { MockTracer tracer = new MockTracer(MockTracer.Propagator.TEXT_MAP); final MockSpan parent = tracer.buildSpan("parent").start(); parent.setBaggageItem("parent", "foo"); final MockSpan precedent = tracer.buildSpan("precedent").start(); precedent.setBaggageItem("precedent", "bar"); final MockSpan followingSpan = tracer.buildSpan("follows") .addReference(References.FOLLOWS_FROM, precedent.context()) .asChildOf(parent.context()) .start(); assertEquals("foo", followingSpan.getBaggageItem("parent")); assertEquals("bar", followingSpan.getBaggageItem("precedent")); }
@Test public void testNonStandardReference() { MockTracer tracer = new MockTracer(MockTracer.Propagator.TEXT_MAP); final MockSpan parent = tracer.buildSpan("parent").start(); final MockSpan nextSpan = tracer.buildSpan("follows") .addReference("a_reference", parent.context()) .start(); assertEquals(parent.context().spanId(), nextSpan.parentId()); assertEquals(1, nextSpan.references().size()); assertEquals(nextSpan.references().get(0), new MockSpan.Reference(parent.context(), "a_reference")); }
@Test public void testChildOfWithNullParentDoesNotThrowException() { MockTracer tracer = new MockTracer(); final Span parent = null; Span span = tracer.buildSpan("foo").asChildOf(parent).start(); span.finish(); }