Skip to content

Commit

Permalink
Perform a smoke test after instrumenting the classes. (#104)
Browse files Browse the repository at this point in the history
Since OpenJDK requires `-XX:+AllowRedefinitionToAddDeleteMethod` (see #33)
starting with Java 13, we should test that the instrumentation
was applied correctly and fail fast if not.
  • Loading branch information
bsideup committed Mar 26, 2020
1 parent 174ef41 commit 5253edb
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
60 changes: 49 additions & 11 deletions agent/src/main/java/reactor/blockhound/BlockHound.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,12 @@ public Builder with(BlockHoundIntegration integration) {
* Installs the agent and runs the instrumentation, but only if BlockHound wasn't installed yet (it is global).
*/
public void install() {
try {
if (!INITIALIZED.compareAndSet(false, true)) {
return;
}
if (!INITIALIZED.compareAndSet(false, true)) {
return;
}

Consumer<BlockingMethod> originalOnBlockingMethod = onBlockingMethod;
try {
Instrumentation instrumentation = ByteBuddyAgent.install();
InstrumentationUtils.injectBootstrapClasses(
instrumentation,
Expand All @@ -382,19 +383,56 @@ public void install() {
onBlockingMethod.accept(new BlockingMethod(className, methodName, modifiers));
};

// Eagerly trigger the classloading of `dynamicThreadPredicate` (since classloading is blocking)
dynamicThreadPredicate.test(Thread.currentThread());
BlockHoundRuntime.dynamicThreadPredicate = dynamicThreadPredicate;

// Eagerly trigger the classloading of `threadPredicate` (since classloading is blocking)
threadPredicate.test(Thread.currentThread());
BlockHoundRuntime.threadPredicate = threadPredicate;
onBlockingMethod = m -> {
Thread currentThread = Thread.currentThread();
if (currentThread instanceof TestThread) {
((TestThread) currentThread).blockingCallDetected = true;
}
};
BlockHoundRuntime.dynamicThreadPredicate = t -> false;
BlockHoundRuntime.threadPredicate = TestThread.class::isInstance;

instrument(instrumentation);
}
catch (Throwable e) {
throw new RuntimeException(e);
}

testInstrumentation();

// Eagerly trigger the classloading of `dynamicThreadPredicate` (since classloading is blocking)
dynamicThreadPredicate.test(Thread.currentThread());
BlockHoundRuntime.dynamicThreadPredicate = dynamicThreadPredicate;

// Eagerly trigger the classloading of `threadPredicate` (since classloading is blocking)
threadPredicate.test(Thread.currentThread());
BlockHoundRuntime.threadPredicate = threadPredicate;

onBlockingMethod = originalOnBlockingMethod;
}

private void testInstrumentation() {
TestThread thread = new TestThread();
thread.startAndWait();

// Set in the artificial blockingMethodConsumer, see install()
if (thread.blockingCallDetected) {
return;
}

String message = "The instrumentation have failed.";
try {
// Test some public API class added in Java 13
Class.forName("sun.nio.ch.NioSocketImpl");
message += "\n";
message += "It looks like you're running on JDK 13+.\n";
message += "You need to add '-XX:+AllowRedefinitionToAddDeleteMethods' JVM flag.\n";
message += "See https://github.com/reactor/BlockHound/issues/33 for more info.";
}
catch (ClassNotFoundException ignored) {
}

throw new IllegalStateException(message);
}

private void instrument(Instrumentation instrumentation) {
Expand Down
57 changes: 57 additions & 0 deletions agent/src/main/java/reactor/blockhound/TestThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2020-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 reactor.blockhound;

import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
* This is an internal class for performing the instrumentation test
*/
class TestThread extends Thread {

volatile boolean blockingCallDetected = false;

final FutureTask<Void> task = new FutureTask<>(() -> {
Thread.sleep(0);
return null;
});

TestThread() {
super();
setName("blockhound-test-thread");
setDaemon(true);
}

@Override
public void run() {
task.run();
}

public void startAndWait() {
start();
try {
task.get(5, TimeUnit.SECONDS);
}
catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new RuntimeException(e);
}
}
}

0 comments on commit 5253edb

Please sign in to comment.