-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
- Loading branch information
Showing
12 changed files
with
474 additions
and
2 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...a/io/helidon/microprofile/testing/junit5/HelidonPinnedThreadValidationJunitExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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.junit5; | ||
|
||
import jdk.jfr.consumer.RecordedEvent; | ||
import jdk.jfr.consumer.RecordingStream; | ||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
/** | ||
* JUnit5 extension to support pinned threads validation. | ||
*/ | ||
class HelidonPinnedThreadValidationJunitExtension implements BeforeAllCallback, AfterAllCallback { | ||
|
||
private RecordingStream recordingStream; | ||
private boolean pinnedThreadValidation; | ||
private PinningException pinningException; | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) throws Exception { | ||
Class<?> testClass = context.getRequiredTestClass(); | ||
pinnedThreadValidation = testClass.getAnnotation(PinnedThreadValidation.class) != null; | ||
if (pinnedThreadValidation) { | ||
recordingStream = new RecordingStream(); | ||
recordingStream.enable("jdk.VirtualThreadPinned").withStackTrace(); | ||
recordingStream.onEvent("jdk.VirtualThreadPinned", this::record); | ||
recordingStream.startAsync(); | ||
} | ||
} | ||
|
||
void record(RecordedEvent event) { | ||
PinningException e = new PinningException(event); | ||
if (pinningException == null) { | ||
pinningException = e; | ||
} else { | ||
pinningException.addSuppressed(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterAll(ExtensionContext context) { | ||
if (pinnedThreadValidation) { | ||
try { | ||
// Flush ending events | ||
recordingStream.stop(); | ||
if (pinningException != null) { | ||
throw pinningException; | ||
} | ||
} finally { | ||
recordingStream.close(); | ||
} | ||
} | ||
} | ||
|
||
private static 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(); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...g/junit5/src/main/java/io/helidon/microprofile/testing/junit5/PinnedThreadValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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.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. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@ExtendWith(HelidonPinnedThreadValidationJunitExtension.class) | ||
@Inherited | ||
public @interface PinnedThreadValidation { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...g/testng/src/main/java/io/helidon/microprofile/testing/testng/PinnedThreadValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.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. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@Inherited | ||
public @interface PinnedThreadValidation { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...g/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestPinnedThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.PinnedThreadValidation; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@PinnedThreadValidation | ||
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(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...g/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestPinnedThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.PinnedThreadValidation; | ||
|
||
import org.testng.annotations.Ignore; | ||
import org.testng.annotations.Test; | ||
|
||
@PinnedThreadValidation | ||
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(); | ||
} | ||
} |
Oops, something went wrong.