increase test converage.
This commit is contained in:
parent
47df503bad
commit
431dd9b5fa
|
|
@ -252,4 +252,14 @@ public class Span {
|
|||
public List<LogData> getLogs() {
|
||||
return Collections.unmodifiableList(logs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Span{" +
|
||||
"spanId=" + spanId +
|
||||
", parentSpanId=" + parentSpanId +
|
||||
", startTime=" + startTime +
|
||||
", operationName='" + operationName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,4 +124,14 @@ public class TraceSegment {
|
|||
public List<Span> getSpans() {
|
||||
return Collections.unmodifiableList(spans);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TraceSegment{" +
|
||||
"traceSegmentId='" + traceSegmentId + '\'' +
|
||||
", endTime=" + endTime +
|
||||
", primaryRef=" + primaryRef +
|
||||
", spans.size=" + spans.size() +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public class TraceSegmentRef {
|
|||
/**
|
||||
* {@link Span#spanId}
|
||||
*/
|
||||
private int spanId;
|
||||
private int spanId = -1;
|
||||
|
||||
/**
|
||||
* Create a {@link TraceSegmentRef} instance, without any data.
|
||||
|
|
@ -38,4 +38,12 @@ public class TraceSegmentRef {
|
|||
public void setSpanId(int spanId) {
|
||||
this.spanId = spanId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TraceSegmentRef{" +
|
||||
"traceSegmentId='" + traceSegmentId + '\'' +
|
||||
", spanId=" + spanId +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ public class ContextCarrier extends TraceSegmentRef implements Serializable {
|
|||
*/
|
||||
public ContextCarrier deserialize(String text) {
|
||||
if(text != null){
|
||||
String[] parts = text.split("|");
|
||||
String[] parts = text.split("\\|");
|
||||
if(parts.length == 2){
|
||||
try{
|
||||
setSpanId(Integer.parseInt(parts[1]));
|
||||
|
|
@ -42,4 +42,13 @@ public class ContextCarrier extends TraceSegmentRef implements Serializable {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure this {@link ContextCarrier} has been initialized.
|
||||
*
|
||||
* @return true for unbroken {@link ContextCarrier} or no-initialized. Otherwise, false;
|
||||
*/
|
||||
public boolean isValid(){
|
||||
return !StringUtil.isEmpty(getTraceSegmentId()) && getSpanId() > -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ public final class TracerContext {
|
|||
*
|
||||
* @param carrier holds the snapshot
|
||||
*/
|
||||
private void inject(ContextCarrier carrier) {
|
||||
public void inject(ContextCarrier carrier) {
|
||||
carrier.setTraceSegmentId(this.segment.getTraceSegmentId());
|
||||
carrier.setSpanId(this.activeSpan().getSpanId());
|
||||
}
|
||||
|
|
@ -106,7 +106,7 @@ public final class TracerContext {
|
|||
* @param carrier holds the snapshot, if get this {@link ContextCarrier} from remote, make sure {@link
|
||||
* ContextCarrier#deserialize(String)} called.
|
||||
*/
|
||||
private void extract(ContextCarrier carrier) {
|
||||
public void extract(ContextCarrier carrier) {
|
||||
this.segment.ref(carrier);
|
||||
}
|
||||
|
||||
|
|
@ -169,5 +169,12 @@ public final class TracerContext {
|
|||
listener.afterFinished(finishedSegment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the given {@link TracerContextListener}
|
||||
*/
|
||||
static synchronized void remove(TracerContextListener listener){
|
||||
listeners.remove(listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.bytebuddy;
|
||||
package com.a.eye.skywalking.bytebuddy;
|
||||
|
||||
import net.bytebuddy.implementation.bind.annotation.AllArguments;
|
||||
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.bytebuddy;
|
||||
package com.a.eye.skywalking.bytebuddy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.Callable;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.bytebuddy;
|
||||
package com.a.eye.skywalking.bytebuddy;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
|
||||
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.bytebuddy;
|
||||
package com.a.eye.skywalking.bytebuddy;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
|
||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.bytebuddy;
|
||||
package com.a.eye.skywalking.bytebuddy;
|
||||
|
||||
public class TestClass {
|
||||
public TestClass(){
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package com.a.eye.skywalking.context;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2017/2/19.
|
||||
*/
|
||||
public class ContextCarrierTest {
|
||||
@Test
|
||||
public void testSerialize(){
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
carrier.setTraceSegmentId("trace_id_A");
|
||||
carrier.setSpanId(100);
|
||||
|
||||
Assert.assertEquals("trace_id_A|100", carrier.serialize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialize(){
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
carrier.deserialize("trace_id_A|100");
|
||||
|
||||
Assert.assertEquals("trace_id_A", carrier.getTraceSegmentId());
|
||||
Assert.assertEquals(100, carrier.getSpanId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIllegalDeserialize(){
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
carrier.deserialize("abcde");
|
||||
Assert.assertFalse(carrier.isValid());
|
||||
|
||||
carrier = new ContextCarrier();
|
||||
carrier.deserialize("trace_id|-100");
|
||||
Assert.assertFalse(carrier.isValid());
|
||||
|
||||
carrier = new ContextCarrier();
|
||||
carrier.deserialize("trace_id|illegal-spanid");
|
||||
Assert.assertFalse(carrier.isValid());
|
||||
|
||||
carrier = new ContextCarrier();
|
||||
carrier.deserialize("trace_id|100|other-illegal");
|
||||
Assert.assertFalse(carrier.isValid());
|
||||
|
||||
carrier = new ContextCarrier();
|
||||
carrier.deserialize("trace_id|100");
|
||||
Assert.assertTrue(carrier.isValid());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
package com.a.eye.skywalking.context;
|
||||
|
||||
import com.a.eye.skywalking.trace.Span;
|
||||
import com.a.eye.skywalking.trace.TraceSegment;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2017/2/19.
|
||||
*/
|
||||
public class TracerContextTest {
|
||||
@Test
|
||||
public void testSpanLifeCycle(){
|
||||
TracerContext context = new TracerContext();
|
||||
Span span = context.createSpan("/serviceA");
|
||||
|
||||
Assert.assertEquals(span, context.activeSpan());
|
||||
|
||||
TracerContext.ListenerManager.add(TestTracerContextListener.INSTANCE);
|
||||
final TraceSegment[] finishedSegmentCarrier = TestTracerContextListener.INSTANCE.finishedSegmentCarrier;
|
||||
context.stopSpan(span);
|
||||
|
||||
Assert.assertNotNull(finishedSegmentCarrier[0]);
|
||||
Assert.assertEquals(1, finishedSegmentCarrier[0].getSpans().size());
|
||||
Assert.assertEquals(span, finishedSegmentCarrier[0].getSpans().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChildOfSpan(){
|
||||
TracerContext context = new TracerContext();
|
||||
Span serviceSpan = context.createSpan("/serviceA");
|
||||
Span dbSpan = context.createSpan("db/preparedStatement/execute");
|
||||
|
||||
Assert.assertEquals(dbSpan, context.activeSpan());
|
||||
|
||||
TracerContext.ListenerManager.add(TestTracerContextListener.INSTANCE);
|
||||
final TraceSegment[] finishedSegmentCarrier = TestTracerContextListener.INSTANCE.finishedSegmentCarrier;
|
||||
|
||||
try {
|
||||
context.stopSpan(serviceSpan);
|
||||
}catch (Throwable t){
|
||||
Assert.assertTrue(t instanceof IllegalStateException);
|
||||
}
|
||||
|
||||
context.stopSpan(dbSpan);
|
||||
context.stopSpan(serviceSpan);
|
||||
|
||||
Assert.assertNotNull(finishedSegmentCarrier[0]);
|
||||
Assert.assertEquals(2, finishedSegmentCarrier[0].getSpans().size());
|
||||
Assert.assertEquals(dbSpan, finishedSegmentCarrier[0].getSpans().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInject(){
|
||||
TracerContext context = new TracerContext();
|
||||
Span serviceSpan = context.createSpan("/serviceA");
|
||||
Span dbSpan = context.createSpan("db/preparedStatement/execute");
|
||||
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
context.inject(carrier);
|
||||
|
||||
Assert.assertTrue(carrier.isValid());
|
||||
Assert.assertEquals(1, carrier.getSpanId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtract(){
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
carrier.setTraceSegmentId("trace_id_1");
|
||||
carrier.setSpanId(5);
|
||||
|
||||
Assert.assertTrue(carrier.isValid());
|
||||
|
||||
TracerContext context = new TracerContext();
|
||||
context.extract(carrier);
|
||||
Span span = context.createSpan("/serviceC");
|
||||
|
||||
TracerContext.ListenerManager.add(TestTracerContextListener.INSTANCE);
|
||||
final TraceSegment[] finishedSegmentCarrier = TestTracerContextListener.INSTANCE.finishedSegmentCarrier;
|
||||
|
||||
context.stopSpan(span);
|
||||
|
||||
Assert.assertEquals("trace_id_1", finishedSegmentCarrier[0].getPrimaryRef().getTraceSegmentId());
|
||||
Assert.assertEquals(5, finishedSegmentCarrier[0].getPrimaryRef().getSpanId());
|
||||
}
|
||||
|
||||
@After
|
||||
public void reset(){
|
||||
TracerContext.ListenerManager.remove(TestTracerContextListener.INSTANCE);
|
||||
}
|
||||
|
||||
public enum TestTracerContextListener implements TracerContextListener {
|
||||
INSTANCE;
|
||||
final TraceSegment[] finishedSegmentCarrier = {null};
|
||||
|
||||
@Override public void afterFinished(TraceSegment traceSegment) {
|
||||
finishedSegmentCarrier[0] = traceSegment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.plugin;
|
||||
package com.a.eye.skywalking.plugin;
|
||||
|
||||
public class BeInterceptedClass {
|
||||
public BeInterceptedClass(){
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
package test.a.eye.cloud.plugin;
|
||||
package com.a.eye.skywalking.plugin;
|
||||
|
||||
import com.a.eye.skywalking.plugin.PluginException;
|
||||
import com.a.eye.skywalking.plugin.TracingBootstrap;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
|
@ -9,7 +7,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||
public class PluginMainTest {
|
||||
@Test
|
||||
public void testMain() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, PluginException {
|
||||
TracingBootstrap.main(new String[] {"test.a.eye.cloud.plugin.PluginMainTest"});
|
||||
TracingBootstrap.main(new String[] {"PluginMainTest"});
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
|
|
@ -17,7 +15,7 @@ public class PluginMainTest {
|
|||
SecurityException {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
BeInterceptedClass inst = (BeInterceptedClass) Class.forName("test.a.eye.cloud.plugin.BeInterceptedClass").newInstance();
|
||||
BeInterceptedClass inst = (BeInterceptedClass) Class.forName("BeInterceptedClass").newInstance();
|
||||
inst.printabc();
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println(end - start + "ms");
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.plugin;
|
||||
package com.a.eye.skywalking.plugin;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.plugin;
|
||||
package com.a.eye.skywalking.plugin;
|
||||
|
||||
import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
|
||||
import com.a.eye.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.plugin;
|
||||
package com.a.eye.skywalking.plugin;
|
||||
|
||||
import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint;
|
||||
import com.a.eye.skywalking.plugin.interceptor.InstanceMethodsInterceptPoint;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package test.a.eye.cloud.plugin;
|
||||
package com.a.eye.skywalking.plugin;
|
||||
|
||||
import com.a.eye.skywalking.plugin.interceptor.enhance.MethodInterceptResult;
|
||||
import com.a.eye.skywalking.plugin.interceptor.enhance.MethodInvokeContext;
|
||||
Loading…
Reference in New Issue