Skip to content

Commit

Permalink
Add test for forkjoinpool
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAure committed Feb 20, 2024
1 parent 2afc648 commit f5d4438
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Expand Up @@ -92,4 +92,10 @@ public void testPlatformThreadFactory() {
public void testVirtualThreadFactory() {
runTest(baseURL, testname);
}

@Assertion(id = "GIT:414", strategy = "Tests that a ForkJoinPool created from a thread factory with virtual = true"
+ " or virtual = false always uses a platform thread which is a restriction of Java.")
public void testVirtualThreadFactoryForkJoinPool() {
runTest(baseURL, testname);
}
}
Expand Up @@ -28,6 +28,7 @@
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -287,6 +288,37 @@ public void testVirtualThreadFactory() throws Exception {
if (result instanceof Throwable)
throw new AssertionError("An error occured on thread.", (Throwable) result);
}

public void testVirtualThreadFactoryForkJoinPool() throws Exception {
ManagedThreadFactory virtualThreadFactoryAnno = InitialContext
.doLookup("java:app/concurrent/ThreadFactoryAnnoVirtual");
ManagedThreadFactory platformThreadFactoryAnno = InitialContext
.doLookup("java:app/concurrent/ThreadFactoryAnnoPlatform");

//Test virtual thread factory
Thread thread1;
ForkJoinPool virtualPool = new ForkJoinPool(3, virtualThreadFactoryAnno, null, false);

try {
thread1 = virtualPool.submit(Thread::currentThread).get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS);
} finally {
virtualPool.shutdown();
}

assertFalse(isVirtual(thread1));

//Test platform thread factory
Future<Thread> thread2;
ForkJoinPool platformPool = new ForkJoinPool(3, platformThreadFactoryAnno, null, false);

try {
thread2 = platformPool.submit(Thread::currentThread);
} finally {
platformPool.shutdown();
}

assertFalse(isVirtual(thread2.get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS)));
}

/**
* Uses reflection to call method isVirtual on on the supplied thread.
Expand Down
Expand Up @@ -86,4 +86,10 @@ public void testPlatformThreadFactory() {
public void testVirtualThreadFactory() {
runTest(baseURL, testname);
}

@Assertion(id = "GIT:414", strategy = "Tests that a ForkJoinPool created from a thread factory with virtual = true"
+ " can return and use a virtual thread, or returns a platform thread if it is not capable of providing virtual threads.")
public void testVirtualThreadFactoryForkJoinPool() {
runTest(baseURL, testname);
}
}

0 comments on commit f5d4438

Please sign in to comment.