From 165151d3e2076203034c758716f6a73122405d2d Mon Sep 17 00:00:00 2001 From: emeroad Date: Thu, 21 Nov 2024 22:44:31 +0900 Subject: [PATCH] [#noissue] Apply junit AutoClose --- ...aultServerMetaDataRegistryServiceTest.java | 9 +- .../grpc/stream/StreamExecutorTest.java | 109 ------------------ .../ApplicationMapBuilderTest.java | 24 +--- .../histogram/NodeHistogramAppenderTest.java | 19 +-- .../service/FilteredMapServiceImplTest.java | 15 +-- .../web/task/ChainedTaskDecoratorTest.java | 6 +- ...stContextPropagatingTaskDecoratorTest.java | 7 +- ...tyContextPropagatingTaskDecoratorTest.java | 7 +- 8 files changed, 33 insertions(+), 163 deletions(-) delete mode 100644 agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/grpc/stream/StreamExecutorTest.java diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultServerMetaDataRegistryServiceTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultServerMetaDataRegistryServiceTest.java index 08d4c591198b..2c74dfe87210 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultServerMetaDataRegistryServiceTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultServerMetaDataRegistryServiceTest.java @@ -18,7 +18,7 @@ import com.navercorp.pinpoint.bootstrap.context.ServerMetaData; import com.navercorp.pinpoint.bootstrap.context.ServiceInfo; -import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -41,11 +41,12 @@ */ public class DefaultServerMetaDataRegistryServiceTest { - private static final int THREAD_COUNT = 500; + private static final int THREAD_COUNT = 32; private static final String SERVER_INFO = "testContainerInfo"; private static final List VM_ARGS = Collections.singletonList("testVmArgs"); + @AutoClose("shutdown") private ExecutorService executorService; @BeforeEach @@ -53,10 +54,6 @@ public void setUp() { this.executorService = Executors.newFixedThreadPool(THREAD_COUNT); } - @AfterEach - public void cleanUp() { - this.executorService.shutdown(); - } @Test public void testRaceConditionWhenAddingConnectors() throws InterruptedException { diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/grpc/stream/StreamExecutorTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/grpc/stream/StreamExecutorTest.java deleted file mode 100644 index 1c1ff7a98bd1..000000000000 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/grpc/stream/StreamExecutorTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2020 NAVER Corp. - * - * 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 - * - * http://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 com.navercorp.pinpoint.profiler.sender.grpc.stream; - -import com.navercorp.pinpoint.profiler.sender.grpc.StreamId; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -/** - * @author Woonduk Kang(emeroad) - */ -public class StreamExecutorTest { - - private ExecutorService executor = Executors.newFixedThreadPool(2); - private StreamId id = StreamId.newStreamId("test"); - - @BeforeEach - public void setUp() throws Exception { - executor = Executors.newFixedThreadPool(2); - } - - @AfterEach - public void tearDown() throws Exception { - if (executor != null) { - executor.shutdown(); - executor.awaitTermination(10, TimeUnit.SECONDS); - executor.shutdownNow(); - } - } - -// @Test -// public void start() throws InterruptedException { -// -// BlockingQueue queue = new LinkedBlockingQueue(); -// StreamState streamState = new StreamState(10, 100); -// Reconnector reconnector = mock(Reconnector.class); -// MessageDispatcherFactory dispatcherFactory = new MessageDispatcherFactory(); -// MockStreamMessageDispatcher dispatcher = new MockStreamMessageDispatcher(); -// StreamExecutorFactory streamFactory = new StreamExecutorFactory(executor, queue, streamState, reconnector); -// -// -// StreamDecorator decorator = new StreamDecorator(id, streamFactory, dispatcherFactory, ); -// -// ClientCallStreamObserver streamObserver = mock(ClientCallStreamObserver.class); -// when(streamObserver.isReady()).thenReturn(true); -// -// StreamExecutor streamExecutor = new StreamExecutor(id, executor, queue, streamState, dispatcher); -// streamExecutor.start(streamObserver); -// queue.add("abc"); -// -// Assert.assertTrue(await(dispatcher.onDispatch)); -// streamExecutor.cancel(); -// -// Assert.assertTrue(await(dispatcher.start)); -// Assert.assertTrue(await(dispatcher.stop)); -// -// } -// -// private boolean await(CountDownLatch latch) throws InterruptedException { -// return latch.await(3, TimeUnit.SECONDS); -// } -// -// static class MockStreamMessageDispatcher implements StreamMessageDispatcher { -// private final StreamMessageDispatcher delegate; -// public final CountDownLatch onDispatch = new CountDownLatch(1); -// public final CountDownLatch start = new CountDownLatch(1); -// public final CountDownLatch stop = new CountDownLatch(1); -// -// public MockStreamMessageDispatcher(StreamMessageDispatcher delegate) { -// this.delegate = delegate; -// } -// -// @Override -// public void onDispatch(ClientCallStreamObserverstream, Object message) { -// this.delegate.onDispatch(stream, message); -// onDispatch.countDown(); -// } -// -// @Override -// public void start(StreamExecutorFactory streamExecutorFactory) { -// this.delegate.start(streamExecutorFactory); -// start.countDown(); -// } -// @Override -// public void stop() { -// delegate.stop(); -// stop.countDown(); -// } -// -// }; -} \ No newline at end of file diff --git a/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ApplicationMapBuilderTest.java b/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ApplicationMapBuilderTest.java index 8f3802e59d38..f7dcae628ac4 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ApplicationMapBuilderTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ApplicationMapBuilderTest.java @@ -16,7 +16,6 @@ package com.navercorp.pinpoint.web.applicationmap; -import com.google.common.util.concurrent.MoreExecutors; import com.navercorp.pinpoint.common.server.bo.SimpleAgentKey; import com.navercorp.pinpoint.common.server.util.AgentLifeCycleState; import com.navercorp.pinpoint.common.server.util.time.Range; @@ -41,18 +40,17 @@ import com.navercorp.pinpoint.web.vo.agent.AgentInfo; import com.navercorp.pinpoint.web.vo.agent.AgentStatus; import com.navercorp.pinpoint.web.vo.agent.AgentStatusQuery; -import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executor; import java.util.concurrent.Executors; import static org.assertj.core.api.Assertions.assertThat; @@ -68,9 +66,11 @@ */ public class ApplicationMapBuilderTest { - private final ExecutorService serialExecutor = Executors.newSingleThreadExecutor(); + @AutoClose("shutdown") + private static final Executor serialExecutor = Executors.newSingleThreadExecutor(); - private final ExecutorService parallelExecutor = Executors.newFixedThreadPool(8); + @AutoClose("shutdown") + private static final Executor parallelExecutor = Executors.newFixedThreadPool(8); private MapResponseNodeHistogramDataSource mapResponseNodeHistogramDataSource; @@ -152,18 +152,6 @@ public List> answer(InvocationOnMock invocation) throws Th }).when(agentInfoService).getAgentStatus(any()); } - @AfterEach - public void cleanUp() { - shutdownExecutor(serialExecutor); - shutdownExecutor(parallelExecutor); - } - - private void shutdownExecutor(ExecutorService executor) { - if (executor != null) { - MoreExecutors.shutdownAndAwaitTermination(executor, Duration.ofSeconds(3)); - } - } - @Test public void testNoCallData() { Range range = Range.between(0, 1000); diff --git a/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/appender/histogram/NodeHistogramAppenderTest.java b/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/appender/histogram/NodeHistogramAppenderTest.java index a0c1411d9a05..aedddc287242 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/appender/histogram/NodeHistogramAppenderTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/appender/histogram/NodeHistogramAppenderTest.java @@ -16,7 +16,6 @@ package com.navercorp.pinpoint.web.applicationmap.appender.histogram; -import com.google.common.util.concurrent.MoreExecutors; import com.navercorp.pinpoint.common.server.util.time.Range; import com.navercorp.pinpoint.common.trace.HistogramSlot; import com.navercorp.pinpoint.common.trace.ServiceType; @@ -34,16 +33,15 @@ import com.navercorp.pinpoint.web.vo.Application; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import java.time.Duration; import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -61,9 +59,8 @@ public class NodeHistogramAppenderTest { private final Logger logger = LogManager.getLogger(getClass()); - private final ExecutorService executor = Executors.newFixedThreadPool(4); - - private final NodeHistogramAppenderFactory nodeHistogramAppenderFactory = new NodeHistogramAppenderFactory(executor); + @AutoClose("shutdown") + private static final Executor executor = Executors.newFixedThreadPool(4); private WasNodeHistogramDataSource wasNodeHistogramDataSource; @@ -75,12 +72,8 @@ public class NodeHistogramAppenderTest { public void setUp() { wasNodeHistogramDataSource = mock(WasNodeHistogramDataSource.class); NodeHistogramFactory nodeHistogramFactory = new DefaultNodeHistogramFactory(wasNodeHistogramDataSource); - nodeHistogramAppender = nodeHistogramAppenderFactory.create(nodeHistogramFactory); - } - - @AfterEach - public void cleanUp() { - MoreExecutors.shutdownAndAwaitTermination(executor, Duration.ofSeconds(3)); + NodeHistogramAppenderFactory factory = new NodeHistogramAppenderFactory(executor); + nodeHistogramAppender = factory.create(nodeHistogramFactory); } @Test diff --git a/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/service/FilteredMapServiceImplTest.java b/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/service/FilteredMapServiceImplTest.java index 52c373411494..fc9162242679 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/service/FilteredMapServiceImplTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/service/FilteredMapServiceImplTest.java @@ -17,7 +17,6 @@ package com.navercorp.pinpoint.web.applicationmap.service; -import com.google.common.util.concurrent.MoreExecutors; import com.navercorp.pinpoint.common.server.bo.SpanBo; import com.navercorp.pinpoint.common.server.bo.SpanEventBo; import com.navercorp.pinpoint.common.server.util.json.JsonField; @@ -54,8 +53,8 @@ import com.navercorp.pinpoint.web.view.TimeViewModel; import com.navercorp.pinpoint.web.view.id.AgentNameView; import com.navercorp.pinpoint.web.vo.Application; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -64,14 +63,13 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.util.CollectionUtils; -import java.time.Duration; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Random; -import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executor; import java.util.concurrent.Executors; import static org.assertj.core.api.Assertions.assertThat; @@ -93,8 +91,8 @@ public class FilteredMapServiceImplTest { private static final Random RANDOM = new Random(); - private final ExecutorService executor = Executors.newFixedThreadPool(8); - + @AutoClose("shutdown") + private static final Executor executor = Executors.newFixedThreadPool(8); @Mock private TraceDao traceDao; @@ -151,11 +149,6 @@ public void init() { } - @AfterEach - public void cleanUp() { - MoreExecutors.shutdownAndAwaitTermination(executor, Duration.ofSeconds(3)); - } - /** * USER -> ROOT_APP -> APP_A -> CACHE */ diff --git a/web/src/test/java/com/navercorp/pinpoint/web/task/ChainedTaskDecoratorTest.java b/web/src/test/java/com/navercorp/pinpoint/web/task/ChainedTaskDecoratorTest.java index b5b57fafce2e..d21cb6d7ac2d 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/task/ChainedTaskDecoratorTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/task/ChainedTaskDecoratorTest.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.web.task; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.core.task.SimpleAsyncTaskExecutor; @@ -31,7 +32,7 @@ * @author HyunGil Jeong */ public class ChainedTaskDecoratorTest { - + @AutoClose private SimpleAsyncTaskExecutor executor; @BeforeEach @@ -39,6 +40,7 @@ public void setup() { executor = new SimpleAsyncTaskExecutor("Test-Worker-"); } + @Test public void chainedDecoratorsShouldBeCalled() throws InterruptedException { // Given @@ -54,7 +56,7 @@ public void chainedDecoratorsShouldBeCalled() throws InterruptedException { for (int i = 0; i < testCount; i++) { executor.execute(new TestWorker(completeLatch)); } - completeLatch.await(5L, TimeUnit.SECONDS); + Assertions.assertTrue(completeLatch.await(5L, TimeUnit.SECONDS)); // Then Assertions.assertEquals(testCount, decorator1.getCount()); Assertions.assertEquals(testCount, decorator2.getCount()); diff --git a/web/src/test/java/com/navercorp/pinpoint/web/task/RequestContextPropagatingTaskDecoratorTest.java b/web/src/test/java/com/navercorp/pinpoint/web/task/RequestContextPropagatingTaskDecoratorTest.java index 577d1fb5c402..5e9b6f59100e 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/task/RequestContextPropagatingTaskDecoratorTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/task/RequestContextPropagatingTaskDecoratorTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -38,13 +39,15 @@ public class RequestContextPropagatingTaskDecoratorTest { private final RequestContextPropagatingTaskDecorator decorator = new RequestContextPropagatingTaskDecorator(); - private final SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("Test-Worker-"); + @AutoClose + private SimpleAsyncTaskExecutor executor; @Mock private RequestAttributes requestAttributes; @BeforeEach public void setup() { + executor = new SimpleAsyncTaskExecutor("Test-Worker-"); executor.setTaskDecorator(decorator); } @@ -72,7 +75,7 @@ public void onError() { for (int i = 0; i < testCount; i++) { executor.execute(new TestWorker(completeLatch, workerCallback)); } - completeLatch.await(5, TimeUnit.SECONDS); + Assertions.assertTrue(completeLatch.await(5, TimeUnit.SECONDS)); // Then boolean testVerified = verifiedFlag.get(); Assertions.assertTrue(testVerified, "RequestContext has not been propagated"); diff --git a/web/src/test/java/com/navercorp/pinpoint/web/task/SecurityContextPropagatingTaskDecoratorTest.java b/web/src/test/java/com/navercorp/pinpoint/web/task/SecurityContextPropagatingTaskDecoratorTest.java index 5520826636ad..57653d331420 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/task/SecurityContextPropagatingTaskDecoratorTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/task/SecurityContextPropagatingTaskDecoratorTest.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.web.task; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -37,13 +38,15 @@ public class SecurityContextPropagatingTaskDecoratorTest { private final SecurityContextPropagatingTaskDecorator decorator = new SecurityContextPropagatingTaskDecorator(); - private final SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("Test-Worker-"); + @AutoClose + private SimpleAsyncTaskExecutor executor; @Mock private SecurityContext securityContext; @BeforeEach public void setup() { + executor = new SimpleAsyncTaskExecutor("Test-Worker-"); executor.setTaskDecorator(decorator); } @@ -71,7 +74,7 @@ public void onError() { for (int i = 0; i < testCount; i++) { executor.execute(new TestWorker(completeLatch, workerCallback)); } - completeLatch.await(5, TimeUnit.SECONDS); + Assertions.assertTrue(completeLatch.await(5, TimeUnit.SECONDS)); // Then boolean testVerified = verifiedFlag.get(); Assertions.assertTrue(testVerified, "SecurityContext has not been propagated");