|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services; |
| 17 | + |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import io.netty.bootstrap.ServerBootstrap; |
| 22 | +import io.netty.channel.Channel; |
| 23 | +import io.netty.channel.ChannelDuplexHandler; |
| 24 | +import io.netty.channel.ChannelHandlerContext; |
| 25 | +import io.netty.channel.EventLoop; |
| 26 | +import io.netty.channel.EventLoopGroup; |
| 27 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 28 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 29 | +import java.io.IOException; |
| 30 | +import java.net.ServerSocket; |
| 31 | +import java.net.Socket; |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.concurrent.atomic.AtomicReference; |
| 35 | +import org.junit.jupiter.api.AfterEach; |
| 36 | +import org.junit.jupiter.api.BeforeEach; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import reactor.blockhound.BlockHound; |
| 39 | +import reactor.blockhound.BlockingOperationError; |
| 40 | +import reactor.blockhound.integration.BlockHoundIntegration; |
| 41 | +import reactor.blockhound.junit.platform.BlockHoundTestExecutionListener; |
| 42 | +import software.amazon.awssdk.testutils.service.AwsIntegrationTestBase; |
| 43 | +import software.amazon.awssdk.testutils.service.AwsTestBase; |
| 44 | +import software.amazon.awssdk.utils.Logger; |
| 45 | + |
| 46 | +/** |
| 47 | + * This test ensures that BlockHound is correctly installed for integration tests. The test is somewhat arbitrarily placed in the |
| 48 | + * {@code s3} module in order to assert against the configuration of all service integration tests. |
| 49 | + * <p> |
| 50 | + * BlockHound is installed in one of two ways: |
| 51 | + * <ol> |
| 52 | + * <li>Using BlockHound's provided {@link BlockHoundTestExecutionListener}, which will be automatically detected by the |
| 53 | + * JUnit 5 platform upon initialization.</li> |
| 54 | + * <li>Manually calling {@link BlockHound#install(BlockHoundIntegration...)}. This is done as part of static initialization in |
| 55 | + * {@link AwsIntegrationTestBase} and {@link AwsTestBase}, which most integration/stability tests extend. |
| 56 | + * </ol> |
| 57 | + * <p> |
| 58 | + * This test ensures BlockHound is correctly installed by intentionally performing a blocking operation on the Netty |
| 59 | + * {@link EventLoop} and asserting that a {@link BlockingOperationError} is thrown to forbid it. |
| 60 | + */ |
| 61 | +class BlockHoundInstalledTest { |
| 62 | + private static final Logger log = Logger.loggerFor(BlockHoundInstalledTest.class); |
| 63 | + |
| 64 | + AtomicReference<Throwable> throwableReference; |
| 65 | + CountDownLatch latch; |
| 66 | + EventLoopGroup bossGroup; |
| 67 | + EventLoopGroup workerGroup; |
| 68 | + Socket clientSocket; |
| 69 | + Channel serverChannel; |
| 70 | + |
| 71 | + @BeforeEach |
| 72 | + public void setup() { |
| 73 | + throwableReference = new AtomicReference<>(); |
| 74 | + latch = new CountDownLatch(1); |
| 75 | + bossGroup = new NioEventLoopGroup(); |
| 76 | + workerGroup = new NioEventLoopGroup(); |
| 77 | + } |
| 78 | + |
| 79 | + @AfterEach |
| 80 | + public void teardown() throws Exception { |
| 81 | + if (clientSocket != null) { |
| 82 | + clientSocket.close(); |
| 83 | + } |
| 84 | + if (serverChannel != null) { |
| 85 | + serverChannel.close(); |
| 86 | + } |
| 87 | + workerGroup.shutdownGracefully(); |
| 88 | + bossGroup.shutdownGracefully(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void testBlockHoundInstalled() throws Exception { |
| 93 | + ServerBootstrap bootstrap = new ServerBootstrap(); |
| 94 | + bootstrap.group(bossGroup, workerGroup) |
| 95 | + .channel(NioServerSocketChannel.class) |
| 96 | + .childHandler(new ChannelDuplexHandler() { |
| 97 | + @Override |
| 98 | + public void channelActive(ChannelHandlerContext ctx) { |
| 99 | + log.info(() -> "Preparing to sleep on the EventLoop to test if BlockHound is installed"); |
| 100 | + try { |
| 101 | + Thread.sleep(1000); |
| 102 | + log.info(() -> "BlockHound does not appear to be successfully installed"); |
| 103 | + } catch (Throwable t) { |
| 104 | + log.info(() -> "BlockHound is successfully installed", t); |
| 105 | + throwableReference.set(t); |
| 106 | + } |
| 107 | + latch.countDown(); |
| 108 | + ctx.fireChannelActive(); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + int port = getUnusedPort(); |
| 113 | + serverChannel = bootstrap.bind(port).sync().channel(); |
| 114 | + clientSocket = new Socket("localhost", port); |
| 115 | + |
| 116 | + latch.await(5, TimeUnit.SECONDS); |
| 117 | + assertThat(throwableReference.get()) |
| 118 | + .withFailMessage("BlockHound does not appear to be successfully installed. Ensure that either BlockHound.install() " |
| 119 | + + "is called prior to all test executions or that BlockHoundTestExecutionListener is available on " |
| 120 | + + "the class path and correctly detected by JUnit: " |
| 121 | + + "https://github.com/reactor/BlockHound/blob/master/docs/supported_testing_frameworks.md") |
| 122 | + .isInstanceOf(BlockingOperationError.class); |
| 123 | + } |
| 124 | + |
| 125 | + private static int getUnusedPort() throws IOException { |
| 126 | + try (ServerSocket socket = new ServerSocket(0)) { |
| 127 | + socket.setReuseAddress(true); |
| 128 | + return socket.getLocalPort(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments