Skip to content

Commit

Permalink
[#noissue] Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Apr 9, 2024
1 parent 1945a25 commit 1c30063
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,34 @@
package com.navercorp.pinpoint.test.plugin.junit5.descriptor;

import com.navercorp.pinpoint.profiler.test.junit5.TestContext;
import com.navercorp.pinpoint.test.plugin.util.ThreadContextExecutor;
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.jupiter.engine.descriptor.ClassTestDescriptor;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.platform.engine.UniqueId;


public class PluginJunitTestClassTestDescriptor extends ClassTestDescriptor {

private TestContext testContext;
private final ThreadContextExecutor executor;

public PluginJunitTestClassTestDescriptor(UniqueId uniqueId, Class<?> testClass, JupiterConfiguration configuration, TestContext testContext) {
super(uniqueId, testClass, configuration);
this.testContext = testContext;
this.executor = new ThreadContextExecutor(testContext.getClassLoader());
}


@Override
public JupiterEngineExecutionContext before(JupiterEngineExecutionContext context) {
final Thread thread = Thread.currentThread();
final ClassLoader originalClassLoader = thread.getContextClassLoader();
try {
thread.setContextClassLoader(testContext.getClassLoader());
return this.executor.call(() -> {
return super.before(context);
} finally {
thread.setContextClassLoader(originalClassLoader);
}
});
}

@Override
public void after(JupiterEngineExecutionContext context) {
final Thread thread = Thread.currentThread();
final ClassLoader originalClassLoader = thread.getContextClassLoader();
try {
thread.setContextClassLoader(testContext.getClassLoader());
this.executor.execute(() -> {
super.after(context);
} finally {
thread.setContextClassLoader(originalClassLoader);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@
import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext;
import com.navercorp.pinpoint.profiler.test.junit5.IsRootSpan;
import com.navercorp.pinpoint.profiler.test.junit5.TestContext;
import com.navercorp.pinpoint.test.plugin.util.ThreadContextExecutor;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.platform.engine.UniqueId;

import java.lang.reflect.Method;
import java.util.Objects;

public class PluginJunitTestMethodTestDescriptor extends TestMethodTestDescriptor {

private TestContext testContext;
private Method testMethod;
private final TestContext testContext;
private final ThreadContextExecutor executor;


public PluginJunitTestMethodTestDescriptor(UniqueId uniqueId, Class<?> testClass, Method testMethod, JupiterConfiguration configuration, TestContext testContext) {
super(uniqueId, testClass, testMethod, configuration);
this.testContext = testContext;
this.testMethod = testMethod;
this.testContext = Objects.requireNonNull(testContext, "testContext");
this.executor = new ThreadContextExecutor(testContext.getClassLoader());
}

@Override
Expand All @@ -48,13 +51,11 @@ public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext conte
setupBaseTest(extensionContext.getRequiredTestInstance());

beginTracing();
final Thread thread = Thread.currentThread();
final ClassLoader originalClassLoader = thread.getContextClassLoader();
try {
thread.setContextClassLoader(testContext.getClassLoader());
super.execute(context, dynamicTestExecutor);
executor.execute(() -> {
super.execute(context, dynamicTestExecutor);
});
} finally {
thread.setContextClassLoader(originalClassLoader);
endTracing();
}

Expand Down Expand Up @@ -104,7 +105,7 @@ private void endTracing() {
}

private boolean shouldCreateNewTraceObject() {
IsRootSpan isRootSpan = this.testMethod.getAnnotation(IsRootSpan.class);
IsRootSpan isRootSpan = getTestMethod().getAnnotation(IsRootSpan.class);
return isRootSpan == null || !isRootSpan.value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@
package com.navercorp.pinpoint.test.plugin.shared;

import com.navercorp.pinpoint.test.plugin.classloader.PluginTestClassLoader;
import com.navercorp.pinpoint.test.plugin.util.ThreadContextExecutor;

public class PluginSharedInstance {

private String className;
private String sharedClassName;
private final String className;
private final String sharedClassName;
private PluginTestClassLoader classLoader;
private Class<?> sharedClass;
private final ThreadContextExecutor executor;

Object object = null;

public PluginSharedInstance(String className, String sharedClassName, PluginTestClassLoader classLoader) {
this.className = className;
this.sharedClassName = sharedClassName;
this.classLoader = classLoader;
this.executor = new ThreadContextExecutor(classLoader);
}

public void before() {
Expand All @@ -41,34 +44,29 @@ public void before() {
throw new RuntimeException(e);
}

Thread thread = Thread.currentThread();
final ClassLoader currentClassLoader = thread.getContextClassLoader();
try {
thread.setContextClassLoader(this.classLoader);
this.object = sharedClass.newInstance();
if (object instanceof SharedTestLifeCycle) {
((SharedTestLifeCycle) object).beforeAll();

this.executor.execute(() -> {
try {
this.object = sharedClass.newInstance();
if (object instanceof SharedTestLifeCycle) {
((SharedTestLifeCycle) object).beforeAll();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
thread.setContextClassLoader(currentClassLoader);
}
});
}

public void after() {
Thread thread = Thread.currentThread();
final ClassLoader currentClassLoader = thread.getContextClassLoader();
try {
thread.setContextClassLoader(this.classLoader);
if (object instanceof SharedTestLifeCycle) {
((SharedTestLifeCycle) object).afterAll();
executor.execute(() -> {
try {
if (object instanceof SharedTestLifeCycle) {
((SharedTestLifeCycle) object).afterAll();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
thread.setContextClassLoader(currentClassLoader);
}
});
}

public void clear() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.navercorp.pinpoint.test.plugin.util;

import java.util.Objects;

public class ThreadContextExecutor {

private final ClassLoader context;

public ThreadContextExecutor(ClassLoader context) {
// @Nullable
this.context = context;
}

public ClassLoader getClassLoader() {
return context;
}

public void execute(Executable executable) {
Objects.requireNonNull(executable, "executable");

final Thread thread = Thread.currentThread();
final ClassLoader before = thread.getContextClassLoader();
thread.setContextClassLoader(context);
try {
executable.execute();
} finally {
thread.setContextClassLoader(before);
}
}


public <V> V call(Callable<V> callable) {
Objects.requireNonNull(callable, "callable");

final Thread thread = Thread.currentThread();
final ClassLoader before = thread.getContextClassLoader();
thread.setContextClassLoader(context);
try {
return callable.call();
} finally {
thread.setContextClassLoader(before);
}
}

@FunctionalInterface
public interface Executable {
void execute();
}

@FunctionalInterface
public interface Callable<V> {
V call();
}
}

0 comments on commit 1c30063

Please sign in to comment.