Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding hierarchy scanning for reporting config #99

Merged
merged 1 commit into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public class JUnitPerfInterceptor implements InvocationInterceptor, TestInstance

@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {

SuiteRegistry.register(context);

JUnitPerfReportingConfig reportingConfig = findTestActiveConfigField(testInstance, context);

if (nonNull(reportingConfig)) {
activeReporters = reportingConfig.getReportGenerators();
activeStatisticsCalculator = reportingConfig.getStatisticsCalculatorSupplier().get();
Expand Down Expand Up @@ -134,12 +134,12 @@ protected JUnitPerfTest getJUnitPerfTestDetails(Method method, ExtensionContext
JUnitPerfTest classAnnotation = method.getDeclaringClass().getAnnotation(JUnitPerfTest.class);
JUnitPerfTest suiteAnnotation = SuiteRegistry.getPerfTestData(ctxt);
// Precedence: method, then class, then suite
JUnitPerfTest specifiedAnnotation = nonNull(methodAnnotation) ? methodAnnotation : classAnnotation;
JUnitPerfTest specifiedAnnotation = nonNull(methodAnnotation) ? methodAnnotation : classAnnotation;
return nonNull(specifiedAnnotation) ? specifiedAnnotation : suiteAnnotation;
}

protected EvaluationContext createEvaluationContext(Method method, boolean isAsync) {
EvaluationContext ctx = new EvaluationContext(method.getName(), nanoTime(), isAsync);
EvaluationContext ctx = new EvaluationContext(method.getName(), nanoTime(), isAsync);
ctx.setGroupName(method.getDeclaringClass().getSimpleName());
return ctx;
}
Expand All @@ -158,14 +158,23 @@ private static void warnIfNonStatic(Field field) {
}

private static JUnitPerfReportingConfig findTestActiveConfigField(Object testInstance, ExtensionContext ctxt) throws IllegalAccessException {
for (Field field : testInstance.getClass().getDeclaredFields()) {
Class<?> testClass = testInstance.getClass();
JUnitPerfReportingConfig config = scanForReportingConfig(testInstance, testClass);
return isNull(config) ? SuiteRegistry.getReportingConfig(ctxt) : config;
}

private static JUnitPerfReportingConfig scanForReportingConfig(Object testInstance, Class<?> testClass) throws IllegalAccessException {
if (isNull(testClass)) {
return null;
}
for (Field field : testClass.getDeclaredFields()) {
if (field.isAnnotationPresent(JUnitPerfTestActiveConfig.class)) {
warnIfNonStatic(field);
field.setAccessible(true);
return (JUnitPerfReportingConfig) field.get(testInstance);
}
}
return SuiteRegistry.getReportingConfig(ctxt);
return scanForReportingConfig(testInstance, testClass.getSuperclass());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,34 @@ void whenASuiteAnnotationsAreAvailable_thenSuiteAnnotationsShouldBeUsed() throws

}

private static void mockActiveSuite(ExtensionContext extensionContextMock, Class<?> suiteClass) {
when(extensionContextMock.getRoot()).thenReturn(extensionContextMock);
when(extensionContextMock.getUniqueId()).thenReturn(buildSuiteId(suiteClass));
}
@Test
void whenAChildClassInheritsFromABaseClassWithReportingConfig_thenChildClassShouldHaveAccessToReportingConfig() throws Throwable {
SampleChildTest test = new SampleChildTest();

Method methodMock = test.getClass().getMethod("someTestMethod");
PerformanceEvaluationStatement statementMock = mock(PerformanceEvaluationStatement.class);
Invocation<Void> invocationMock = mock(Invocation.class);
ReflectiveInvocationContext<Method> invocationContextMock = mock(ReflectiveInvocationContext.class);
ExtensionContext extensionContextMock = mockTestContext();

when(extensionContextMock.getRequiredTestMethod()).thenReturn(methodMock);
when(extensionContextMock.getRequiredTestClass()).thenReturn((Class) test.getClass());
when(statementBuilderMock.build()).thenReturn(statementMock);

interceptor.postProcessTestInstance(test, extensionContextMock);
interceptor.statementBuilder = statementBuilderMock;
interceptor.interceptTestMethod(invocationMock, invocationContextMock, extensionContextMock);

assertTrue(interceptor.measurementsStartTimeMs > 0);

assertEquals(1, interceptor.activeReporters.size());
assertEquals( SampleBaseTest.config.getReportGenerators(), interceptor.activeReporters);

EvaluationContext context = captureEvaluationContext();
assertEquals(120, context.getConfiguredExecutionTarget());
assertEquals(15, context.getConfiguredThreads());
assertEquals(67, context.getRequiredThroughput());

private static String buildSuiteId(Class<?> clazz) {
return "[engine:junit-platform-suite]/[suite:" + clazz.getName() + "]/[engine:junit-jupiter]";
}

@Test
Expand All @@ -250,6 +271,15 @@ void whenInterceptorSupportsParameterIsCalled_thenParameterTypeShouldBeChecked()
void whenInterceptorResolveParameterIsCalled_thenTestContextSupplierShouldBeReturned() {
assertTrue(interceptor.resolveParameter(null, null) instanceof TestContextSupplier);
}

private static void mockActiveSuite(ExtensionContext extensionContextMock, Class<?> suiteClass) {
when(extensionContextMock.getRoot()).thenReturn(extensionContextMock);
when(extensionContextMock.getUniqueId()).thenReturn(buildSuiteId(suiteClass));
}

private static String buildSuiteId(Class<?> clazz) {
return "[engine:junit-platform-suite]/[suite:" + clazz.getName() + "]/[engine:junit-jupiter]";
}

private static ParameterContext mockTestContextSupplierParameterType() throws NoSuchMethodException {
Method methodMock = SampleAsyncAnnotatedTest.class.getMethod("someTestMethod", TestContextSupplier.class);
Expand Down Expand Up @@ -355,5 +385,22 @@ public static class SuiteSampleTest {
.reportGenerator(new ConsoleReportGenerator())
.build();
}

public static class SampleBaseTest {
@JUnitPerfTestActiveConfig
public static final JUnitPerfReportingConfig config = JUnitPerfReportingConfig.builder()
.reportGenerator(new HtmlReportGenerator())
.build();
}

@Disabled
@JUnitPerfTest( threads = 15, totalExecutions = 120)
@JUnitPerfTestRequirement(executionsPerSec = 67)
public static class SampleChildTest extends SampleBaseTest {
@Test
public void someTestMethod() {
assertTrue(true);
}
}

}