Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide BlockHound's own frame from the Error #62

Merged
merged 4 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion agent/src/main/java/reactor/blockhound/BlockHound.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,26 @@ public static class Builder {
}};

private Consumer<BlockingMethod> onBlockingMethod = method -> {
throw new Error(String.format("Blocking call! %s", method));
Error error = new Error(String.format("Blocking call! %s", method));

// Strip BlockHound's internal noisy frames from the stacktrace to not mislead the users
StackTraceElement[] stackTrace = error.getStackTrace();
int length = stackTrace.length;
for (int i = 0; i < length; i++) {
StackTraceElement stackTraceElement = stackTrace[i];
if (!BlockHoundRuntime.class.getName().equals(stackTraceElement.getClassName())) {
continue;
}

if ("checkBlocking".equals(stackTraceElement.getMethodName())) {
if (i + 1 < length) {
error.setStackTrace(Arrays.copyOfRange(stackTrace, i + 1, length));
}
break;
}
}

throw error;
};

private Predicate<Thread> threadPredicate = t -> false;
Expand Down
65 changes: 65 additions & 0 deletions example/src/test/java/com/example/StackTraceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2019-Present Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import reactor.blockhound.BlockHound;
import reactor.core.scheduler.NonBlocking;

import java.util.concurrent.CompletableFuture;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

public class StackTraceTest {

static {
BlockHound.install();
}

@Test
public void shouldHideInternalFrames() {
CompletableFuture<Void> future = new CompletableFuture<>();
class TestThread extends Thread implements NonBlocking {
@Override
public void run() {
try {
Thread.sleep(0);
future.complete(null);
}
catch (Throwable e) {
future.completeExceptionally(e);
}
}
}
new TestThread().start();

assertThat(Assertions.catchThrowable(future::join))
.as("exception")
.isNotNull()
.satisfies(e -> {
assertThat(e.getCause().getStackTrace())
.as("Cause's stacktrace")
.extracting(StackTraceElement::getClassName, StackTraceElement::getMethodName)
.containsExactly(
tuple("java.lang.Thread", "sleep"),
tuple(TestThread.class.getName(), "run")
);
});
}
}