Convert Trace Junit tests to Spock
This commit is contained in:
parent
cc274cb3be
commit
9f5f2e9a1d
|
@ -0,0 +1,54 @@
|
|||
package datadog.opentracing
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.google.common.collect.Maps
|
||||
import spock.lang.Specification
|
||||
|
||||
class DDSpanSerializationTest extends Specification {
|
||||
|
||||
def "serialize spans"() throws Exception {
|
||||
setup:
|
||||
final Map<String, String> baggage = new HashMap<>()
|
||||
baggage.put("a-baggage", "value")
|
||||
final Map<String, Object> tags = new HashMap<>()
|
||||
baggage.put("k1", "v1")
|
||||
|
||||
Map<String, Object> expected = Maps.newHashMap()
|
||||
expected.put("meta", baggage)
|
||||
expected.put("service", "service")
|
||||
expected.put("error", 0)
|
||||
expected.put("type", "type")
|
||||
expected.put("name", "operation")
|
||||
expected.put("duration", 33000)
|
||||
expected.put("resource", "operation")
|
||||
expected.put("start", 100000)
|
||||
expected.put("span_id", 2l)
|
||||
expected.put("parent_id", 0l)
|
||||
expected.put("trace_id", 1l)
|
||||
|
||||
final DDSpanContext context =
|
||||
new DDSpanContext(
|
||||
1L,
|
||||
2L,
|
||||
0L,
|
||||
"service",
|
||||
"operation",
|
||||
null,
|
||||
new HashMap<>(baggage),
|
||||
false,
|
||||
"type",
|
||||
tags,
|
||||
null,
|
||||
null)
|
||||
|
||||
baggage.put(DDTags.THREAD_NAME, Thread.currentThread().getName())
|
||||
baggage.put(DDTags.THREAD_ID, String.valueOf(Thread.currentThread().getId()))
|
||||
|
||||
DDSpan span = new DDSpan(100L, context)
|
||||
span.finish(133L)
|
||||
ObjectMapper serializer = new ObjectMapper()
|
||||
|
||||
expect:
|
||||
serializer.readTree(serializer.writeValueAsString(span)) == serializer.readTree(serializer.writeValueAsString(expected))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package datadog.opentracing
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
class DDSpanTest extends Specification {
|
||||
|
||||
def "getters and setters"() {
|
||||
setup:
|
||||
final DDSpanContext context =
|
||||
new DDSpanContext(
|
||||
1L,
|
||||
1L,
|
||||
0L,
|
||||
"fakeService",
|
||||
"fakeOperation",
|
||||
"fakeResource",
|
||||
Collections.<String, String>emptyMap(),
|
||||
false,
|
||||
"fakeType",
|
||||
null,
|
||||
null,
|
||||
null)
|
||||
|
||||
final DDSpan span = new DDSpan(1L, context)
|
||||
|
||||
when:
|
||||
span.setServiceName("service")
|
||||
then:
|
||||
span.getServiceName() == "service"
|
||||
|
||||
when:
|
||||
span.setOperationName("operation")
|
||||
then:
|
||||
span.getOperationName() == "operation"
|
||||
|
||||
when:
|
||||
span.setResourceName("resource")
|
||||
then:
|
||||
span.getResourceName() == "resource"
|
||||
|
||||
when:
|
||||
span.setSpanType("type")
|
||||
then:
|
||||
span.getType() == "type"
|
||||
}
|
||||
|
||||
def "resource name equals operation name if null"() {
|
||||
setup:
|
||||
final String opName = "operationName"
|
||||
DDSpan span
|
||||
|
||||
when:
|
||||
span = new DDTracer().buildSpan(opName).startManual()
|
||||
then:
|
||||
span.getResourceName() == opName
|
||||
span.getServiceName() == DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME
|
||||
|
||||
when:
|
||||
final String resourceName = "fake"
|
||||
final String serviceName = "myService"
|
||||
span = new DDTracer()
|
||||
.buildSpan(opName)
|
||||
.withResourceName(resourceName)
|
||||
.withServiceName(serviceName)
|
||||
.startManual()
|
||||
then:
|
||||
span.getResourceName() == resourceName
|
||||
span.getServiceName() == serviceName
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package datadog.opentracing.propagation
|
||||
|
||||
import com.datadoghq.trace.DDSpanContext
|
||||
import io.opentracing.propagation.TextMapExtractAdapter
|
||||
import io.opentracing.propagation.TextMapInjectAdapter
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
class HTTPCodecTest extends Specification {
|
||||
@Shared
|
||||
private static final String OT_BAGGAGE_PREFIX = "ot-baggage-"
|
||||
@Shared
|
||||
private static final String TRACE_ID_KEY = "x-datadog-trace-id"
|
||||
@Shared
|
||||
private static final String SPAN_ID_KEY = "x-datadog-parent-id"
|
||||
|
||||
def "inject http headers"() {
|
||||
setup:
|
||||
final DDSpanContext mockedContext =
|
||||
new DDSpanContext(
|
||||
1L,
|
||||
2L,
|
||||
0L,
|
||||
"fakeService",
|
||||
"fakeOperation",
|
||||
"fakeResource",
|
||||
new HashMap<String, String>() {
|
||||
{
|
||||
put("k1", "v1")
|
||||
put("k2", "v2")
|
||||
}
|
||||
},
|
||||
false,
|
||||
"fakeType",
|
||||
null,
|
||||
null,
|
||||
null)
|
||||
|
||||
final Map<String, String> carrier = new HashMap<>()
|
||||
|
||||
final HTTPCodec codec = new HTTPCodec()
|
||||
codec.inject(mockedContext, new TextMapInjectAdapter(carrier))
|
||||
|
||||
expect:
|
||||
carrier.get(TRACE_ID_KEY) == "1"
|
||||
carrier.get(SPAN_ID_KEY) == "2"
|
||||
carrier.get(OT_BAGGAGE_PREFIX + "k1") == "v1"
|
||||
carrier.get(OT_BAGGAGE_PREFIX + "k2") == "v2"
|
||||
}
|
||||
|
||||
def "extract http headers"() {
|
||||
setup:
|
||||
final Map<String, String> actual =
|
||||
new HashMap<String, String>() {
|
||||
{
|
||||
put(TRACE_ID_KEY.toUpperCase(), "1")
|
||||
put(SPAN_ID_KEY.toUpperCase(), "2")
|
||||
put(OT_BAGGAGE_PREFIX.toUpperCase() + "k1", "v1")
|
||||
put(OT_BAGGAGE_PREFIX.toUpperCase() + "k2", "v2")
|
||||
}
|
||||
}
|
||||
|
||||
final HTTPCodec codec = new HTTPCodec()
|
||||
final DDSpanContext context = codec.extract(new TextMapExtractAdapter(actual))
|
||||
|
||||
expect:
|
||||
context.getTraceId() == 1l
|
||||
context.getSpanId() == 2l
|
||||
context.getBaggageItem("k1") == "v1"
|
||||
context.getBaggageItem("k2") == "v2"
|
||||
}
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
package datadog.opentracing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.collect.Maps;
|
||||
import datadog.trace.api.DDTags;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DDSpanSerializationTest {
|
||||
|
||||
ObjectMapper serializer;
|
||||
DDSpan span;
|
||||
Map<String, Object> expected = Maps.newHashMap();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
final Map<String, String> baggage = new HashMap<>();
|
||||
baggage.put("a-baggage", "value");
|
||||
final Map<String, Object> tags = new HashMap<>();
|
||||
baggage.put("k1", "v1");
|
||||
|
||||
expected.put("meta", baggage);
|
||||
expected.put("service", "service");
|
||||
expected.put("error", 0);
|
||||
expected.put("type", "type");
|
||||
expected.put("name", "operation");
|
||||
expected.put("duration", 33000);
|
||||
expected.put("resource", "operation");
|
||||
expected.put("start", 100000);
|
||||
expected.put("span_id", 2l);
|
||||
expected.put("parent_id", 0l);
|
||||
expected.put("trace_id", 1l);
|
||||
|
||||
final DDSpanContext context =
|
||||
new DDSpanContext(
|
||||
1L,
|
||||
2L,
|
||||
0L,
|
||||
"service",
|
||||
"operation",
|
||||
null,
|
||||
new HashMap<>(baggage),
|
||||
false,
|
||||
"type",
|
||||
tags,
|
||||
null,
|
||||
null);
|
||||
|
||||
baggage.put(DDTags.THREAD_NAME, Thread.currentThread().getName());
|
||||
baggage.put(DDTags.THREAD_ID, String.valueOf(Thread.currentThread().getId()));
|
||||
|
||||
span = new DDSpan(100L, context);
|
||||
span.finish(133L);
|
||||
serializer = new ObjectMapper();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
assertThat(serializer.readTree(serializer.writeValueAsString(span)))
|
||||
.isEqualTo(serializer.readTree(serializer.writeValueAsString(expected)));
|
||||
}
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
package datadog.opentracing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Collections;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DDSpanTest {
|
||||
|
||||
@Test
|
||||
public void testGetterSetter() {
|
||||
|
||||
final DDSpanContext context =
|
||||
new DDSpanContext(
|
||||
1L,
|
||||
1L,
|
||||
0L,
|
||||
"fakeService",
|
||||
"fakeOperation",
|
||||
"fakeResource",
|
||||
Collections.<String, String>emptyMap(),
|
||||
false,
|
||||
"fakeType",
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
String expected;
|
||||
final DDSpan span = new DDSpan(1L, context);
|
||||
|
||||
expected = "service";
|
||||
span.setServiceName(expected);
|
||||
assertThat(span.getServiceName()).isEqualTo(expected);
|
||||
|
||||
expected = "operation";
|
||||
span.setOperationName(expected);
|
||||
assertThat(span.getOperationName()).isEqualTo(expected);
|
||||
|
||||
expected = "resource";
|
||||
span.setResourceName(expected);
|
||||
assertThat(span.getResourceName()).isEqualTo(expected);
|
||||
|
||||
expected = "type";
|
||||
span.setSpanType(expected);
|
||||
assertThat(span.getType()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldResourceNameEqualsOperationNameIfNull() {
|
||||
|
||||
final String expectedName = "operationName";
|
||||
|
||||
DDSpan span = new DDTracer().buildSpan(expectedName).startManual();
|
||||
// ResourceName = expectedName
|
||||
assertThat(span.getResourceName()).isEqualTo(expectedName);
|
||||
assertThat(span.getServiceName()).isEqualTo(DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME);
|
||||
|
||||
// ResourceName = expectedResourceName
|
||||
final String expectedResourceName = "fake";
|
||||
span =
|
||||
new DDTracer()
|
||||
.buildSpan(expectedName)
|
||||
.withResourceName(expectedResourceName)
|
||||
.withServiceName("foo")
|
||||
.startManual();
|
||||
|
||||
assertThat(span.getResourceName()).isEqualTo(expectedResourceName);
|
||||
assertThat(span.getServiceName()).isEqualTo("foo");
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
package datadog.opentracing.propagation;
|
||||
|
||||
import static org.assertj.core.api.Java6Assertions.assertThat;
|
||||
|
||||
import datadog.opentracing.DDSpanContext;
|
||||
import io.opentracing.propagation.TextMapExtractAdapter;
|
||||
import io.opentracing.propagation.TextMapInjectAdapter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
/** Created by gpolaert on 6/23/17. */
|
||||
public class HTTPCodecTest {
|
||||
|
||||
private static final String OT_BAGGAGE_PREFIX = "ot-baggage-";
|
||||
private static final String TRACE_ID_KEY = "x-datadog-trace-id";
|
||||
private static final String SPAN_ID_KEY = "x-datadog-parent-id";
|
||||
|
||||
@Test
|
||||
public void shoudAddHttpHeaders() {
|
||||
|
||||
final DDSpanContext mockedContext =
|
||||
new DDSpanContext(
|
||||
1L,
|
||||
2L,
|
||||
0L,
|
||||
"fakeService",
|
||||
"fakeOperation",
|
||||
"fakeResource",
|
||||
new HashMap<String, String>() {
|
||||
{
|
||||
put("k1", "v1");
|
||||
put("k2", "v2");
|
||||
}
|
||||
},
|
||||
false,
|
||||
"fakeType",
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
final Map<String, String> carrier = new HashMap<>();
|
||||
|
||||
final HTTPCodec codec = new HTTPCodec();
|
||||
codec.inject(mockedContext, new TextMapInjectAdapter(carrier));
|
||||
|
||||
assertThat(carrier.get(TRACE_ID_KEY)).isEqualTo("1");
|
||||
assertThat(carrier.get(SPAN_ID_KEY)).isEqualTo("2");
|
||||
assertThat(carrier.get(OT_BAGGAGE_PREFIX + "k1")).isEqualTo("v1");
|
||||
assertThat(carrier.get(OT_BAGGAGE_PREFIX + "k2")).isEqualTo("v2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shoudReadHttpHeaders() {
|
||||
|
||||
final Map<String, String> actual =
|
||||
new HashMap<String, String>() {
|
||||
{
|
||||
put(TRACE_ID_KEY.toUpperCase(), "1");
|
||||
put(SPAN_ID_KEY.toUpperCase(), "2");
|
||||
put(OT_BAGGAGE_PREFIX.toUpperCase() + "k1", "v1");
|
||||
put(OT_BAGGAGE_PREFIX.toUpperCase() + "k2", "v2");
|
||||
}
|
||||
};
|
||||
|
||||
final HTTPCodec codec = new HTTPCodec();
|
||||
final DDSpanContext context = codec.extract(new TextMapExtractAdapter(actual));
|
||||
|
||||
assertThat(context.getTraceId()).isEqualTo(1l);
|
||||
assertThat(context.getSpanId()).isEqualTo(2l);
|
||||
assertThat(context.getBaggageItem("k1")).isEqualTo("v1");
|
||||
assertThat(context.getBaggageItem("k2")).isEqualTo("v2");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue