From dd2017c8b57f87bbc2440699ac6dd0b04fc2f125 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Thu, 26 Dec 2019 10:23:17 +0000 Subject: [PATCH] Configure BlockHound --- build.gradle | 4 ++- .../StateMachineBlockHoundIntegration.java | 33 +++++++++++++++++++ .../statemachine/state/ObjectStateTests.java | 32 ++++++++++++++---- ...ockhound.integration.BlockHoundIntegration | 1 + .../StateMachineBlockHoundIntegration.java | 30 +++++++++++++++++ ...ockhound.integration.BlockHoundIntegration | 1 + .../StateMachineBlockHoundIntegration.java | 30 +++++++++++++++++ ...ockhound.integration.BlockHoundIntegration | 1 + 8 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineBlockHoundIntegration.java create mode 100644 spring-statemachine-core/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration create mode 100644 spring-statemachine-recipes/src/test/java/org/springframework/statemachine/recipes/StateMachineBlockHoundIntegration.java create mode 100644 spring-statemachine-recipes/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration create mode 100644 spring-statemachine-samples/tasks/src/test/java/demo/tasks/StateMachineBlockHoundIntegration.java create mode 100644 spring-statemachine-samples/tasks/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration diff --git a/build.gradle b/build.gradle index b5b65779..611e0a69 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ buildscript { curatorVersion = '2.11.1' docResourcesVersion = '0.1.1.RELEASE' awaitilityVersion = '3.1.6' - reactorBlockHoundVersion = '1.0.0.M3' + reactorBlockHoundVersion = '1.0.1.RELEASE' } repositories { maven { url 'https://repo.springsource.org/libs-release'} @@ -106,6 +106,7 @@ configure(allprojects) { dependency "org.apache.curator:curator-recipes:$curatorVersion" dependency "org.apache.curator:curator-test:$curatorVersion" dependency "org.awaitility:awaitility:$awaitilityVersion" + dependency "io.projectreactor.tools:blockhound:$reactorBlockHoundVersion" dependency "io.projectreactor.tools:blockhound-junit-platform:$reactorBlockHoundVersion" } } @@ -136,6 +137,7 @@ configure(subprojects) { subproject -> dependencies { testCompile("org.junit.jupiter:junit-jupiter-api") + testCompile("io.projectreactor.tools:blockhound") testRuntime("org.junit.jupiter:junit-jupiter-engine") if (project.hasProperty('statemachineBlockHound') && statemachineBlockHound.toBoolean()) { testRuntime("org.junit.platform:junit-platform-launcher") diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineBlockHoundIntegration.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineBlockHoundIntegration.java new file mode 100644 index 00000000..a269436f --- /dev/null +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineBlockHoundIntegration.java @@ -0,0 +1,33 @@ +/* + * Copyright 2019 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.statemachine; + +import reactor.blockhound.BlockHound.Builder; +import reactor.blockhound.integration.BlockHoundIntegration; + +public class StateMachineBlockHoundIntegration implements BlockHoundIntegration { + + @Override + public void applyTo(Builder builder) { + // whitelisting some blocking calls in tests + builder + .allowBlockingCallsInside("org.springframework.statemachine.AbstractStateMachineTests$TestSleepAction", "execute") + .allowBlockingCallsInside("org.springframework.statemachine.monitor.StateMachineMonitorTests$LatchAction", "execute") + .allowBlockingCallsInside("org.springframework.statemachine.state.CompletionEventTests$Config1$1", "execute") + .allowBlockingCallsInside("org.apache.commons.logging.LogAdapter$Log4jLog", "debug") + .allowBlockingCallsInside("org.springframework.statemachine.state.ObjectStateTests$TestBlockingAction", "sleep"); + } +} diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ObjectStateTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ObjectStateTests.java index 9b8a4fc2..7b98d8e6 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ObjectStateTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ObjectStateTests.java @@ -153,15 +153,33 @@ public class ObjectStateTests { @Override public Mono apply(StateContext context) { + // TODO: leaving this here as I had to extract sleep to + // its own method for BlockHound to find it change + // allowBlockingCallsInside from 'apply' to 'sleep' + + // return Mono.fromRunnable(() -> { + // countBefore.incrementAndGet(); + // try { + // Thread.sleep(1000); + // } catch (Exception e) { + // countInterrupt.incrementAndGet(); + // } + // countAfter.incrementAndGet(); + // }); + return Mono.fromRunnable(() -> { - countBefore.incrementAndGet(); - try { - Thread.sleep(1000); - } catch (Exception e) { - countInterrupt.incrementAndGet(); - } - countAfter.incrementAndGet(); + sleep(); }); } + + private void sleep() { + countBefore.incrementAndGet(); + try { + Thread.sleep(1000); + } catch (Exception e) { + countInterrupt.incrementAndGet(); + } + countAfter.incrementAndGet(); + } } } diff --git a/spring-statemachine-core/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration b/spring-statemachine-core/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration new file mode 100644 index 00000000..d83b7cc4 --- /dev/null +++ b/spring-statemachine-core/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration @@ -0,0 +1 @@ +org.springframework.statemachine.StateMachineBlockHoundIntegration \ No newline at end of file diff --git a/spring-statemachine-recipes/src/test/java/org/springframework/statemachine/recipes/StateMachineBlockHoundIntegration.java b/spring-statemachine-recipes/src/test/java/org/springframework/statemachine/recipes/StateMachineBlockHoundIntegration.java new file mode 100644 index 00000000..31c4e60b --- /dev/null +++ b/spring-statemachine-recipes/src/test/java/org/springframework/statemachine/recipes/StateMachineBlockHoundIntegration.java @@ -0,0 +1,30 @@ +/* + * Copyright 2019 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.statemachine.recipes; + +import reactor.blockhound.BlockHound.Builder; +import reactor.blockhound.integration.BlockHoundIntegration; + +public class StateMachineBlockHoundIntegration implements BlockHoundIntegration { + + @Override + public void applyTo(Builder builder) { + // whitelisting some blocking calls in tests + builder + .allowBlockingCallsInside("java.util.concurrent.locks.LockSupport", "park") + .allowBlockingCallsInside("org.springframework.statemachine.recipes.TasksHandlerTests$1", "run"); + } +} diff --git a/spring-statemachine-recipes/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration b/spring-statemachine-recipes/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration new file mode 100644 index 00000000..ab70dd83 --- /dev/null +++ b/spring-statemachine-recipes/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration @@ -0,0 +1 @@ +org.springframework.statemachine.recipes.StateMachineBlockHoundIntegration \ No newline at end of file diff --git a/spring-statemachine-samples/tasks/src/test/java/demo/tasks/StateMachineBlockHoundIntegration.java b/spring-statemachine-samples/tasks/src/test/java/demo/tasks/StateMachineBlockHoundIntegration.java new file mode 100644 index 00000000..a41363a4 --- /dev/null +++ b/spring-statemachine-samples/tasks/src/test/java/demo/tasks/StateMachineBlockHoundIntegration.java @@ -0,0 +1,30 @@ +/* + * Copyright 2019 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 demo.tasks; + +import reactor.blockhound.BlockHound.Builder; +import reactor.blockhound.integration.BlockHoundIntegration; + +public class StateMachineBlockHoundIntegration implements BlockHoundIntegration { + + @Override + public void applyTo(Builder builder) { + // whitelisting some blocking calls in tests + builder + .allowBlockingCallsInside("demo.tasks.Tasks", "sleep") + .allowBlockingCallsInside("java.util.concurrent.locks.LockSupport", "park"); + } +} diff --git a/spring-statemachine-samples/tasks/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration b/spring-statemachine-samples/tasks/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration new file mode 100644 index 00000000..a487938c --- /dev/null +++ b/spring-statemachine-samples/tasks/src/test/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration @@ -0,0 +1 @@ +demo.tasks.StateMachineBlockHoundIntegration \ No newline at end of file