Skip to content

Commit

Permalink
Integrate pinning detection to HelidonTest
Browse files Browse the repository at this point in the history
 * TestNG with always helidon fix

Signed-off-by: Daniel Kec <daniel.kec@oracle.com>
  • Loading branch information
danielkec committed Dec 19, 2024
1 parent 2513001 commit 4fbf405
Show file tree
Hide file tree
Showing 36 changed files with 885 additions and 375 deletions.
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,11 @@
<version>${helidon.version}</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>io.helidon.common.testing</groupId>
<artifactId>helidon-common-testing-virtual-threads</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-junit5</artifactId>
Expand Down
1 change: 1 addition & 0 deletions common/testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
<modules>
<module>junit5</module>
<module>http-junit5</module>
<module>virtual-threads</module>
</modules>
</project>
32 changes: 32 additions & 0 deletions common/testing/virtual-threads/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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.common.testing</groupId>
<artifactId>helidon-common-testing-project</artifactId>
<version>4.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>helidon-common-testing-virtual-threads</artifactId>

<name>Helidon Virtual Threads Testing Utilities</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.common.testing.virtualthreads;

import jdk.jfr.consumer.RecordedEvent;

/**
* Assertion error used for reporting of Virtual Thread pinning detected during test.
*/
public class PinningAssertionError extends AssertionError {

/**
* Pinning JFR event.
*/
private final RecordedEvent recordedEvent;

/**
* Create new pinning exception.
*
* @param recordedEvent pinning JFR event
*/
PinningAssertionError(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,88 @@
/*
* 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.common.testing.virtualthreads;

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 {

/**
* Default threshold for considering carrier thread blocking as pinning.
*/
public static final long DEFAULT_THRESHOLD = 20;
private static final String JFR_EVENT_VIRTUAL_THREAD_PINNED = "jdk.VirtualThreadPinned";
private final RecordingStream recordingStream = new RecordingStream();
private volatile PinningAssertionError pinningAssertionError;

private PinningRecorder() {
//noop
}

/**
* Create new pinning JFR event recorder.
*
* @return new pinning recorder
*/
public static PinningRecorder create() {
return new PinningRecorder();
}

/**
* 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 (pinningAssertionError != null) {
throw pinningAssertionError;
}
}

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

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;

/**
* An annotation making this test class to fail at the end if a pinned virtual thread was detected.
* Virtual Threads testing features.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface PinnedThreadValidation {
}
package io.helidon.common.testing.virtualthreads;
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.webserver.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.
* Virtual Thread testing features for Helidon test extensions.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ExtendWith(HelidonPinnedThreadValidationJunitExtension.class)
@Inherited
public @interface PinnedThreadValidation {
}
module io.helidon.common.testing.vitualthreads {
requires jdk.jfr;
exports io.helidon.common.testing.virtualthreads to
io.helidon.microprofile.testing.junit5,
io.helidon.microprofile.testing.testng,
io.helidon.webserver.testing.junit5;
}
6 changes: 6 additions & 0 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<version.lib.jersey>3.1.9</version.lib.jersey>
<version.lib.jgit>6.7.0.202309050840-r</version.lib.jgit>
<version.lib.junit>5.9.3</version.lib.junit>
<version.lib.junit-platform-testkit>1.11.3</version.lib.junit-platform-testkit>
<version.lib.kafka>3.8.1</version.lib.kafka>
<version.lib.log4j>2.21.1</version.lib.log4j>
<version.lib.logback>1.4.14</version.lib.logback>
Expand Down Expand Up @@ -1284,6 +1285,11 @@
<artifactId>hamcrest-core</artifactId>
<version>${version.lib.hamcrest}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<version>${version.lib.junit-platform-testkit}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
29 changes: 29 additions & 0 deletions docs/src/main/asciidoc/mp/testing/testing-ng.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ include::{rootdir}/includes/mp.adoc[]
- <<Usage, Usage>>
- <<API, API>>
- <<Examples, Examples>>
- <<Virtual Threads, Virtual Threads>>
- <<Reference, Reference>>
== Overview
Expand Down Expand Up @@ -150,6 +151,34 @@ include::{sourcedir}/mp/testing/TestingNgSnippets.java[tag=snippet_2, indent=0]
<3> Add JaxRs support to the current test class.
<4> Define a `RequestScoped` bean.
== Virtual Threads
Helidon tests are able to detect Virtual Threads pinning. A situation when carrier thread is blocked
in a way, that virtual thread scheduler can't use it for scheduling of other virtual threads.
This can happen for example when blocking native code is invoked, or prior to the JDK 24 when
blocking IO operation happens in a synchronized block.
Pinning can in some cases negatively affect application performance.
[source,java]
.Enable pinning detection
----
include::{sourcedir}/mp/testing/TestingNgSnippets.java[tag=snippet_3, indent=0]
----
Pinning is considered as harmful when it takes longer than 20 milliseconds,
that is also the default when detecting it within Helidon tests.
Pinning threshold can be changed with:
[source,java]
.Configure pinning threshold
----
include::{sourcedir}/mp/testing/TestingNgSnippets.java[tag=snippet_4, indent=0]
----
<1> Change pinning threshold from default(20) to 50 milliseconds.
When pinning is detected, test fails with stacktrace pointing to the line of code causing it.
== Reference
* https://testng.org[TestNG Documentation]
28 changes: 28 additions & 0 deletions docs/src/main/asciidoc/mp/testing/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ include::{rootdir}/includes/mp.adoc[]
- <<Usage, Usage>>
- <<Examples, Examples>>
- <<Mock Support, Mock Support>>
- <<Virtual Threads, Virtual Threads>>
- <<Additional Information, Additional Information>>
- <<Reference, Reference>>
Expand Down Expand Up @@ -224,6 +225,33 @@ include::{sourcedir}/mp/testing/CDIMockingSnippets.java[tag=snippet_2, indent=0]
<4> Test that the mock is injected with modified behavior.
<5> Test the real method behavior.
== Virtual Threads
Helidon tests are able to detect Virtual Threads pinning. A situation when carrier thread is blocked
in a way, that virtual thread scheduler can't use it for scheduling of other virtual threads.
This can happen for example when blocking native code is invoked, or prior to the JDK 24 when
blocking IO operation happens in a synchronized block.
Pinning can in some cases negatively affect application performance.
[source,java]
.Enable pinning detection
----
include::{sourcedir}/mp/testing/TestingSnippets.java[tag=snippet_3, indent=0]
----
Pinning is considered as harmful when it takes longer than 20 milliseconds,
that is also the default when detecting it within Helidon tests.
Pinning threshold can be changed with:
[source,java]
.Configure pinning threshold
----
include::{sourcedir}/mp/testing/TestingSnippets.java[tag=snippet_4, indent=0]
----
<1> Change pinning threshold from default(20) to 50 milliseconds.
When pinning is detected, test fails with stacktrace pointing to the line of code causing it.
== Additional Information
* https://medium.com/helidon/testing-helidon-9df2ea14e22[Official blog article about Helidon and JUnit usage]
Expand Down
28 changes: 28 additions & 0 deletions docs/src/main/asciidoc/se/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ include::{rootdir}/includes/se.adoc[]
- <<Maven Coordinates, Maven Coordinates>>
- <<Usage, Usage>>
- <<Examples, Examples>>
- <<Virtual Threads, Virtual Threads>>
- <<Additional Information, Additional Information>>
- <<Reference, Reference>>
Expand Down Expand Up @@ -207,6 +208,33 @@ It is required to annotate the test class with the `@RoutingTest` annotation to
Routing is configured the same way as in full server testing using the `@SetUpRoute` annotation.
== Virtual Threads
Helidon tests are able to detect Virtual Threads pinning. A situation when carrier thread is blocked
in a way, that virtual thread scheduler can't use it for scheduling of other virtual threads.
This can happen for example when blocking native code is invoked, or prior to the JDK 24 when
blocking IO operation happens in a synchronized block.
Pinning can in some cases negatively affect application performance.
[source,java]
.Enable pinning detection
----
include::{sourcedir}/se/TestingSnippets.java[tag=snippet_6, indent=0]
----
Pinning is considered as harmful when it takes longer than 20 milliseconds,
that is also the default when detecting it within Helidon tests.
Pinning threshold can be changed with:
[source,java]
.Configure pinning threshold
----
include::{sourcedir}/se/TestingSnippets.java[tag=snippet_7, indent=0]
----
<1> Change pinning threshold from default(20) to 50 milliseconds.
When pinning is detected, test fails with stacktrace pointing to the line of code causing it.
== Additional Information
=== WebSocket Testing
Expand Down
Loading

0 comments on commit 4fbf405

Please sign in to comment.