Skip to content

Commit

Permalink
Merge pull request #466 from KyleAure/fix-virtual-tests-java-17
Browse files Browse the repository at this point in the history
Fix virtual tests for Java 17
  • Loading branch information
KyleAure authored Mar 26, 2024
2 parents 1b8ef01 + bd333d4 commit 2fcec24
Showing 1 changed file with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ public void testPlatformExecutor() throws Exception {

assertNotNull(platformManagedExecutorAnno);
assertNotNull(platformManagedExecutorDD);

Thread annoThread = platformManagedExecutorAnno.submit(Thread::currentThread).get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS);
Thread ddThread = platformManagedExecutorDD.submit(Thread::currentThread).get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS);

if (VERSION == 17) { //TODO remove when Concurrency API supports only 21+
assertThrows(NoSuchMethodException.class, () -> isVirtual(annoThread), "Should be impossible to get a virtual thread on Java 17");
assertThrows(NoSuchMethodException.class, () -> isVirtual(ddThread), "Should be impossible to get a virtual thread on Java 17");
return;
}

Future<Thread> future;

future = platformManagedExecutorAnno.submit(Thread::currentThread);
assertFalse(isVirtual(future.get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS)));

future = platformManagedExecutorDD.submit(Thread::currentThread);
assertFalse(isVirtual(future.get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS)));
assertFalse(isVirtual(annoThread));
assertFalse(isVirtual(ddThread));
}

public void testVirtualExecutor() throws Exception {
Expand Down Expand Up @@ -178,13 +182,17 @@ public void testPlatformScheduledExecutor() throws Exception {
assertNotNull(platformManagedScheduledExecutorAnno);
assertNotNull(platformManagedScheduledExecutorDD);

Future<Thread> future;

future = platformManagedScheduledExecutorAnno.submit(Thread::currentThread);
assertFalse(isVirtual(future.get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS)));
Thread annoThread = platformManagedScheduledExecutorAnno.submit(Thread::currentThread).get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS);
Thread ddThread = platformManagedScheduledExecutorDD.submit(Thread::currentThread).get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS);

if (VERSION == 17) { //TODO remove when Concurrency API supports only 21+
assertThrows(NoSuchMethodException.class, () -> isVirtual(annoThread), "Should be impossible to get a virtual thread on Java 17");
assertThrows(NoSuchMethodException.class, () -> isVirtual(ddThread), "Should be impossible to get a virtual thread on Java 17");
return;
}

future = platformManagedScheduledExecutorDD.submit(Thread::currentThread);
assertFalse(isVirtual(future.get(TestConstants.waitTimeout.toMillis(), TimeUnit.MILLISECONDS)));
assertFalse(isVirtual(annoThread));
assertFalse(isVirtual(ddThread));
}

public void testVirtualScheduledExecutor() throws Exception {
Expand Down Expand Up @@ -293,17 +301,26 @@ public void testVirtualScheduledExecutor() throws Exception {
}

public void testPlatformThreadFactory() throws Exception {
ManagedThreadFactory platfromThreadFactoryAnno = InitialContext
ManagedThreadFactory platformThreadFactoryAnno = InitialContext
.doLookup("java:app/concurrent/ThreadFactoryAnnoPlatform");
ManagedThreadFactory platfromThreadFactoryDD = InitialContext
ManagedThreadFactory platformThreadFactoryDD = InitialContext
.doLookup("java:app/concurrent/ThreadFactoryDDPlatform");

assertNotNull(platfromThreadFactoryAnno);
assertNotNull(platfromThreadFactoryDD);
assertNotNull(platformThreadFactoryAnno);
assertNotNull(platformThreadFactoryDD);

Thread annoThread = platformThreadFactoryAnno.newThread(NOOP_RUNNABLE);
Thread ddThread = platformThreadFactoryDD.newThread(NOOP_RUNNABLE);

if (VERSION == 17) { //TODO remove when Concurrency API supports only 21+
assertThrows(NoSuchMethodException.class, () -> isVirtual(annoThread), "Should be impossible to get a virtual thread on Java 17");
assertThrows(NoSuchMethodException.class, () -> isVirtual(ddThread), "Should be impossible to get a virtual thread on Java 17");
return;
}

assertFalse(isVirtual(platfromThreadFactoryAnno.newThread(NOOP_RUNNABLE)),
assertFalse(isVirtual(annoThread),
"Thread Factory should not have returned a virtual thread when defined with virtual=false");
assertFalse(isVirtual(platfromThreadFactoryDD.newThread(NOOP_RUNNABLE)),
assertFalse(isVirtual(ddThread),
"Thread Factory should not have returned a virtual thread when defined with virtual=false");
}

Expand Down

0 comments on commit 2fcec24

Please sign in to comment.