Skip to content

Commit

Permalink
4.x: Fail HelidonTests by default when pinning jfr event is detected h…
Browse files Browse the repository at this point in the history
…elidon-io#8857

Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Jun 13, 2024
1 parent 335c98b commit 3c71ff0
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordedFrame;
import jdk.jfr.consumer.RecordingStream;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
Expand All @@ -77,6 +80,7 @@
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;

import static org.junit.jupiter.api.Assertions.fail;

/**
* Junit5 extension to support Helidon CDI container in tests.
Expand All @@ -100,7 +104,9 @@ class HelidonJunitExtension implements BeforeAllCallback,

private final List<AddExtension> classLevelExtensions = new ArrayList<>();
private final List<AddBean> classLevelBeans = new ArrayList<>();
private final List<EventWrapper> jfrVTPinned = new ArrayList<>();
private final ConfigMeta classLevelConfigMeta = new ConfigMeta();
private final RecordingStream recordingStream = new RecordingStream();
private boolean classLevelDisableDiscovery = false;
private boolean resetPerTest;

Expand All @@ -113,6 +119,8 @@ class HelidonJunitExtension implements BeforeAllCallback,
@SuppressWarnings("unchecked")
@Override
public void beforeAll(ExtensionContext context) {
startRecordingStream();

testClass = context.getRequiredTestClass();

AddConfig[] configs = getAnnotations(testClass, AddConfig.class);
Expand Down Expand Up @@ -383,6 +391,27 @@ public void afterAll(ExtensionContext context) {
stopContainer();
releaseConfig();
callAfterStop();
closeRecordingStream();
}

private void startRecordingStream() {
recordingStream.enable("jdk.VirtualThreadPinned").withStackTrace();
recordingStream.onEvent("jdk.VirtualThreadPinned", event -> {
jfrVTPinned.add(new EventWrapper(event));
});
recordingStream.startAsync();
}

private void closeRecordingStream() {
try {
// Flush ending events
recordingStream.stop();
if (!jfrVTPinned.isEmpty()) {
fail("Some pinned virtual threads were detected:\n" + jfrVTPinned);
}
} finally {
recordingStream.close();
}
}

@Override
Expand Down Expand Up @@ -748,4 +777,28 @@ public Class<? extends Extension> value() {
}
}

private static class EventWrapper {

private final RecordedEvent recordedEvent;

private EventWrapper(RecordedEvent recordedEvent) {
this.recordedEvent = recordedEvent;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder(recordedEvent.toString());
if (recordedEvent.getStackTrace() != null) {
builder.append("full-stackTrace = [");
List<RecordedFrame> frames = recordedEvent.getStackTrace().getFrames();
for (RecordedFrame frame : frames) {
builder.append("\n\t").append(frame.getMethod().getType().getName())
.append("#").append(frame.getMethod().getName())
.append("(").append(frame.getLineNumber()).append(")");
}
builder.append("\n]");
}
return builder.toString();
}
}
}
1 change: 1 addition & 0 deletions microprofile/testing/junit5/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
requires io.helidon.microprofile.cdi;
requires jakarta.inject;
requires org.junit.jupiter.api;
requires jdk.jfr;

requires transitive jakarta.cdi;
requires transitive jakarta.ws.rs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
import jakarta.inject.Singleton;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordedFrame;
import jdk.jfr.consumer.RecordingStream;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
Expand All @@ -71,6 +74,8 @@
import org.testng.ITestResult;
import org.testng.annotations.Test;

import static org.testng.Assert.fail;

/**
* TestNG extension to support Helidon CDI container in tests.
*/
Expand All @@ -90,6 +95,8 @@ public class HelidonTestNgListener implements IClassListener, ITestListener {
private List<AddExtension> classLevelExtensions = new ArrayList<>();
private List<AddBean> classLevelBeans = new ArrayList<>();
private ConfigMeta classLevelConfigMeta = new ConfigMeta();
private List<EventWrapper> jfrVTPinned;
private RecordingStream recordingStream;
private boolean classLevelDisableDiscovery = false;
private boolean resetPerTest;

Expand All @@ -100,6 +107,7 @@ public class HelidonTestNgListener implements IClassListener, ITestListener {

@Override
public void onBeforeClass(ITestClass iTestClass) {
startRecordingStream();

testClass = iTestClass.getRealClass();

Expand Down Expand Up @@ -162,6 +170,7 @@ public void onAfterClass(ITestClass testClass) {
releaseConfig();
stopContainer();
}
closeRecordingStream();
}

@Override
Expand Down Expand Up @@ -398,6 +407,27 @@ private void stopContainer() {
}
}

private void startRecordingStream() {
jfrVTPinned = new ArrayList<>();
recordingStream = new RecordingStream();
recordingStream.enable("jdk.VirtualThreadPinned").withStackTrace();
recordingStream.onEvent("jdk.VirtualThreadPinned", event -> {
jfrVTPinned.add(new EventWrapper(event));
});
recordingStream.startAsync();
}

private void closeRecordingStream() {
try {
// Flush ending events
recordingStream.stop();
if (!jfrVTPinned.isEmpty()) {
fail("Some pinned virtual threads were detected:\n" + jfrVTPinned);
}
} finally {
recordingStream.close();
}
}

@SuppressWarnings("unchecked")
private <T extends Annotation> T[] getAnnotations(Class<?> testClass, Class<T> annotClass) {
Expand Down Expand Up @@ -635,4 +665,29 @@ public Class<? extends Extension> value() {
}
}

private static class EventWrapper {

private final RecordedEvent recordedEvent;

private EventWrapper(RecordedEvent recordedEvent) {
this.recordedEvent = recordedEvent;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder(recordedEvent.toString());
if (recordedEvent.getStackTrace() != null) {
builder.append("full-stackTrace = [");
List<RecordedFrame> frames = recordedEvent.getStackTrace().getFrames();
for (RecordedFrame frame : frames) {
builder.append("\n\t").append(frame.getMethod().getType().getName())
.append("#").append(frame.getMethod().getName())
.append("(").append(frame.getLineNumber()).append(")");
}
builder.append("\n]");
}
return builder.toString();
}
}

}
1 change: 1 addition & 0 deletions microprofile/testing/testng/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
requires jakarta.cdi;
requires jakarta.inject;
requires jakarta.ws.rs;
requires jdk.jfr;
requires microprofile.config.api;
requires org.testng;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.tests.testing.junit5;

import io.helidon.microprofile.testing.junit5.HelidonTest;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@HelidonTest
class TestPinnedThread {

@Test
@Disabled("Enable to verify pinned threads fails")
void test() throws InterruptedException {
Thread.ofVirtual().start(() -> {
synchronized (this) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
}).join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.tests.testing.testng;

import io.helidon.microprofile.testing.testng.HelidonTest;

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

@HelidonTest
class TestPinnedThread {

@Test
@Ignore("Enable to verify pinned threads fails")
void test() throws InterruptedException {
Thread.ofVirtual().start(() -> {
synchronized (this) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
}).join();
}
}

0 comments on commit 3c71ff0

Please sign in to comment.