use of io.opentracing.Scope in project opentracing-java by opentracing.
the class ThreadLocalScopeManagerTest method dontFinishSpanNoClose.
@Test
public void dontFinishSpanNoClose() throws Exception {
Span span = mock(Span.class);
// We can't use 1.7 features like try-with-resources in this repo without meddling with pom details for tests.
Scope scope = source.activate(span, false);
try {
assertNotNull(scope);
assertNotNull(source.active());
} finally {
scope.close();
}
// Make sure the Span did *not* get finish()ed.
verify(span, never()).finish();
// Verify it's gone.
assertNull(source.active());
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class ThreadLocalScopeManagerTest method finishSpanClose.
@Test
public void finishSpanClose() throws Exception {
Span span = mock(Span.class);
// We can't use 1.7 features like try-with-resources in this repo without meddling with pom details for tests.
Scope scope = source.activate(span, true);
try {
assertNotNull(scope);
assertNotNull(source.active());
} finally {
scope.close();
}
// Make sure the Span got finish()ed.
verify(span, times(1)).finish();
// Verify it's gone.
assertNull(source.active());
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class ThreadLocalScopeTest method implicitSpanStack.
@Test
public void implicitSpanStack() throws Exception {
Span backgroundSpan = mock(Span.class);
Span foregroundSpan = mock(Span.class);
// Quasi try-with-resources (this is 1.6).
Scope backgroundActive = scopeManager.activate(backgroundSpan, true);
try {
assertNotNull(backgroundActive);
// Activate a new Scope on top of the background one.
Scope foregroundActive = scopeManager.activate(foregroundSpan, true);
try {
Scope shouldBeForeground = scopeManager.active();
assertEquals(foregroundActive, shouldBeForeground);
} finally {
foregroundActive.close();
}
// And now the backgroundActive should be reinstated.
Scope shouldBeBackground = scopeManager.active();
assertEquals(backgroundActive, shouldBeBackground);
} finally {
backgroundActive.close();
}
// The background and foreground Spans should be finished.
verify(backgroundSpan, times(1)).finish();
verify(foregroundSpan, times(1)).finish();
// And now nothing is active.
Scope missingSpan = scopeManager.active();
assertNull(missingSpan);
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class ThreadLocalScopeTest method testDeactivateWhenDifferentSpanIsActive.
@Test
public void testDeactivateWhenDifferentSpanIsActive() {
Span span = mock(Span.class);
Scope active = scopeManager.activate(span, false);
scopeManager.activate(mock(Span.class), false);
active.close();
verify(span, times(0)).finish();
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class HandlerTest method bad_solution_to_set_parent.
/**
* Solution is bad because parent is per client (we don't have better choice).
* Therefore all client requests will have the same parent.
* But if client is long living and injected/reused in different places then initial parent will not be correct.
*/
@Test
public void bad_solution_to_set_parent() throws Exception {
Client client;
try (Scope parent = tracer.buildSpan("parent").startActive(true)) {
client = new Client(new RequestHandler(tracer, parent.span().context()));
String response = client.send("correct_parent").get(15, TimeUnit.SECONDS);
assertEquals("correct_parent:response", response);
}
// Send second request, now there is no active parent, but it will be set, ups
String response = client.send("wrong_parent").get(15, TimeUnit.SECONDS);
assertEquals("wrong_parent:response", response);
List<MockSpan> finished = tracer.finishedSpans();
assertEquals(3, finished.size());
sortByStartMicros(finished);
MockSpan parent = getOneByOperationName(finished, "parent");
assertNotNull(parent);
// now there is parent/child relation between first and second span:
assertEquals(parent.context().spanId(), finished.get(1).parentId());
// third span should not have parent, but it has, damn it
assertEquals(parent.context().spanId(), finished.get(2).parentId());
}
Aggregations