Skip to content

Commit

Permalink
[pinpoint-apm#11705] Refactor PluginTestEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Nov 19, 2024
1 parent 8e43f7e commit 08eeb98
Show file tree
Hide file tree
Showing 26 changed files with 344 additions and 423 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import com.navercorp.pinpoint.test.plugin.classloader.PluginTestClassLoader;
import com.navercorp.pinpoint.test.plugin.shared.ThreadFactory;
import com.navercorp.pinpoint.test.plugin.util.CallExecutable;
import com.navercorp.pinpoint.test.plugin.util.RunExecutable;
import org.junit.platform.commons.JUnitException;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
Expand All @@ -29,12 +32,12 @@

public class DefaultPluginTestInstance implements PluginTestInstance {

private String id;
private final String id;
private PluginTestClassLoader classLoader;
private Class<?> testClass;
private boolean manageTraceObject;
private final Class<?> testClass;
private final boolean manageTraceObject;
private PluginTestInstanceCallback callback;
private ExecutorService executorService;
private final ExecutorService executorService;

public DefaultPluginTestInstance(String id, PluginTestClassLoader classLoader, Class<?> testClass, boolean manageTraceObject, PluginTestInstanceCallback callback) {
this.id = id;
Expand Down Expand Up @@ -63,10 +66,10 @@ public Class<?> getTestClass() {
return this.testClass;
}

public <T> T execute(final Callable<T> callable, boolean verify) {
public <T> T execute(final CallExecutable<T> callable, boolean verify) {
Callable<T> task = new Callable<T>() {
@Override
public T call() throws Exception {
public T call() {
try {
callback.before(verify, manageTraceObject);
return callable.call();
Expand All @@ -77,17 +80,38 @@ public T call() throws Exception {
};

Future<T> future = this.executorService.submit(task);
return await(future);
}

public void execute(final RunExecutable runnable, boolean verify) {
Runnable task = new Runnable() {
@Override
public void run() {
try {
callback.before(verify, manageTraceObject);
runnable.run();
} finally {
callback.after(verify, manageTraceObject);
}
}
};

Future<?> future = this.executorService.submit(task);
await(future);
}

private <T> T await(Future<T> future) {
try {
return future.get(30l, TimeUnit.SECONDS);
return future.get(30L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
throw new JUnitException(this.id + " failed", e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
throw new JUnitException(this.id + " failed", e);
} catch (TimeoutException e) {
// testcase interrupt
future.cancel(true);
throw new RuntimeException(e);
throw new JUnitException(this.id + " failed", e);
}
}

Expand All @@ -102,10 +126,11 @@ public void clear() {
this.classLoader = null;
}
if (this.executorService != null) {
this.executorService.shutdownNow();
this.executorService.shutdown();
try {
if (!this.executorService.awaitTermination(10, TimeUnit.SECONDS)) {
if (!this.executorService.awaitTermination(10L, TimeUnit.SECONDS)) {
System.err.println("ExecutorService did not terminate in the specified time");
this.executorService.shutdownNow();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package com.navercorp.pinpoint.test.plugin;

import java.util.concurrent.Callable;
import com.navercorp.pinpoint.test.plugin.util.CallExecutable;
import com.navercorp.pinpoint.test.plugin.util.RunExecutable;

public interface PluginTestInstance {
String getTestId();
Expand All @@ -25,7 +26,9 @@ public interface PluginTestInstance {

Class<?> getTestClass();

<T> T execute(final Callable<T> callable, boolean verify);
<T> T execute(final CallExecutable<T> callable, boolean verify);

void execute(final RunExecutable runnable, boolean verify);

void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.test.plugin.classloader.PluginAgentTestClassLoader;
import com.navercorp.pinpoint.test.plugin.classloader.PluginTestJunitTestClassLoader;
import com.navercorp.pinpoint.test.plugin.util.URLUtils;
import org.junit.platform.commons.JUnitException;

import java.io.File;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -70,11 +71,11 @@ List<String> getClassPath(List<String> libs, ClassLoding classLoading) {
PluginTestInstanceCallback startAgent(String configFile, ClassLoader classLoader) {
try {
Class<?> testClass = classLoader.loadClass(PluginTestAgentStarter.class.getName());
Constructor<?> constructor = testClass.getConstructor(String.class, ClassLoader.class);
Constructor<?> constructor = testClass.getConstructor(String.class);
Method method = testClass.getDeclaredMethod("getCallback");
return (PluginTestInstanceCallback) method.invoke(constructor.newInstance(configFile, classLoader));
return (PluginTestInstanceCallback) method.invoke(constructor.newInstance(configFile));
} catch (Exception e) {
throw new RuntimeException("agent configFile=" + configFile + ", classLoader=" + classLoader, e);
throw new JUnitException("agent configFile=" + configFile + ", classLoader=" + classLoader, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class PluginTestAgentStarter {

private DefaultApplicationContext applicationContext;

public PluginTestAgentStarter(String configFile, ClassLoader classLoader) {
public PluginTestAgentStarter(String configFile) {
final com.navercorp.pinpoint.profiler.test.MockApplicationContextFactory factory = new MockApplicationContextFactory();
this.applicationContext = factory.build(configFile);
this.mockInstrumentor = new MockInstrumentor(classLoader, applicationContext.getClassFileTransformer());
this.mockInstrumentor = new MockInstrumentor(applicationContext.getClassFileTransformer());
this.interceptorRegistryBinder = applicationContext.getInterceptorRegistryBinder();
this.pluginTestVerifier = new PluginVerifierExternalAdaptor(applicationContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
*/
public class MockInstrumentor {
private final Logger logger = LogManager.getLogger(this.getClass());
private final ClassLoader loader;
private final ClassFileTransformer dispatcher;

public MockInstrumentor(ClassLoader loader, ClassFileTransformer defaultTransformer) {
this.loader = loader;
public MockInstrumentor(ClassFileTransformer defaultTransformer) {
this.dispatcher = Objects.requireNonNull(defaultTransformer, "defaultTransformer");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext conte
MutableExtensionRegistry extensionRegistry = context.getExtensionRegistry();

JupiterEngineDescriptor engineDescriptor = new JupiterEngineDescriptor(this.getUniqueId(), configuration);
ExtensionContext classExtensionContext = ExtensionContextFactory.jupiterEngineContext(
ExtensionContext jupiterExtensionContext = ExtensionContextFactory.jupiterEngineContext(
context.getExecutionListener(), engineDescriptor,
context.getConfiguration(), ec -> new DefaultExecutableInvoker(ec, extensionRegistry));

// @formatter:off
return context.extend()
.withThrowableCollector(throwableCollector)
.withExtensionContext(classExtensionContext)
.withExtensionContext(jupiterExtensionContext)
.build();
// @formatter:on
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public JupiterEngineExecutionContext before(JupiterEngineExecutionContext contex
public void after(JupiterEngineExecutionContext context) {
this.pluginTestInstance.execute(() -> {
super.after(context);
return null;
}, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext conte
public JupiterEngineExecutionContext before(JupiterEngineExecutionContext context) {
ThrowableCollector throwableCollector = context.getThrowableCollector();
if (sharedInstance != null) {
throwableCollector.execute(() -> {
sharedInstance.before();
});
throwableCollector.execute(sharedInstance::before);
}

if (throwableCollector.isNotEmpty()) {
Expand All @@ -82,9 +80,7 @@ public void after(JupiterEngineExecutionContext context) {
Throwable previousThrowable = throwableCollector.getThrowable();

if (sharedInstance != null) {
throwableCollector.execute(() -> {
sharedInstance.after();
});
throwableCollector.execute(sharedInstance::after);
}

// If the previous Throwable was not null when this method was called,
Expand Down
Loading

0 comments on commit 08eeb98

Please sign in to comment.