Skip to content

Commit

Permalink
BlockHoundIntegration for spring-core
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Feb 24, 2020
1 parent f048f27 commit 2ae9140
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ configure(allprojects) { project ->
dependency "io.reactivex:rxjava-reactive-streams:1.2.1"
dependency "io.reactivex.rxjava2:rxjava:2.2.17"

dependency "io.projectreactor.tools:blockhound:1.0.2.RELEASE"

dependency "com.caucho:hessian:4.0.62"
dependency "com.fasterxml:aalto-xml:1.2.2"
dependency("com.fasterxml.woodstox:woodstox-core:6.0.3") {
Expand Down
2 changes: 2 additions & 0 deletions spring-core/spring-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
compile(files(objenesisRepackJar))
compile(project(":spring-jcl"))
compileOnly(project(":kotlin-coroutines"))
compileOnly("io.projectreactor.tools:blockhound")
optional("net.sf.jopt-simple:jopt-simple")
optional("org.aspectj:aspectjweaver")
optional("org.jetbrains.kotlin:kotlin-reflect")
Expand All @@ -60,6 +61,7 @@ dependencies {
testCompile("javax.xml.bind:jaxb-api")
testCompile("com.fasterxml.woodstox:woodstox-core")
testCompile(project(":kotlin-coroutines"))
testCompile("io.projectreactor.tools:blockhound")
testFixturesImplementation("com.google.code.findbugs:jsr305")
testFixturesImplementation("io.projectreactor:reactor-test")
testFixturesImplementation("org.assertj:assertj-core")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,12 +29,15 @@
import kotlinx.coroutines.CompletableDeferredKt;
import kotlinx.coroutines.Deferred;
import org.reactivestreams.Publisher;
import reactor.blockhound.BlockHound;
import reactor.blockhound.integration.BlockHoundIntegration;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import rx.RxReactiveStreams;

import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.ReflectionUtils;

/**
Expand Down Expand Up @@ -354,4 +357,32 @@ void registerAdapters(ReactiveAdapterRegistry registry) {
);
}
}


/**
* {@code BlockHoundIntegration} for spring-core classes.
* <p>Whitelists the following:
* <ul>
* <li>Reading class info via {@link LocalVariableTableParameterNameDiscoverer}.
* <li>Locking within {@link ConcurrentReferenceHashMap}.
* </ul>
* @since 5.2.4
*/
public static class SpringCoreBlockHoundIntegration implements BlockHoundIntegration {

@Override
public void applyTo(BlockHound.Builder builder) {

// Avoid hard references potentially anywhere in spring-core (no need for structural dependency)

builder.allowBlockingCallsInside(
"org.springframework.core.LocalVariableTableParameterNameDiscoverer", "inspectClass");

String className = "org.springframework.util.ConcurrentReferenceHashMap$Segment";
builder.allowBlockingCallsInside(className, "doTask");
builder.allowBlockingCallsInside(className, "clear");
builder.allowBlockingCallsInside(className, "restructure");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2002-2020 the original author or authors.
#
# 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.

org.springframework.core.ReactiveAdapterRegistry$SpringCoreBlockHoundIntegration
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* 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 org.springframework.core;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import reactor.blockhound.BlockHound;
import reactor.core.scheduler.Schedulers;

import org.springframework.tests.sample.objects.TestObject;
import org.springframework.util.ConcurrentReferenceHashMap;

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

/**
* Tests to verify the spring-core BlockHound integration rules.
*
* @author Rossen Stoyanchev
* @since 5.2.4
*/
public class SpringCoreBlockHoundIntegrationTests {


@BeforeAll
static void setUp() {
BlockHound.install();
}


@Test
void blockHoundIsInstalled() {
assertThatThrownBy(() -> testNonBlockingTask(() -> Thread.sleep(10)))
.hasMessageContaining("Blocking call!");
}

@Test
void localVariableTableParameterNameDiscoverer() {
testNonBlockingTask(() -> {
Method setName = TestObject.class.getMethod("setName", String.class);
String[] names = new LocalVariableTableParameterNameDiscoverer().getParameterNames(setName);
assertThat(names).isEqualTo(new String[] {"name"});
});
}

@Test
void concurrentReferenceHashMap() {
int size = 10000;
Map<String, String> map = new ConcurrentReferenceHashMap<>(size);

CompletableFuture<Object> future1 = new CompletableFuture<>();
testNonBlockingTask(() -> {
for (int i = 0; i < size / 2; i++) {
map.put("a" + i, "bar");
}
}, future1);

CompletableFuture<Object> future2 = new CompletableFuture<>();
testNonBlockingTask(() -> {
for (int i = 0; i < size / 2; i++) {
map.put("b" + i, "bar");
}
}, future2);

CompletableFuture.allOf(future1, future2).join();
assertThat(map).hasSize(size);
}

private void testNonBlockingTask(NonBlockingTask task) {
CompletableFuture<Object> future = new CompletableFuture<>();
testNonBlockingTask(task, future);
future.join();
}

private void testNonBlockingTask(NonBlockingTask task, CompletableFuture<Object> future) {
Schedulers.parallel().schedule(() -> {
try {
task.run();
future.complete(null);
}
catch (Throwable ex) {
future.completeExceptionally(ex);
}
});
}


@FunctionalInterface
private interface NonBlockingTask {

void run() throws Exception;
}

}

0 comments on commit 2ae9140

Please sign in to comment.