Added BlockHound integration

This commit is contained in:
Tim Ysewyn
2019-11-22 12:06:25 +01:00
committed by GitHub
parent e26fece200
commit 1fb63dc6c5
6 changed files with 123 additions and 9 deletions

View File

@@ -56,6 +56,7 @@
<spring-cloud-netflix.version>2.2.0.BUILD-SNAPSHOT</spring-cloud-netflix.version>
<spring-cloud-circuitbreaker.version>1.0.0.BUILD-SNAPSHOT</spring-cloud-circuitbreaker.version>
<embedded-redis.version>0.6</embedded-redis.version>
<blockhound.version>1.0.1.RELEASE</blockhound.version>
</properties>
<dependencyManagement>
@@ -134,6 +135,11 @@
<artifactId>embedded-redis</artifactId>
<version>${embedded-redis.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor.tools</groupId>
<artifactId>blockhound-junit-platform</artifactId>
<version>${blockhound.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@@ -112,6 +112,11 @@
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor.tools</groupId>
<artifactId>blockhound-junit-platform</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>

View File

@@ -31,6 +31,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
@@ -193,19 +194,15 @@ public class RetryGatewayFilterFactoryIntegrationTests extends BaseWebClientTest
private String uri;
@RequestMapping("/httpbin/sleep")
public ResponseEntity<String> sleep(@RequestParam("key") String key,
public Mono<ResponseEntity<String>> sleep(@RequestParam("key") String key,
@RequestParam("millis") long millisToSleep) {
AtomicInteger num = getCount(key);
int retryCount = num.incrementAndGet();
log.warn("Retry count: " + retryCount);
try {
Thread.sleep(millisToSleep);
}
catch (InterruptedException e) {
}
return ResponseEntity.status(HttpStatus.OK)
.header("X-Retry-Count", String.valueOf(retryCount))
.body("slept " + millisToSleep + " ms");
return Mono.delay(Duration.ofMillis(millisToSleep))
.thenReturn(ResponseEntity.status(HttpStatus.OK)
.header("X-Retry-Count", String.valueOf(retryCount))
.body("slept " + millisToSleep + " ms"));
}
@RequestMapping("/httpbin/retryalwaysfail")

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2019-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 reactor.blockhound.integration;
import reactor.blockhound.BlockHound;
/**
* @author Tim Ysewyn
*/
public class CustomBlockHoundIntegration implements BlockHoundIntegration {
@Override
public void applyTo(BlockHound.Builder builder) {
// Uses
// ch.qos.logback.classic.spi.PackagingDataCalculator#getImplementationVersion
builder.allowBlockingCallsInside(
"org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler",
"logError");
builder.allowBlockingCallsInside("reactor.util.Loggers$Slf4JLogger", "debug");
// Uses org.springframework.util.JdkIdGenerator#generateId
// Uses UUID#randomUUID
builder.allowBlockingCallsInside(
"org.springframework.web.server.session.InMemoryWebSessionStore",
"lambda$createWebSession$0");
// Uses java.util.Random#nextInt
builder.allowBlockingCallsInside("org.springframework.util.MimeTypeUtils",
"generateMultipartBoundary");
// SECURITY RELATED
// For HTTPS traffic
builder.allowBlockingCallsInside("io.netty.handler.ssl.SslHandler",
"channelActive");
builder.allowBlockingCallsInside("io.netty.handler.ssl.SslHandler", "unwrap");
builder.allowBlockingCallsInside("io.netty.handler.ssl.SslContext",
"newClientContextInternal");
// Uses
// org.springframework.cloud.commons.httpclient.DefaultApacheHttpClientConnectionManagerFactory#newConnectionManager
// Uses javax.net.ssl.SSLContext#init
builder.allowBlockingCallsInside(
"org.springframework.cloud.netflix.ribbon.SpringClientFactory",
"getContext");
// Uses org.springframework.security.crypto.bcrypt.BCrypt#gensalt
// Uses java.security.SecureRandom#nextBytes
builder.allowBlockingCallsInside(
"org.springframework.security.authentication.AbstractUserDetailsReactiveAuthenticationManager",
"lambda$authenticate$4");
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2019-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 reactor.blockhound.integration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
/**
* @author Tim Ysewyn
*/
public class CustomBlockHoundIntegrationTest {
@Test
public void shouldThrowErrorForBlockingCallWithCustomBlockHoundIntegration() {
Assertions.assertThrows(RuntimeException.class, () -> Mono.fromCallable(() -> {
Thread.sleep(1);
return null;
}).subscribeOn(Schedulers.parallel()).block());
}
}

View File

@@ -0,0 +1 @@
reactor.blockhound.integration.CustomBlockHoundIntegration