https://github.com/cescoffier/loom-unit
A Junit5 Extension to check if virtual threads are pinning the carrier thread
https://github.com/cescoffier/loom-unit
Last synced: 28 days ago
JSON representation
A Junit5 Extension to check if virtual threads are pinning the carrier thread
- Host: GitHub
- URL: https://github.com/cescoffier/loom-unit
- Owner: cescoffier
- Created: 2022-10-05T11:49:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-13T12:13:33.000Z (about 1 year ago)
- Last Synced: 2025-02-28T03:22:27.622Z (about 2 months ago)
- Language: Java
- Size: 39.1 KB
- Stars: 38
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-java - Loom-Unit
README
# Loom-Unit
A Junit 5 extension capturing weather a virtual threads _pins_ the carrier thread during the execution of the test.
 
## Usage
1. Add the following dependency to your project:
```xml
me.escoffier.loom
loom-unit
VERSION
test```
**IMPORTANT**: You need to use Java 19+.
2. Extends your test class with the `me.escoffier.loom.loomunit.LoomUnitExtension` extension:
```java
@ExtendWith(LoomUnitExtension.class)
public class LoomUnitExampleTest {
// ...
}
```3. Use the `me.escoffier.loom.loomunit.ShouldNotPin` or `me.escoffier.loom.loomunit.ShouldPin` annotation on your test.
## Complete example
```java
package me.escoffier.loom.loomunit.snippets;
import me.escoffier.loom.loomunit.LoomUnitExtension;
import me.escoffier.loom.loomunit.ThreadPinnedEvents;
import me.escoffier.loom.loomunit.ShouldNotPin;
import me.escoffier.loom.loomunit.ShouldPin;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;import static org.awaitility.Awaitility.await;
@ExtendWith(LoomUnitExtension.class) // Use the extension
public class LoomUnitExampleTest {CodeUnderTest codeUnderTest = new CodeUnderTest();
@Test
@ShouldNotPin
public void testThatShouldNotPin() {
// ...
}@Test
@ShouldPin(atMost = 1)
public void testThatShouldPinAtMostOnce() {
codeUnderTest.pin();
}@Test
public void testThatShouldNotPin(ThreadPinnedEvents events) { // Inject an object to check the pin events
Assertions.assertTrue(events.getEvents().isEmpty());
codeUnderTest.pin();
await().until(() -> events.getEvents().size() > 0);
Assertions.assertEquals(events.getEvents().size(), 1);
}}
```You can also use the `@ShouldPin` and `@ShouldNotPin` annotations on the class:
```java
@ExtendWith(LoomUnitExtension.class) // Use the extension
@ShouldNotPin // You can use @ShouldNotPin or @ShouldPin on the class itself, it's applied to each method.
public class LoomUnitExampleOnClassTest {CodeUnderTest codeUnderTest = new CodeUnderTest();
@Test
public void testThatShouldNotPin() {
// ...
}@Test
@ShouldPin(atMost = 1) // Method annotation overrides the class annotation
public void testThatShouldPinAtMostOnce() {
codeUnderTest.pin();
}}
```