Skip to content

Commit

Permalink
Add caller stack information in test
Browse files Browse the repository at this point in the history
TestRequestSender now shows caller stack when operation failed.

Previously, when operation failed, the output only showed operation response
failure. It didn't show any information about where the call has originated.

In this change, the caller stack information is displayed as primary
stacktrace and the failure response stack is showed in suppressed exception
stacktrace.
For "sendAndWaitFailure", when operation was successful, the displayed
exception is now the caller stack information.

Operation failure log:

Before:

  ..IllegalAccessError: forbidden

    at ..Operation.fail(Operation.java:1102)
    at ..ServiceHost.loadServiceState(ServiceHost.java:2567)
    ....

New:

  ..RuntimeException: Received Failure response. (See suppressed exception for detail)

    at ..TestRequestSender.sendAndWait(TestRequestSender.java:192)
    at ..TestRequestSender.sendAndWait(TestRequestSender.java:175)
    at ..TestExampleWithMultiNode.authentication(TestExampleWithMultiNode.java:257)
    ...
    Suppressed: java.lang.IllegalAccessError: forbidden
      at ..Operation.fail(Operation.java:1102)
      at ..loadServiceState(ServiceHost.java:2567)
      ...

https://www.pivotaltracker.com/story/show/127395667

Change-Id: I3c860695f25df659067f4233cc9f48023eeb0eca
  • Loading branch information
ttddyy authored and Gerrit Code Review committed Sep 13, 2016
1 parent 41694a1 commit e1730b6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public <T extends ServiceDocument> T sendAndWait(Operation op, Class<T> bodyType
* Expecting all {@link Operation} to be successful.
* The order of result corresponds to the input order.
*
* @param ops operations to perform
* @param ops operations to perform
* @param bodyType returned body type
* @param <T> ServiceDocument
* @return body documents
Expand Down Expand Up @@ -150,13 +150,20 @@ public List<Operation> sendAndWait(List<Operation> ops) {
public List<Operation> sendAndWait(List<Operation> ops, boolean checkResponse) {
Operation[] response = new Operation[ops.size()];

// Keep caller stack information for operation failure.
// Actual operation failure will be added to this exception as suppressed exception.
// This way, it'll display the original caller location in stacktrace
String callerStackMessage = "Received Failure response. (See suppressed exception for detail)";
Exception callerStack = new RuntimeException(callerStackMessage);

TestContext waitContext = new TestContext(ops.size(), this.timeout);
for (int i = 0; i < ops.size(); i++) {
int index = i;
Operation op = ops.get(i);
op.appendCompletion((o, e) -> {
if (e != null && checkResponse) {
waitContext.fail(e);
callerStack.addSuppressed(e);
waitContext.fail(callerStack);
return;
}
response[index] = o;
Expand Down Expand Up @@ -209,6 +216,10 @@ public <T extends ServiceDocument> T sendPostAndWait(String url, Class<T> bodyTy
public FailureResponse sendAndWaitFailure(Operation op) {
FailureResponse response = new FailureResponse();

// Prepare caller stack information for operation success.
String msg = String.format("Expected operation failure but was successful. uri=%s ", op.getUri());
Exception callerStack = new RuntimeException(msg);

TestContext waitContext = new TestContext(1, this.timeout);
op.appendCompletion((o, e) -> {
if (e != null) {
Expand All @@ -217,8 +228,7 @@ public FailureResponse sendAndWaitFailure(Operation op) {
waitContext.complete();
return;
}
String msg = String.format("Expected operation failure but was successful. uri=%s", o.getUri());
waitContext.fail(new RuntimeException(msg));
waitContext.fail(callerStack);
});
sendRequest(op);
waitContext.await();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,13 @@ public URI createQueryTaskService(URI factoryUri, QueryTask create, boolean forc
create.querySpec.options,
create.querySpec.resultLimit);

QueryTask result = this.sender.sendAndWait(startPost, QueryTask.class);
QueryTask result;
try {
result = this.sender.sendAndWait(startPost, QueryTask.class);
} catch (RuntimeException e) {
// throw original exception
throw ExceptionTestUtils.throwAsUnchecked(e.getSuppressed()[0]);
}

if (isDirect) {
taskResult.results = result.results;
Expand Down

0 comments on commit e1730b6

Please sign in to comment.