Skip to content

Commit

Permalink
[pinpoint-apm#3396] Add verification for recorded exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Xylus committed Oct 23, 2017
1 parent f94f21c commit cfaa3e8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,55 @@ public static Object anyAnnotationValue() {
}

public static ExpectedTrace root(String serviceType, Member method, String rpc, String endPoint, String remoteAddr, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.ROOT, serviceType, method, null, rpc, endPoint, remoteAddr, null, annotations, null);
return new ExpectedTrace(TraceType.ROOT, serviceType, method, null, null, rpc, endPoint, remoteAddr, null, annotations, null);
}

public static ExpectedTrace root(String serviceType, Member method, Exception exception, String rpc, String endPoint, String remoteAddr, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.ROOT, serviceType, method, null, exception, rpc, endPoint, remoteAddr, null, annotations, null);
}

public static ExpectedTrace root(String serviceType, String methodDescriptor, String rpc, String endPoint, String remoteAddr, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.ROOT, serviceType, null, methodDescriptor, rpc, endPoint, remoteAddr, null, annotations, null);
return new ExpectedTrace(TraceType.ROOT, serviceType, null, methodDescriptor, null, rpc, endPoint, remoteAddr, null, annotations, null);
}

public static ExpectedTrace root(String serviceType, String methodDescriptor, Exception exception, String rpc, String endPoint, String remoteAddr, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.ROOT, serviceType, null, methodDescriptor, exception, rpc, endPoint, remoteAddr, null, annotations, null);
}

public static ExpectedTrace event(String serviceType, Member method, String rpc, String endPoint, String destinationId, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.EVENT, serviceType, method, null, rpc, endPoint, null, destinationId, annotations, null);
return new ExpectedTrace(TraceType.EVENT, serviceType, method, null, null, rpc, endPoint, null, destinationId, annotations, null);
}


public static ExpectedTrace event(String serviceType, Member method, Exception exception, String rpc, String endPoint, String destinationId, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.EVENT, serviceType, method, null, exception, rpc, endPoint, null, destinationId, annotations, null);
}

public static ExpectedTrace event(String serviceType, Member method, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.EVENT, serviceType, method, null, null, null, null, null, annotations, null);
return new ExpectedTrace(TraceType.EVENT, serviceType, method, null, null, null, null, null, null, annotations, null);
}

public static ExpectedTrace event(String serviceType, Member method, Exception exception, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.EVENT, serviceType, method, null, exception, null, null, null, null, annotations, null);
}

public static ExpectedTrace event(String serviceType, String methodDescriptor, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.EVENT, serviceType, null, methodDescriptor, null, null, null, null, annotations, null);
return new ExpectedTrace(TraceType.EVENT, serviceType, null, methodDescriptor, null, null, null, null, null, annotations, null);
}

public static ExpectedTrace event(String serviceType, String methodDescriptor, Exception exception, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.EVENT, serviceType, null, methodDescriptor, exception, null, null, null, null, annotations, null);
}

public static ExpectedTrace event(String serviceType, String methodDescriptor, String rpc, String endPoint, String destinationId, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.EVENT, serviceType, null, methodDescriptor, rpc, endPoint, null, destinationId, annotations, null);
return new ExpectedTrace(TraceType.EVENT, serviceType, null, methodDescriptor, null, rpc, endPoint, null, destinationId, annotations, null);
}

public static ExpectedTrace event(String serviceType, String methodDescriptor, Exception exception, String rpc, String endPoint, String destinationId, ExpectedAnnotation... annotations) {
return new ExpectedTrace(TraceType.EVENT, serviceType, null, methodDescriptor, exception, rpc, endPoint, null, destinationId, annotations, null);
}

public static ExpectedTrace async(ExpectedTrace initiator, ExpectedTrace... asyncTraces) {
return new ExpectedTrace(initiator.getType(), initiator.getServiceType(), initiator.getMethod(), initiator.getMethodSignature(), initiator.getRpc(), initiator.getEndPoint(), initiator.getRemoteAddr(), initiator.getDestinationId(), initiator.getAnnotations(), asyncTraces);
return new ExpectedTrace(initiator.getType(), initiator.getServiceType(), initiator.getMethod(), initiator.getMethodSignature(), initiator.getException(), initiator.getRpc(), initiator.getEndPoint(), initiator.getRemoteAddr(), initiator.getDestinationId(), initiator.getAnnotations(), asyncTraces);
}

public static ExpectedAnnotation[] annotations(ExpectedAnnotation... annotations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ public class ExpectedTrace {
private final String serviceType;
private final Member method;
private final String methodSignature;
private final Exception exception;
private final String rpc;
private final String endPoint;
private final String remoteAddr;
private final String destinationId;
private final ExpectedAnnotation[] annotations;
private final ExpectedTrace[] asyncTraces;

public ExpectedTrace(TraceType type, String serviceType, Member method, String methodSignature, String rpc, String endPoint, String remoteAddr, String destinationId, ExpectedAnnotation[] annotations, ExpectedTrace[] asyncTraces) {
public ExpectedTrace(TraceType type, String serviceType, Member method, String methodSignature, Exception exception, String rpc, String endPoint, String remoteAddr, String destinationId, ExpectedAnnotation[] annotations, ExpectedTrace[] asyncTraces) {
this.type = type;
this.serviceType = serviceType;
this.method = method;
this.methodSignature = methodSignature;
this.exception = exception;
this.rpc = rpc;
this.endPoint = endPoint;
this.remoteAddr = remoteAddr;
Expand All @@ -61,6 +63,10 @@ public String getMethodSignature() {
return methodSignature;
}

public Exception getException() {
return exception;
}

public String getRpc() {
return rpc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.navercorp.pinpoint.profiler.util.JavaAssistUtils;
import com.navercorp.pinpoint.thrift.dto.TAnnotation;
import com.navercorp.pinpoint.thrift.dto.TIntStringStringValue;
import com.navercorp.pinpoint.thrift.dto.TIntStringValue;
import com.navercorp.pinpoint.thrift.dto.TSpan;
import com.navercorp.pinpoint.thrift.dto.TSpanEvent;

Expand Down Expand Up @@ -289,7 +290,7 @@ private ResolvedExpectedTrace resolveExpectedTrace(ExpectedTrace expected, Integ
final Class<?> spanClass = resolveSpanClass(expected.getType());
final int apiId = getApiId(expected);

return new ResolvedExpectedTrace(spanClass, serviceType, apiId, expected.getRpc(), expected.getEndPoint(), expected.getRemoteAddr(), expected.getDestinationId(), expected.getAnnotations(), asyncId);
return new ResolvedExpectedTrace(spanClass, serviceType, apiId, expected.getException(), expected.getRpc(), expected.getEndPoint(), expected.getRemoteAddr(), expected.getDestinationId(), expected.getAnnotations(), asyncId);
}

private int getApiId(ExpectedTrace expected) {
Expand Down Expand Up @@ -348,6 +349,8 @@ private interface ActualTrace {

Integer getNextAsyncId();

TIntStringValue getExceptionInfo();

String getRpc();

String getEndPoint();
Expand Down Expand Up @@ -388,6 +391,11 @@ public Integer getNextAsyncId() {
return null;
}

@Override
public TIntStringValue getExceptionInfo() {
return span.getExceptionInfo();
}

@Override
public String getRpc() {
return span.getRpc();
Expand Down Expand Up @@ -425,6 +433,8 @@ public String toString() {
builder.append(getServiceType());
builder.append(", apiId: ");
builder.append(getApiId());
builder.append(", exceptionInfo: ");
builder.append(getExceptionInfo());
builder.append(", rpc: ");
builder.append(getRpc());
builder.append(", endPoint: ");
Expand Down Expand Up @@ -466,6 +476,11 @@ public Integer getNextAsyncId() {
return span.isSetNextAsyncId() ? span.getNextAsyncId() : null;
}

@Override
public TIntStringValue getExceptionInfo() {
return span.getExceptionInfo();
}

@Override
public String getRpc() {
return span.getRpc();
Expand Down Expand Up @@ -503,6 +518,8 @@ public String toString() {
builder.append(getServiceType());
builder.append(", apiId: ");
builder.append(getApiId());
builder.append(", exceptionInfo: ");
builder.append(getExceptionInfo());
builder.append(", rpc: ");
builder.append(getRpc());
builder.append(", endPoint: ");
Expand All @@ -526,16 +543,18 @@ private static final class ResolvedExpectedTrace {
private final ServiceType serviceType;
private final Integer asyncId;
private final Integer apiId;
private final Exception exception;
private final String rpc;
private final String endPoint;
private final String remoteAddr;
private final String destinationId;
private final ExpectedAnnotation[] annotations;

public ResolvedExpectedTrace(Class<?> type, ServiceType serviceType, Integer apiId, String rpc, String endPoint, String remoteAddr, String destinationId, ExpectedAnnotation[] annotations, Integer asyncId) {
public ResolvedExpectedTrace(Class<?> type, ServiceType serviceType, Integer apiId, Exception exception, String rpc, String endPoint, String remoteAddr, String destinationId, ExpectedAnnotation[] annotations, Integer asyncId) {
this.type = type;
this.serviceType = serviceType;
this.apiId = apiId;
this.exception = exception;
this.rpc = rpc;
this.endPoint = endPoint;
this.remoteAddr = remoteAddr;
Expand All @@ -553,6 +572,8 @@ public String toString() {
builder.append(serviceType.getCode());
builder.append(", apiId: ");
builder.append(apiId);
builder.append(", exception: ");
builder.append(exception);
builder.append(", rpc: ");
builder.append(rpc);
builder.append(", endPoint: ");
Expand Down Expand Up @@ -619,6 +640,16 @@ private void verifySpan(ResolvedExpectedTrace expected, ActualTrace actual) {
throw new AssertionError("Expected a " + expected.type.getSimpleName() + " with asyncId[" + expected.asyncId + "] but was [" + actual.getAsyncId() + "]. expected: " + expected + ", was: " + actual);
}

TIntStringValue actualExceptionInfo = actual.getExceptionInfo();
if (actualExceptionInfo != null && actualExceptionInfo.isSetIntValue()) {
String actualExceptionClassName = getTestTcpDataSender().getString(actualExceptionInfo.getIntValue());
String actualExceptionMessage = actualExceptionInfo.getStringValue();
verifyException(expected.exception, actualExceptionClassName, actualExceptionMessage);
} else {
if (expected.exception != null) {
throw new AssertionError("Expected [" + expected.exception.getClass().getName() + "] but was none");
}
}

List<TAnnotation> actualAnnotations = actual.getAnnotations();

Expand Down Expand Up @@ -658,6 +689,20 @@ private void verifySpan(ResolvedExpectedTrace expected, ActualTrace actual) {
}
}

private void verifyException(Exception expectedException, String actualExceptionClassName, String actualExceptionMessage) {
if (expectedException == null) {
throw new AssertionError("Expected no exception but was [" + actualExceptionClassName + "]");
}
String expectedExceptionClassName = expectedException.getClass().getName();
String expectedExceptionMessage = expectedException.getMessage();
if (!Objects.equal(actualExceptionClassName, expectedExceptionClassName)) {
throw new AssertionError("Expected [" + expectedExceptionClassName + "] but was [" + actualExceptionClassName + "]");
}
if (!Objects.equal(actualExceptionMessage, expectedExceptionMessage)) {
throw new AssertionError("Expected exception with message [" + expectedExceptionMessage + "] but was [" + actualExceptionMessage + "]");
}
}

private void verifySql(ExpectedSql expected, TAnnotation actual) {
int id = getTestTcpDataSender().getSqlId(expected.getQuery());
TIntStringStringValue value = actual.getValue().getIntStringStringValue();
Expand Down

0 comments on commit cfaa3e8

Please sign in to comment.