use of io.opentracing.mock.MockSpan in project camel by apache.
the class SqlSpanDecoratorTest method testPre.
@Test
public void testPre() {
Endpoint endpoint = Mockito.mock(Endpoint.class);
Exchange exchange = Mockito.mock(Exchange.class);
Message message = Mockito.mock(Message.class);
Mockito.when(endpoint.getEndpointUri()).thenReturn("test");
Mockito.when(exchange.getIn()).thenReturn(message);
Mockito.when(message.getHeader(SqlSpanDecorator.CAMEL_SQL_QUERY)).thenReturn(SQL_STATEMENT);
SpanDecorator decorator = new SqlSpanDecorator();
MockTracer tracer = new MockTracer();
MockSpan span = (MockSpan) tracer.buildSpan("TestSpan").start();
decorator.pre(span, exchange, endpoint);
assertEquals("sql", span.tags().get(Tags.DB_TYPE.getKey()));
assertEquals(SQL_STATEMENT, span.tags().get(Tags.DB_STATEMENT.getKey()));
}
use of io.opentracing.mock.MockSpan 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());
}
use of io.opentracing.mock.MockSpan in project opentracing-java by opentracing.
the class HandlerTest method parent_not_picked_up.
/**
* active parent is not picked up by child
*/
@Test
public void parent_not_picked_up() throws Exception {
try (Scope parent = tracer.buildSpan("parent").startActive(true)) {
String response = client.send("no_parent").get(15, TimeUnit.SECONDS);
assertEquals("no_parent:response", response);
}
List<MockSpan> finished = tracer.finishedSpans();
assertEquals(2, finished.size());
MockSpan child = getOneByOperationName(finished, RequestHandler.OPERATION_NAME);
assertNotNull(child);
MockSpan parent = getOneByOperationName(finished, "parent");
assertNotNull(parent);
// Here check that there is no parent-child relation although it should be because child is
// created when parent is active
assertNotEquals(parent.context().spanId(), child.parentId());
}
use of io.opentracing.mock.MockSpan in project opentracing-java by opentracing.
the class NestedCallbacksTest method test.
@Test
public void test() throws Exception {
try (Scope scope = tracer.buildSpan("one").startActive(false)) {
submitCallbacks(scope.span());
}
await().atMost(15, TimeUnit.SECONDS).until(finishedSpansSize(tracer), equalTo(1));
List<MockSpan> spans = tracer.finishedSpans();
assertEquals(1, spans.size());
assertEquals("one", spans.get(0).operationName());
Map<String, Object> tags = spans.get(0).tags();
assertEquals(3, tags.size());
for (int i = 1; i <= 3; i++) {
assertEquals(Integer.toString(i), tags.get("key" + i));
}
assertNull(tracer.scopeManager().active());
}
use of io.opentracing.mock.MockSpan in project opentracing-java by opentracing.
the class PromisePropagationTest method testPromiseCallback.
@Test
public void testPromiseCallback() {
// register test thread
phaser.register();
final AtomicReference<String> successResult1 = new AtomicReference<>();
final AtomicReference<String> successResult2 = new AtomicReference<>();
final AtomicReference<Throwable> errorResult = new AtomicReference<>();
try (PromiseContext context = new PromiseContext(phaser, 3)) {
try (Scope parent = tracer.buildSpan("promises").withTag(Tags.COMPONENT.getKey(), "example-promises").startActive(true)) {
Promise<String> successPromise = new Promise<>(context, tracer);
successPromise.onSuccess(new Promise.SuccessCallback<String>() {
@Override
public void accept(String s) {
tracer.scopeManager().active().span().log("Promised 1 " + s);
successResult1.set(s);
// result set
phaser.arriveAndAwaitAdvance();
}
});
successPromise.onSuccess(new Promise.SuccessCallback<String>() {
@Override
public void accept(String s) {
tracer.scopeManager().active().span().log("Promised 2 " + s);
successResult2.set(s);
// result set
phaser.arriveAndAwaitAdvance();
}
});
Promise errorPromise = new Promise(context, tracer);
errorPromise.onError(new Promise.ErrorCallback() {
@Override
public void accept(Throwable t) {
errorResult.set(t);
// result set
phaser.arriveAndAwaitAdvance();
}
});
assertThat(tracer.finishedSpans().size()).isEqualTo(0);
successPromise.success("success!");
errorPromise.error(new Exception("some error."));
}
// wait for results to be set
phaser.arriveAndAwaitAdvance();
assertThat(successResult1.get()).isEqualTo("success!");
assertThat(successResult2.get()).isEqualTo("success!");
assertThat(errorResult.get()).hasMessage("some error.");
// wait for traces to be reported
phaser.arriveAndAwaitAdvance();
List<MockSpan> finished = tracer.finishedSpans();
assertThat(finished.size()).isEqualTo(4);
assertThat(getOneByTag(finished, Tags.COMPONENT, "example-promises")).isNotNull();
assertThat(getOneByTag(finished, Tags.COMPONENT, "example-promises").parentId()).isEqualTo(0);
long parentId = getOneByTag(finished, Tags.COMPONENT, "example-promises").context().spanId();
assertThat(getByTag(finished, Tags.COMPONENT, "success")).hasSize(2);
for (MockSpan span : getByTag(finished, Tags.COMPONENT, "success")) {
assertThat(span.parentId()).isEqualTo(parentId);
}
assertThat(getOneByTag(finished, Tags.COMPONENT, "error")).isNotNull();
assertThat(getOneByTag(finished, Tags.COMPONENT, "error").parentId()).isEqualTo(parentId);
}
}
Aggregations