From 5cc435dabab6f300c0232db935ef6d29338839a8 Mon Sep 17 00:00:00 2001 From: SanHalacogluImproving Date: Wed, 24 Jan 2024 14:30:44 -0800 Subject: [PATCH] Spotless. --- .../src/main/java/glide/api/RedisClient.java | 48 ++++----- .../commands/BaseCommandResponseResolver.java | 98 +++++++++---------- .../main/java/glide/api/commands/Command.java | 16 +-- .../RedisExceptionCheckedFunction.java | 20 ++-- .../BaseClientConfiguration.java | 28 +++--- .../connectors/handlers/ChannelHandler.java | 30 +++--- .../connectors/resources/EpollResource.java | 20 ++-- .../resources/KQueuePoolResource.java | 20 ++-- .../glide/connectors/resources/Platform.java | 18 ++-- .../resources/ThreadPoolResource.java | 16 +-- .../ThreadPoolResourceAllocator.java | 60 ++++++------ .../java/glide/api/RedisClientCreateTest.java | 40 ++++---- .../ThreadPoolResourceAllocatorTest.java | 56 +++++------ 13 files changed, 236 insertions(+), 234 deletions(-) diff --git a/java/client/src/main/java/glide/api/RedisClient.java b/java/client/src/main/java/glide/api/RedisClient.java index 5673e6bf63..e09244a7ec 100644 --- a/java/client/src/main/java/glide/api/RedisClient.java +++ b/java/client/src/main/java/glide/api/RedisClient.java @@ -20,31 +20,33 @@ */ public class RedisClient extends BaseClient implements BaseCommands { - /** - * Request an async (non-blocking) Redis client in Standalone mode. - * - * @param config - Redis Client Configuration - * @return a Future to connect and return a RedisClient - */ - public static CompletableFuture CreateClient(RedisClientConfiguration config) { - ThreadPoolResource threadPoolResource = config.getThreadPoolResource(); - if (threadPoolResource == null) { - threadPoolResource = - ThreadPoolResourceAllocator.getOrCreate(Platform.getThreadPoolResourceSupplier()); + /** + * Request an async (non-blocking) Redis client in Standalone mode. + * + * @param config - Redis Client Configuration + * @return a Future to connect and return a RedisClient + */ + public static CompletableFuture CreateClient(RedisClientConfiguration config) + throws InterruptedException { + ThreadPoolResource threadPoolResource = config.getThreadPoolResource(); + if (threadPoolResource == null) { + threadPoolResource = + ThreadPoolResourceAllocator.getOrCreate(Platform.getThreadPoolResourceSupplier()); + } + ChannelHandler channelHandler = buildChannelHandler(threadPoolResource); + var connectionManager = buildConnectionManager(channelHandler); + var commandManager = buildCommandManager(channelHandler); + // TODO: Support exception throwing, including interrupted exceptions + return connectionManager + .connectToRedis(config) + .thenApply(ignore -> new RedisClient(connectionManager, commandManager)); } - ChannelHandler channelHandler = buildChannelHandler(threadPoolResource); - var connectionManager = buildConnectionManager(channelHandler); - var commandManager = buildCommandManager(channelHandler); - // TODO: Support exception throwing, including interrupted exceptions - return connectionManager - .connectToRedis(config) - .thenApply(ignore -> new RedisClient(connectionManager, commandManager)); - } - protected static ChannelHandler buildChannelHandler(ThreadPoolResource threadPoolResource) { - CallbackDispatcher callbackDispatcher = new CallbackDispatcher(); - return new ChannelHandler(callbackDispatcher, getSocket(), threadPoolResource); - } + protected static ChannelHandler buildChannelHandler(ThreadPoolResource threadPoolResource) + throws InterruptedException { + CallbackDispatcher callbackDispatcher = new CallbackDispatcher(); + return new ChannelHandler(callbackDispatcher, getSocket(), threadPoolResource); + } protected static ConnectionManager buildConnectionManager(ChannelHandler channelHandler) { return new ConnectionManager(channelHandler); diff --git a/java/client/src/main/java/glide/api/commands/BaseCommandResponseResolver.java b/java/client/src/main/java/glide/api/commands/BaseCommandResponseResolver.java index eebdef03df..27762096f1 100644 --- a/java/client/src/main/java/glide/api/commands/BaseCommandResponseResolver.java +++ b/java/client/src/main/java/glide/api/commands/BaseCommandResponseResolver.java @@ -15,56 +15,56 @@ */ @AllArgsConstructor public class BaseCommandResponseResolver - implements RedisExceptionCheckedFunction { + implements RedisExceptionCheckedFunction { - private RedisExceptionCheckedFunction respPointerResolver; + private RedisExceptionCheckedFunction respPointerResolver; - /** - * Extracts value from the RESP pointer.
- * Throws errors when the response is unsuccessful. - * - * @return A generic Object with the Response | null if the response is empty - */ - public Object apply(Response response) throws RedisException { - // TODO: handle object if the object is small - // TODO: handle RESP2 object if configuration is set - if (response.hasRequestError()) { - RequestError error = response.getRequestError(); - String msg = error.getMessage(); - switch (error.getType()) { - case Unspecified: - // Unspecified error on Redis service-side - throw new RequestException(msg); - case ExecAbort: - // Transactional error on Redis service-side - throw new ExecAbortException(msg); - case Timeout: - // Timeout from Glide to Redis service - throw new TimeoutException(msg); - case Disconnect: - // Connection problem between Glide and Redis - throw new ConnectionException(msg); - default: - // Request or command error from Redis - throw new RequestException(msg); - } + /** + * Extracts value from the RESP pointer.
+ * Throws errors when the response is unsuccessful. + * + * @return A generic Object with the Response | null if the response is empty + */ + public Object apply(Response response) throws RedisException { + // TODO: handle object if the object is small + // TODO: handle RESP2 object if configuration is set + if (response.hasRequestError()) { + RequestError error = response.getRequestError(); + String msg = error.getMessage(); + switch (error.getType()) { + case Unspecified: + // Unspecified error on Redis service-side + throw new RequestException(msg); + case ExecAbort: + // Transactional error on Redis service-side + throw new ExecAbortException(msg); + case Timeout: + // Timeout from Glide to Redis service + throw new TimeoutException(msg); + case Disconnect: + // Connection problem between Glide and Redis + throw new ConnectionException(msg); + default: + // Request or command error from Redis + throw new RequestException(msg); + } + } + if (response.hasClosingError()) { + // A closing error is thrown when Rust-core is not connected to Redis + // We want to close shop and throw a ClosingException + // TODO: close the channel on a closing error + // channel.close(); + throw new ClosingException(response.getClosingError()); + } + if (response.hasConstantResponse()) { + // Return "OK" + return response.getConstantResponse().toString(); + } + if (response.hasRespPointer()) { + // Return the shared value - which may be a null value + return respPointerResolver.apply(response.getRespPointer()); + } + // if no response payload is provided, assume null + return null; } - if (response.hasClosingError()) { - // A closing error is thrown when Rust-core is not connected to Redis - // We want to close shop and throw a ClosingException - // TODO: close the channel on a closing error - // channel.close(); - throw new ClosingException(response.getClosingError()); - } - if (response.hasConstantResponse()) { - // Return "OK" - return response.getConstantResponse().toString(); - } - if (response.hasRespPointer()) { - // Return the shared value - which may be a null value - return respPointerResolver.apply(response.getRespPointer()); - } - // if no response payload is provided, assume null - return null; - } } diff --git a/java/client/src/main/java/glide/api/commands/Command.java b/java/client/src/main/java/glide/api/commands/Command.java index 413ea63c30..8dba62553a 100644 --- a/java/client/src/main/java/glide/api/commands/Command.java +++ b/java/client/src/main/java/glide/api/commands/Command.java @@ -11,14 +11,14 @@ @EqualsAndHashCode public class Command { - /** Redis command request type */ - @NonNull final RequestType requestType; + /** Redis command request type */ + @NonNull final RequestType requestType; - /** List of Arguments for the Redis command request */ - @Builder.Default final String[] arguments = new String[] {}; + /** List of Arguments for the Redis command request */ + @Builder.Default final String[] arguments = new String[] {}; - public enum RequestType { - /** Call a custom command with list of string arguments */ - CUSTOM_COMMAND, - } + public enum RequestType { + /** Call a custom command with list of string arguments */ + CUSTOM_COMMAND, + } } diff --git a/java/client/src/main/java/glide/api/commands/RedisExceptionCheckedFunction.java b/java/client/src/main/java/glide/api/commands/RedisExceptionCheckedFunction.java index 8e8267b011..878e1441c6 100644 --- a/java/client/src/main/java/glide/api/commands/RedisExceptionCheckedFunction.java +++ b/java/client/src/main/java/glide/api/commands/RedisExceptionCheckedFunction.java @@ -5,14 +5,14 @@ @FunctionalInterface public interface RedisExceptionCheckedFunction { - /** - * Functional response handler that takes a protobuf Response object.
- * Returns a typed object on a successful Redis response.
- * Throws RedisException when receiving a Redis error response.
- * - * @param response - Redis Response - * @return T - response payload type - * @throws RedisException - */ - T apply(R response) throws RedisException; + /** + * Functional response handler that takes a protobuf Response object.
+ * Returns a typed object on a successful Redis response.
+ * Throws RedisException when receiving a Redis error response.
+ * + * @param response - Redis Response + * @return T - response payload type + * @throws RedisException + */ + T apply(R response) throws RedisException; } diff --git a/java/client/src/main/java/glide/api/models/configuration/BaseClientConfiguration.java b/java/client/src/main/java/glide/api/models/configuration/BaseClientConfiguration.java index a79f43b67e..ac3741f829 100644 --- a/java/client/src/main/java/glide/api/models/configuration/BaseClientConfiguration.java +++ b/java/client/src/main/java/glide/api/models/configuration/BaseClientConfiguration.java @@ -2,8 +2,6 @@ import glide.connectors.resources.ThreadPoolResource; import java.util.List; - -import glide.connectors.resources.ThreadPoolResource; import lombok.Builder; import lombok.Getter; import lombok.NonNull; @@ -46,18 +44,18 @@ public abstract class BaseClientConfiguration { */ private final RedisCredentials credentials; - /** - * The duration in milliseconds that the client should wait for a request to complete. This - * duration encompasses sending the request, awaiting for a response from the server, and any - * required reconnections or retries. If the specified timeout is exceeded for a pending request, - * it will result in a timeout error. If not set, a default value will be used. - */ - private final Integer requestTimeout; + /** + * The duration in milliseconds that the client should wait for a request to complete. This + * duration encompasses sending the request, awaiting for a response from the server, and any + * required reconnections or retries. If the specified timeout is exceeded for a pending request, + * it will result in a timeout error. If not set, a default value will be used. + */ + private final Integer requestTimeout; - /** - * Advanced users can pass an extended {@link glide.connectors.resources.ThreadPoolResource} to - * pass a user-defined event loop group. Users are responsible for shutting the resource down when - * no longer in use. - */ - private final ThreadPoolResource threadPoolResource; + /** + * Advanced users can pass an extended {@link glide.connectors.resources.ThreadPoolResource} to + * pass a user-defined event loop group. Users are responsible for shutting the resource down when + * no longer in use. + */ + private final ThreadPoolResource threadPoolResource; } diff --git a/java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java b/java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java index fa44cc8c9c..1fe8a99353 100644 --- a/java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java +++ b/java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java @@ -23,25 +23,27 @@ public class ChannelHandler { /** * Open a new channel for a new client and running it on the provided EventLoopGroup. + * * @param callbackDispatcher - dispatcher to handle callbacks * @param socketPath - address to connect * @param threadPoolResource - resource to choose ELG and domainSocketChannelClass */ - public ChannelHandler( - CallbackDispatcher callbackDispatcher, - String socketPath, - ThreadPoolResource threadPoolResource) throws InterruptedException { + public ChannelHandler( + CallbackDispatcher callbackDispatcher, + String socketPath, + ThreadPoolResource threadPoolResource) + throws InterruptedException { - channel = - new Bootstrap() - .group(threadPoolResource.getEventLoopGroup()) - .channel(threadPoolResource.getDomainSocketChannelClass()) - .handler(new ProtobufSocketChannelInitializer(callbackDispatcher)) - .connect(new DomainSocketAddress(socketPath)) - .sync() - .channel(); - this.callbackDispatcher = callbackDispatcher; - } + channel = + new Bootstrap() + .group(threadPoolResource.getEventLoopGroup()) + .channel(threadPoolResource.getDomainSocketChannelClass()) + .handler(new ProtobufSocketChannelInitializer(callbackDispatcher)) + .connect(new DomainSocketAddress(socketPath)) + .sync() + .channel(); + this.callbackDispatcher = callbackDispatcher; + } /** * Complete a protobuf message and write it to the channel (to UDS). diff --git a/java/client/src/main/java/glide/connectors/resources/EpollResource.java b/java/client/src/main/java/glide/connectors/resources/EpollResource.java index f3a77deebc..e4ce9de72b 100644 --- a/java/client/src/main/java/glide/connectors/resources/EpollResource.java +++ b/java/client/src/main/java/glide/connectors/resources/EpollResource.java @@ -9,16 +9,16 @@ * configurations. */ public class EpollResource extends ThreadPoolResource { - private static final String EPOLL_EVENT_LOOP_IDENTIFIER = "glide-channel-epoll-elg"; + private static final String EPOLL_EVENT_LOOP_IDENTIFIER = "glide-channel-epoll-elg"; - public EpollResource() { - this( - new EpollEventLoopGroup( - Runtime.getRuntime().availableProcessors(), - new DefaultThreadFactory(EPOLL_EVENT_LOOP_IDENTIFIER, true))); - } + public EpollResource() { + this( + new EpollEventLoopGroup( + Runtime.getRuntime().availableProcessors(), + new DefaultThreadFactory(EPOLL_EVENT_LOOP_IDENTIFIER, true))); + } - public EpollResource(EpollEventLoopGroup epollEventLoopGroup) { - super(epollEventLoopGroup, EpollDomainSocketChannel.class); - } + public EpollResource(EpollEventLoopGroup epollEventLoopGroup) { + super(epollEventLoopGroup, EpollDomainSocketChannel.class); + } } diff --git a/java/client/src/main/java/glide/connectors/resources/KQueuePoolResource.java b/java/client/src/main/java/glide/connectors/resources/KQueuePoolResource.java index 078e65a69e..95392942e9 100644 --- a/java/client/src/main/java/glide/connectors/resources/KQueuePoolResource.java +++ b/java/client/src/main/java/glide/connectors/resources/KQueuePoolResource.java @@ -9,16 +9,16 @@ * configurations. */ public class KQueuePoolResource extends ThreadPoolResource { - private static final String KQUEUE_EVENT_LOOP_IDENTIFIER = "glide-channel-kqueue-elg"; + private static final String KQUEUE_EVENT_LOOP_IDENTIFIER = "glide-channel-kqueue-elg"; - public KQueuePoolResource() { - this( - new KQueueEventLoopGroup( - Runtime.getRuntime().availableProcessors(), - new DefaultThreadFactory(KQUEUE_EVENT_LOOP_IDENTIFIER, true))); - } + public KQueuePoolResource() { + this( + new KQueueEventLoopGroup( + Runtime.getRuntime().availableProcessors(), + new DefaultThreadFactory(KQUEUE_EVENT_LOOP_IDENTIFIER, true))); + } - public KQueuePoolResource(KQueueEventLoopGroup eventLoopGroup) { - super(eventLoopGroup, KQueueDomainSocketChannel.class); - } + public KQueuePoolResource(KQueueEventLoopGroup eventLoopGroup) { + super(eventLoopGroup, KQueueDomainSocketChannel.class); + } } diff --git a/java/client/src/main/java/glide/connectors/resources/Platform.java b/java/client/src/main/java/glide/connectors/resources/Platform.java index 9403064678..5be95cf0cc 100644 --- a/java/client/src/main/java/glide/connectors/resources/Platform.java +++ b/java/client/src/main/java/glide/connectors/resources/Platform.java @@ -54,15 +54,15 @@ private static boolean isEPollAvailable() { } } - public static Supplier getThreadPoolResourceSupplier() { - if (Platform.getCapabilities().isKQueueAvailable()) { - return KQueuePoolResource::new; - } + public static Supplier getThreadPoolResourceSupplier() { + if (Platform.getCapabilities().isKQueueAvailable()) { + return KQueuePoolResource::new; + } - if (Platform.getCapabilities().isEPollAvailable()) { - return EpollResource::new; + if (Platform.getCapabilities().isEPollAvailable()) { + return EpollResource::new; + } + // TODO support IO-Uring and NIO + throw new RuntimeException("Current platform supports no known thread pool resources"); } - // TODO support IO-Uring and NIO - throw new RuntimeException("Current platform supports no known thread pool resources"); - } } diff --git a/java/client/src/main/java/glide/connectors/resources/ThreadPoolResource.java b/java/client/src/main/java/glide/connectors/resources/ThreadPoolResource.java index badfebf8c8..52c01b87ed 100644 --- a/java/client/src/main/java/glide/connectors/resources/ThreadPoolResource.java +++ b/java/client/src/main/java/glide/connectors/resources/ThreadPoolResource.java @@ -11,13 +11,13 @@ */ @Getter public abstract class ThreadPoolResource { - private EventLoopGroup eventLoopGroup; - private Class domainSocketChannelClass; + private EventLoopGroup eventLoopGroup; + private Class domainSocketChannelClass; - public ThreadPoolResource( - @NonNull EventLoopGroup eventLoopGroup, - @NonNull Class domainSocketChannelClass) { - this.eventLoopGroup = eventLoopGroup; - this.domainSocketChannelClass = domainSocketChannelClass; - } + public ThreadPoolResource( + @NonNull EventLoopGroup eventLoopGroup, + @NonNull Class domainSocketChannelClass) { + this.eventLoopGroup = eventLoopGroup; + this.domainSocketChannelClass = domainSocketChannelClass; + } } diff --git a/java/client/src/main/java/glide/connectors/resources/ThreadPoolResourceAllocator.java b/java/client/src/main/java/glide/connectors/resources/ThreadPoolResourceAllocator.java index 7837d4e3d5..b231f45437 100644 --- a/java/client/src/main/java/glide/connectors/resources/ThreadPoolResourceAllocator.java +++ b/java/client/src/main/java/glide/connectors/resources/ThreadPoolResourceAllocator.java @@ -4,40 +4,40 @@ /** A class responsible to allocating and deallocating the default Thread Pool Resource. */ public class ThreadPoolResourceAllocator { - private static final Object lock = new Object(); - private static ThreadPoolResource defaultThreadPoolResource = null; + private static final Object lock = new Object(); + private static ThreadPoolResource defaultThreadPoolResource = null; - public static ThreadPoolResource getOrCreate(Supplier supplier) { - // once the default is set, we want to avoid hitting the lock - if (defaultThreadPoolResource != null) { - return defaultThreadPoolResource; - } + public static ThreadPoolResource getOrCreate(Supplier supplier) { + // once the default is set, we want to avoid hitting the lock + if (defaultThreadPoolResource != null) { + return defaultThreadPoolResource; + } - synchronized (lock) { - if (defaultThreadPoolResource == null) { - defaultThreadPoolResource = supplier.get(); - } - } + synchronized (lock) { + if (defaultThreadPoolResource == null) { + defaultThreadPoolResource = supplier.get(); + } + } - return defaultThreadPoolResource; - } + return defaultThreadPoolResource; + } - /** - * A JVM shutdown hook to be registered. It is responsible for closing connection and freeing - * resources. It is recommended to use a class instead of lambda to ensure that it is called.
- * See {@link Runtime#addShutdownHook}. - */ - protected static class ShutdownHook implements Runnable { - @Override - public void run() { - if (defaultThreadPoolResource != null) { - defaultThreadPoolResource.getEventLoopGroup().shutdownGracefully(); - defaultThreadPoolResource = null; - } + /** + * A JVM shutdown hook to be registered. It is responsible for closing connection and freeing + * resources. It is recommended to use a class instead of lambda to ensure that it is called.
+ * See {@link Runtime#addShutdownHook}. + */ + protected static class ShutdownHook implements Runnable { + @Override + public void run() { + if (defaultThreadPoolResource != null) { + defaultThreadPoolResource.getEventLoopGroup().shutdownGracefully(); + defaultThreadPoolResource = null; + } + } } - } - static { - Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook(), "Glide-shutdown-hook")); - } + static { + Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook(), "Glide-shutdown-hook")); + } } diff --git a/java/client/src/test/java/glide/api/RedisClientCreateTest.java b/java/client/src/test/java/glide/api/RedisClientCreateTest.java index 051e2d1894..e96c4ca4c5 100644 --- a/java/client/src/test/java/glide/api/RedisClientCreateTest.java +++ b/java/client/src/test/java/glide/api/RedisClientCreateTest.java @@ -28,25 +28,25 @@ public class RedisClientCreateTest { - private MockedStatic mockedClient; - private ChannelHandler channelHandler; - private ConnectionManager connectionManager; - private CommandManager commandManager; - private ThreadPoolResource threadPoolResource; + private MockedStatic mockedClient; + private ChannelHandler channelHandler; + private ConnectionManager connectionManager; + private CommandManager commandManager; + private ThreadPoolResource threadPoolResource; @BeforeEach public void init() { mockedClient = Mockito.mockStatic(RedisClient.class); - channelHandler = mock(ChannelHandler.class); - commandManager = mock(CommandManager.class); - connectionManager = mock(ConnectionManager.class); - threadPoolResource = mock(ThreadPoolResource.class); + channelHandler = mock(ChannelHandler.class); + commandManager = mock(CommandManager.class); + connectionManager = mock(ConnectionManager.class); + threadPoolResource = mock(ThreadPoolResource.class); - mockedClient.when(() -> buildChannelHandler(any())).thenReturn(channelHandler); - mockedClient.when(() -> buildConnectionManager(channelHandler)).thenReturn(connectionManager); - mockedClient.when(() -> buildCommandManager(channelHandler)).thenReturn(commandManager); - } + mockedClient.when(() -> buildChannelHandler(any())).thenReturn(channelHandler); + mockedClient.when(() -> buildConnectionManager(channelHandler)).thenReturn(connectionManager); + mockedClient.when(() -> buildCommandManager(channelHandler)).thenReturn(commandManager); + } @AfterEach public void teardown() { @@ -57,11 +57,11 @@ public void teardown() { @SneakyThrows public void createClient_withConfig_successfullyReturnsRedisClient() { - // setup - CompletableFuture connectToRedisFuture = new CompletableFuture<>(); - connectToRedisFuture.complete(null); - RedisClientConfiguration config = - RedisClientConfiguration.builder().threadPoolResource(threadPoolResource).build(); + // setup + CompletableFuture connectToRedisFuture = new CompletableFuture<>(); + connectToRedisFuture.complete(null); + RedisClientConfiguration config = + RedisClientConfiguration.builder().threadPoolResource(threadPoolResource).build(); when(connectionManager.connectToRedis(eq(config))).thenReturn(connectToRedisFuture); mockedClient.when(() -> CreateClient(config)).thenCallRealMethod(); @@ -84,8 +84,8 @@ public void createClient_errorOnConnectionThrowsExecutionException() { connectToRedisFuture.completeExceptionally(exception); RedisClientConfiguration config = RedisClientConfiguration.builder().build(); - when(connectionManager.connectToRedis(any())).thenReturn(connectToRedisFuture); - mockedClient.when(() -> CreateClient(any())).thenCallRealMethod(); + when(connectionManager.connectToRedis(any())).thenReturn(connectToRedisFuture); + mockedClient.when(() -> CreateClient(any())).thenCallRealMethod(); // exercise CompletableFuture result = CreateClient(config); diff --git a/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java b/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java index 068d55e5ef..7bbcd6a0b1 100644 --- a/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java +++ b/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java @@ -12,32 +12,32 @@ public class ThreadPoolResourceAllocatorTest { - ThreadPoolResourceAllocator service; - - @Test - public void getOrCreateReturnsDefault() { - new ThreadPoolResourceAllocator.ShutdownHook().run(); - - ThreadPoolResource mockedThreadPool = mock(ThreadPoolResource.class); - Supplier threadPoolSupplier = mock(Supplier.class); - when(threadPoolSupplier.get()).thenReturn(mockedThreadPool); - - ThreadPoolResource theResource = service.getOrCreate(threadPoolSupplier); - assertEquals(mockedThreadPool, theResource); - - ThreadPoolResource theSameResource = service.getOrCreate(threadPoolSupplier); - assertEquals(mockedThreadPool, theSameResource); - - // and test that the supplier isn't used any longer once the default is set - Supplier notUsedSupplier = mock(Supplier.class); - ThreadPoolResource firstResource = service.getOrCreate(notUsedSupplier); - verify(notUsedSupplier, times(0)).get(); - assertEquals(mockedThreadPool, firstResource); - - // teardown - // remove the mocked resource - EventLoopGroup mockedELG = mock(EventLoopGroup.class); - when(mockedThreadPool.getEventLoopGroup()).thenReturn(mockedELG); - new ThreadPoolResourceAllocator.ShutdownHook().run(); - } + ThreadPoolResourceAllocator service; + + @Test + public void getOrCreateReturnsDefault() { + new ThreadPoolResourceAllocator.ShutdownHook().run(); + + ThreadPoolResource mockedThreadPool = mock(ThreadPoolResource.class); + Supplier threadPoolSupplier = mock(Supplier.class); + when(threadPoolSupplier.get()).thenReturn(mockedThreadPool); + + ThreadPoolResource theResource = service.getOrCreate(threadPoolSupplier); + assertEquals(mockedThreadPool, theResource); + + ThreadPoolResource theSameResource = service.getOrCreate(threadPoolSupplier); + assertEquals(mockedThreadPool, theSameResource); + + // and test that the supplier isn't used any longer once the default is set + Supplier notUsedSupplier = mock(Supplier.class); + ThreadPoolResource firstResource = service.getOrCreate(notUsedSupplier); + verify(notUsedSupplier, times(0)).get(); + assertEquals(mockedThreadPool, firstResource); + + // teardown + // remove the mocked resource + EventLoopGroup mockedELG = mock(EventLoopGroup.class); + when(mockedThreadPool.getEventLoopGroup()).thenReturn(mockedELG); + new ThreadPoolResourceAllocator.ShutdownHook().run(); + } }