Skip to content

Commit

Permalink
Integrate pinning detection to HelidonTest
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkec committed Dec 15, 2024
1 parent 940a889 commit 43b8fb0
Show file tree
Hide file tree
Showing 14 changed files with 380 additions and 125 deletions.
35 changes: 35 additions & 0 deletions microprofile/testing/common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-project</artifactId>
<version>4.2.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-microprofile-testing-common</artifactId>
<name>Helidon Microprofile Testing Common Utilities</name>

<description>
Integration with Mocking to support tests with CDI injection
</description>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.microprofile.testing.common;

import jdk.jfr.consumer.RecordedEvent;

/**
* Exception used for reporting of Virtual Thread pinning detected during test.
*/
public class PinningException extends AssertionError {
private final RecordedEvent recordedEvent;

PinningException(RecordedEvent recordedEvent) {
this.recordedEvent = recordedEvent;
if (recordedEvent.getStackTrace() != null) {
StackTraceElement[] stackTraceElements = recordedEvent.getStackTrace().getFrames().stream()
.map(f -> new StackTraceElement(f.getMethod().getType().getName(),
f.getMethod().getName(),
f.getMethod().getType().getName() + ".java",
f.getLineNumber()))
.toArray(StackTraceElement[]::new);
super.setStackTrace(stackTraceElements);
}
}

@Override
public String getMessage() {
return "Pinned virtual threads were detected:\n"
+ recordedEvent.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.microprofile.testing.common;

import java.time.Duration;

import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingStream;

/**
* Record pinned thread events and throw exception when detected.
*/
public class PinningRecorder implements AutoCloseable {

private static final String JFR_EVENT_VIRTUAL_THREAD_PINNED = "jdk.VirtualThreadPinned";
private final RecordingStream recordingStream = new RecordingStream();
private volatile PinningException pinningException;

/**
* Start async recording of {@code jdk.VirtualThreadPinned} JFR event.
*
* @param threshold time threshold for carrier thread blocking to be considered as pinning
*/
public void record(Duration threshold) {
recordingStream.enable(JFR_EVENT_VIRTUAL_THREAD_PINNED)
.withThreshold(threshold)
.withStackTrace();
recordingStream.onEvent(JFR_EVENT_VIRTUAL_THREAD_PINNED, this::record);
recordingStream.startAsync();
}

@Override
public void close() {
try {
// Flush ending events
recordingStream.stop();
} finally {
recordingStream.close();
}
checkAndThrow();
}

void checkAndThrow() {
if (pinningException != null) {
throw pinningException;
}
}

private void record(RecordedEvent event) {
PinningException e = new PinningException(event);
if (pinningException == null) {
pinningException = e;
} else {
pinningException.addSuppressed(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.testing.junit5;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* An annotation making this test class to fail at the end if a pinned virtual thread was detected.
* Common features for {@code @HelidonTest}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ExtendWith(HelidonPinnedThreadValidationJunitExtension.class)
@Inherited
public @interface PinnedThreadValidation {
}
package io.helidon.microprofile.testing.common;
23 changes: 23 additions & 0 deletions microprofile/testing/common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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.
*/

/**
* Common features for {@code @HelidonTest}.
*/
module io.helidon.microprofile.testing.common {
requires jdk.jfr;
exports io.helidon.microprofile.testing.common;
}
5 changes: 5 additions & 0 deletions microprofile/testing/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<artifactId>helidon-microprofile-server</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-common</artifactId>
<version>4.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.cdi</groupId>
<artifactId>helidon-microprofile-cdi</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import io.helidon.config.mp.MpConfigSources;
import io.helidon.microprofile.server.JaxRsCdiExtension;
import io.helidon.microprofile.server.ServerCdiExtension;
import io.helidon.microprofile.testing.common.PinningRecorder;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
Expand Down Expand Up @@ -111,7 +114,7 @@ class HelidonJunitExtension implements BeforeAllCallback,
private ConfigProviderResolver configProviderResolver;
private Config config;
private SeContainer container;

private PinningRecorder pinningRecorder;

@SuppressWarnings("unchecked")
@Override
Expand All @@ -137,6 +140,13 @@ public void beforeAll(ExtensionContext context) {
resetPerTest = testAnnot.resetPerTest();
}

pinningRecorder = new PinningRecorder();
if (testAnnot.pinningDetection()) {
pinningRecorder.record(Duration.ofMillis(Optional.ofNullable(testAnnot)
.map(HelidonTest::pinningThreshold)
.orElse(20L)));
}

DisableDiscovery discovery = getAnnotation(testClass, DisableDiscovery.class, metaAnnotations);
if (discovery != null) {
classLevelDisableDiscovery = discovery.value();
Expand Down Expand Up @@ -174,7 +184,7 @@ private List<Annotation> extractMetaAnnotations(Class<?> testClass) {
for (Annotation testAnnotation : testAnnotations) {
List<Annotation> annotations = List.of(testAnnotation.annotationType().getAnnotations());
List<Class<?>> annotationsClass = annotations.stream()
.map(a -> a.annotationType()).collect(Collectors.toList());
.map(Annotation::annotationType).collect(Collectors.toList());
if (!Collections.disjoint(HELIDON_TEST_ANNOTATIONS, annotationsClass)) {
// Contains at least one of HELIDON_TEST_ANNOTATIONS
return annotations;
Expand Down Expand Up @@ -268,6 +278,7 @@ public void afterEach(ExtensionContext context) throws Exception {
releaseConfig();
stopContainer();
}
// pinningRecorder.checkAndThrow();
}

private void validatePerClass() {
Expand Down Expand Up @@ -427,6 +438,8 @@ public void afterAll(ExtensionContext context) {
stopContainer();
releaseConfig();
callAfterStop();
pinningRecorder.close();
pinningRecorder = null;
}

@Override
Expand Down

This file was deleted.

Loading

0 comments on commit 43b8fb0

Please sign in to comment.