From f5d44383105be74323c1db8f547c159fa71563e2 Mon Sep 17 00:00:00 2001 From: Kyle Aure Date: Thu, 8 Feb 2024 10:10:58 -0600 Subject: [PATCH] Add test for forkjoinpool --- .../Platform/virtual/VirtualFullTests.java | 6 ++++ .../virtual/VirtualThreadServlet.java | 32 +++++++++++++++++++ .../Platform/virtual/VirtualWebTests.java | 6 ++++ 3 files changed, 44 insertions(+) diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualFullTests.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualFullTests.java index 6ebb2f92..56b94626 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualFullTests.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualFullTests.java @@ -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); + } } diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualThreadServlet.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualThreadServlet.java index 2e7dab80..61431699 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualThreadServlet.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualThreadServlet.java @@ -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; @@ -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 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. diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualWebTests.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualWebTests.java index b8c534b0..371f8448 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualWebTests.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/Platform/virtual/VirtualWebTests.java @@ -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); + } }