diff --git a/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/ExecuteSharedThread.java b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/ExecuteSharedThread.java deleted file mode 100644 index a4a9f8eb2f02..000000000000 --- a/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/ExecuteSharedThread.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright 2021 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.test.plugin.shared; - -import com.navercorp.pinpoint.test.plugin.util.TestLogger; - -import org.tinylog.TaggedLogger; - -import java.util.Objects; -import java.util.Properties; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -/** - * @author Taejin Koo - */ -public class ExecuteSharedThread { - - private static final TaggedLogger logger = TestLogger.getLogger(); - - private final Thread thread; - private final ExecuteSharedThreadRunnable runnable; - private final String testClazzName; - private final ClassLoader testClassLoader; - - private final Properties properties = new Properties(); - - private final CountDownLatch beforeCompleteLatch = new CountDownLatch(1); - private final CountDownLatch afterStartLatch = new CountDownLatch(1); - - public ExecuteSharedThread(String testClazzName, ClassLoader testClassLoader) { - this.testClazzName = Objects.requireNonNull(testClazzName, "testClazzName"); - this.testClassLoader = Objects.requireNonNull(testClassLoader, "testClassLoader"); - - this.runnable = new ExecuteSharedThreadRunnable(); - - thread = new Thread(runnable); - thread.setName(testClazzName + "-Shared-Thread"); - thread.setContextClassLoader(testClassLoader); - thread.setDaemon(true); - } - - void startBefore() { - thread.start(); - } - - public SharedTestLifeCycleWrapper getSharedClassWrapper() { - return runnable.sharedTestLifeCycleWrapper; - } - - public Throwable getRunnableError() { - return runnable.throwable; - } - - boolean awaitBeforeCompleted(long timeout, TimeUnit unit) { - if (!thread.isAlive()) { - throw new IllegalStateException("Thread is not alive."); - } - - try { - return beforeCompleteLatch.await(timeout, unit); - } catch (InterruptedException e) { - logger.warn("awaitBeforeCompleted() interrupted. message:{}", e.getMessage()); - } - return false; - } - - private void setBeforeCompleted() { - beforeCompleteLatch.countDown(); - } - - void startAfter() { - if (!thread.isAlive()) { - throw new IllegalStateException("Thread is not alive."); - } - - long count = afterStartLatch.getCount(); - if (count == 0) { - logger.info("startAfter() already executed."); - } else { - afterStart(); - } - } - - private void afterStart() { - afterStartLatch.countDown(); - } - - private boolean awaitAfterStart() { - try { - afterStartLatch.await(); - return true; - } catch (InterruptedException e) { - logger.error(e, "awaitAfterStart interrupted. message:{}", e.getMessage()); - } - return false; - } - - Properties getProperties() { - if (beforeCompleteLatch.getCount() == 0) { - return properties; - } else { - throw new IllegalStateException("SharedBeforeClass is not completed."); - } - } - - boolean join(long millis) { - try { - thread.join(millis); - return true; - } catch (InterruptedException e) { - logger.warn("join() interrupted. message:{}", e.getMessage()); - } - return false; - } - - private class ExecuteSharedThreadRunnable implements Runnable { - private volatile SharedTestLifeCycleWrapper sharedTestLifeCycleWrapper; - private volatile Throwable throwable; - @Override - public void run() { - try { - Class testClazz = loadClass(); - - logger.debug("Execute testClazz:{} cl:{}", testClazz.getName(), testClazz.getClassLoader()); - - sharedTestLifeCycleWrapper = SharedTestLifeCycleWrapper.newVersionTestLifeCycleWrapper(testClazz); - if (sharedTestLifeCycleWrapper != null) { - sharedTestLifeCycleWrapper.beforeAll(); - } - } catch (Throwable th) { - logger.warn("{} testclass error", testClazzName, th); - throwable = th; - } finally { - setBeforeCompleted(); - } - - awaitAfterStart(); - if (sharedTestLifeCycleWrapper != null) { - sharedTestLifeCycleWrapper.afterAll(); - } - } - - private Class loadClass() { - try { - return testClassLoader.loadClass(testClazzName); - } catch (ClassNotFoundException e) { - logger.error(e, "testClazz:{} not found", testClazzName); - throw new RuntimeException(e); - } - } - } - -} diff --git a/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedPinpointPluginTest.java b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedPinpointPluginTest.java index d777e50d4ee1..756499fd0b83 100644 --- a/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedPinpointPluginTest.java +++ b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedPinpointPluginTest.java @@ -39,7 +39,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Properties; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; @@ -50,7 +49,7 @@ public class SharedPinpointPluginTest { private static final TaggedLogger logger = TestLogger.getLogger(); - public static void main(String[] args) throws Throwable { + public static void main(String[] args) throws Exception { final String mavenDependencyResolverClassPaths = System.getProperty(SharedPluginTestConstants.MAVEN_DEPENDENCY_RESOLVER_CLASS_PATHS); if (mavenDependencyResolverClassPaths == null) { logger.error("mavenDependencyResolverClassPaths must not be empty"); @@ -202,7 +201,7 @@ private void logTestInformation() { } } - public void execute() throws Throwable { + public void execute() throws Exception { logTestInformation(); ClassLoader mavenDependencyResolverClassLoader = new ChildFirstClassLoader(URLUtils.fileToUrls(mavenDependencyResolverClassPaths)); File testClazzLocation = new File(testLocation); @@ -211,34 +210,24 @@ public void execute() throws Throwable { executes(testInfos); } - private void executes(List testInfos) throws Throwable { + private void executes(List testInfos) { if (!CollectionUtils.hasLength(testInfos)) { return; } TestInfo firstTestInfo = testInfos.get(0); - final ClassLoader testClassLoader = createTestClassLoader(firstTestInfo); - ExecuteSharedThread executeSharedThread = new ExecuteSharedThread(testClazzName, testClassLoader); - - executeSharedThread.startBefore(); - executeSharedThread.awaitBeforeCompleted(10, TimeUnit.MINUTES); - Throwable runnableError = executeSharedThread.getRunnableError(); - if (runnableError != null) { - throw runnableError; - } - Properties properties = executeSharedThread.getProperties(); - if (logger.isDebugEnabled()) { - logger.debug("sharedThread properties:{}", properties); - } + final ClassLoader sharedClassLoader = createTestClassLoader(firstTestInfo); + SharedTestExecutor sharedTestExecutor = new SharedTestExecutor(testClazzName, sharedClassLoader); + + sharedTestExecutor.startBefore(10, TimeUnit.MINUTES); + - final SharedTestLifeCycleWrapper sharedTestLifeCycleWrapper = executeSharedThread.getSharedClassWrapper(); + final SharedTestLifeCycleWrapper sharedTestLifeCycleWrapper = sharedTestExecutor.getSharedClassWrapper(); for (TestInfo testInfo : testInfos) { - execute(testInfo, properties, sharedTestLifeCycleWrapper); + execute(testInfo, sharedTestLifeCycleWrapper); } - executeSharedThread.startAfter(); - - executeSharedThread.join(TimeUnit.MINUTES.toMillis(5)); + sharedTestExecutor.startAfter(5, TimeUnit.MINUTES); } private ClassLoader createTestClassLoader(TestInfo testInfo) { @@ -249,11 +238,10 @@ private ClassLoader createTestClassLoader(TestInfo testInfo) { } } URL[] urls = URLUtils.fileToUrls(dependencyFileList); - final ClassLoader testClassLoader = new ChildFirstClassLoader(urls, ProfilerClass.PINPOINT_PROFILER_CLASS); - return testClassLoader; + return new ChildFirstClassLoader(urls, ProfilerClass.PINPOINT_PROFILER_CLASS); } - private void execute(final TestInfo testInfo, final Properties properties, SharedTestLifeCycleWrapper sharedTestLifeCycleWrapper) { + private void execute(final TestInfo testInfo, SharedTestLifeCycleWrapper sharedTestLifeCycleWrapper) { try { final ClassLoader testClassLoader = createTestClassLoader(testInfo); @@ -294,7 +282,9 @@ private Class loadClass() { } }; String threadName = testClazzName + " " + testInfo.getTestId() + " Thread"; - Thread testThread = newThread(runnable, threadName, testClassLoader); + + ThreadFactory threadFactory = new ThreadFactory(threadName, testClassLoader); + Thread testThread = threadFactory.newThread(runnable); testThread.start(); testThread.join(TimeUnit.MINUTES.toMillis(5)); @@ -313,12 +303,4 @@ private void checkTerminatedState(Thread testThread, String testInfo) { } } - private Thread newThread(Runnable runnable, String threadName, ClassLoader testClassLoader) { - Thread testThread = new Thread(runnable); - testThread.setName(threadName); - testThread.setContextClassLoader(testClassLoader); - testThread.setDaemon(true); - return testThread; - } - } diff --git a/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestExecutor.java b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestExecutor.java new file mode 100644 index 000000000000..5377cee58373 --- /dev/null +++ b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestExecutor.java @@ -0,0 +1,106 @@ +/* + * Copyright 2021 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.test.plugin.shared; + +import com.navercorp.pinpoint.test.plugin.util.TestLogger; + +import org.tinylog.TaggedLogger; + +import java.util.Objects; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +/** + * @author Taejin Koo + */ +public class SharedTestExecutor { + + private static final TaggedLogger logger = TestLogger.getLogger(); + + private final ExecutorService executor; + private final String testClazzName; + private final ClassLoader testClassLoader; + + private volatile SharedTestLifeCycleWrapper sharedTestLifeCycleWrapper; + + public SharedTestExecutor(String testClazzName, ClassLoader testClassLoader) { + this.testClazzName = Objects.requireNonNull(testClazzName, "testClazzName"); + this.testClassLoader = Objects.requireNonNull(testClassLoader, "testClassLoader"); + + ThreadFactory threadFactory = new ThreadFactory(testClazzName + "-Shared-Executor", testClassLoader); + this.executor = Executors.newSingleThreadExecutor(threadFactory); + } + + void startBefore(long timeout, TimeUnit unit) { + Future future = this.executor.submit(this::beforeAll); + awaitFuture("startBefore", future, timeout, unit); + } + + private V awaitFuture(String action, Future future, long timeout, TimeUnit unit) { + try { + return future.get(timeout, unit); + } catch (ExecutionException | InterruptedException e) { + logger.warn("{} execution error {}", action, testClazzName, e); + throw new IllegalStateException(action + " execution error " + testClazzName, e); + } catch (TimeoutException e) { + future.cancel(true); + logger.warn("{} timeout {}", action, testClazzName); + throw new IllegalStateException(action + " timeout " + testClazzName); + } + } + + public SharedTestLifeCycleWrapper getSharedClassWrapper() { + return sharedTestLifeCycleWrapper; + } + + + void startAfter(long timeout, TimeUnit unit) { + Future future = this.executor.submit(this::afterAll); + awaitFuture("startAfter", future, timeout, unit); + } + + + private void beforeAll() { + Class testClazz = loadClass(); + + logger.debug("Execute testClazz:{} cl:{}", testClazz.getName(), testClazz.getClassLoader()); + + sharedTestLifeCycleWrapper = SharedTestLifeCycleWrapper.newSharedTestLifeCycleWrapper(testClazz); + if (sharedTestLifeCycleWrapper != null) { + sharedTestLifeCycleWrapper.beforeAll(); + } + } + + private void afterAll() { + if (sharedTestLifeCycleWrapper != null) { + sharedTestLifeCycleWrapper.afterAll(); + } + } + + private Class loadClass() { + try { + return testClassLoader.loadClass(testClazzName); + } catch (ClassNotFoundException e) { + logger.error(e, "testClazz:{} not found", testClazzName); + throw new RuntimeException(e); + } + } +} diff --git a/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestLifeCycleWrapper.java b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestLifeCycleWrapper.java index 5688ca54cd70..5f48370507a5 100644 --- a/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestLifeCycleWrapper.java +++ b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestLifeCycleWrapper.java @@ -10,7 +10,7 @@ public class SharedTestLifeCycleWrapper { private final SharedTestLifeCycle sharedTestLifecycle; private Properties lifeCycleResult; - private static Class getVersionTestLifeCycle(final Class testClazz) { + private static Class getSharedTestLifeCycle(final Class testClazz) { SharedTestLifeCycleClass sharedTestLifeCycleClass = testClazz.getAnnotation(SharedTestLifeCycleClass.class); if (sharedTestLifeCycleClass == null) { return null; @@ -18,8 +18,8 @@ private static Class getVersionTestLifeCycle(fina return sharedTestLifeCycleClass.value(); } - public static SharedTestLifeCycleWrapper newVersionTestLifeCycleWrapper(final Class testClazz) { - Class versionTestClazz = getVersionTestLifeCycle(testClazz); + public static SharedTestLifeCycleWrapper newSharedTestLifeCycleWrapper(final Class testClazz) { + Class versionTestClazz = getSharedTestLifeCycle(testClazz); if (versionTestClazz == null) { return null; } diff --git a/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/ThreadFactory.java b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/ThreadFactory.java new file mode 100644 index 000000000000..968929ff99a8 --- /dev/null +++ b/test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/ThreadFactory.java @@ -0,0 +1,22 @@ +package com.navercorp.pinpoint.test.plugin.shared; + +import java.util.Objects; + +public class ThreadFactory implements java.util.concurrent.ThreadFactory { + private final String threadName; + private final ClassLoader cl; + + public ThreadFactory(String threadName, ClassLoader cl) { + this.threadName = Objects.requireNonNull(threadName, "threadName"); + this.cl = Objects.requireNonNull(cl, "cl"); + } + + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setName(threadName); + thread.setContextClassLoader(cl); + thread.setDaemon(true); + return thread; + } +} diff --git a/test/src/test/java/com/navercorp/pinpoint/test/plugin/shared/TestParameterParserTest.java b/test/src/test/java/com/navercorp/pinpoint/test/plugin/shared/TestParameterParserTest.java index c7e9575981f3..34eba985d3db 100644 --- a/test/src/test/java/com/navercorp/pinpoint/test/plugin/shared/TestParameterParserTest.java +++ b/test/src/test/java/com/navercorp/pinpoint/test/plugin/shared/TestParameterParserTest.java @@ -19,7 +19,7 @@ import org.junit.Assert; import org.junit.Test; -import java.util.Arrays; +import java.util.Collections; import java.util.List; @@ -35,6 +35,6 @@ public void parse() { Assert.assertEquals(parameters.size(), 1); TestParameter one = parameters.get(0); Assert.assertEquals(one.getTestId(), "testId" ); - Assert.assertEquals(one.getMavenDependencies(), Arrays.asList("dependency1") ); + Assert.assertEquals(one.getMavenDependencies(), Collections.singletonList("dependency1")); } } \ No newline at end of file