Skip to content

Commit

Permalink
Merge pull request #19945 from keithc-ca/yield
Browse files Browse the repository at this point in the history
Update number of frames for test_YieldedVirtualThreadGetStackTrace
  • Loading branch information
llxia authored Aug 1, 2024
2 parents 0b32a25 + 6c5f2b3 commit 5b6168a
Showing 1 changed file with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,27 @@
*/
package org.openj9.test.jep425;

import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.AssertJUnit;
import static org.testng.Assert.fail;
import org.testng.annotations.Test;

import java.lang.reflect.*;
import java.lang.Thread;
import java.time.Duration;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.IntStream;

import org.openj9.test.util.VersionCheck;

/**
* Test cases for JEP 425: Virtual Threads (Preview) Continuation execution
* which verifies the basic cases including Continuation enter, yield, resume.
*/
@Test(groups = { "level.sanity" })
public class VirtualThreadTests {

static {
try {
System.loadLibrary("j9ben");
Expand All @@ -63,7 +64,9 @@ private void incrementalWait(Thread t) throws InterruptedException {

@Test
public void test_basicVirtualthread() {
var wrapper = new Object(){ boolean executed = false; };
var wrapper = new Object() {
boolean executed = false;
};
try {
Thread t = Thread.ofVirtual().name("duke").unstarted(() -> {
wrapper.executed = true;
Expand All @@ -87,14 +90,14 @@ public void test_VirtualthreadYieldResume() {
int[] results = new int[numThreads];

IntStream.range(0, numThreads).forEach(i -> {
executor.submit(() -> {
results[i] = 1;
Thread.sleep(Duration.ofSeconds(1));
results[i] += 1;
Thread.sleep(Duration.ofSeconds(1));
results[i] += 1;
return i;
});
executor.submit(() -> {
results[i] = 1;
Thread.sleep(Duration.ofSeconds(1));
results[i] += 1;
Thread.sleep(Duration.ofSeconds(1));
results[i] += 1;
return i;
});
});

/* Wait incrementally for the worst-case scenario where all virtual threads are
Expand Down Expand Up @@ -161,29 +164,31 @@ public void test_jniFromVirtualthread() {
while (!testJNIThreadReady) {
Thread.sleep(10);
}

/* Incrementally wait for 10000 ms to let the virtual thread park. */
incrementalWait(t);
Assert.assertEquals(t.getState(), Thread.State.WAITING);
LockSupport.unpark(t);
t.join();
} catch (Exception e) {
Assert.fail("Unexpected exception occured : " + e.getMessage() , e);
Assert.fail("Unexpected exception occured : " + e.getMessage(), e);
}
}

private static volatile boolean testThread1Ready = false;

@Test
public void test_YieldedVirtualThreadGetStackTrace() {
/* The expected frame count is based on test's callstack */
int expectedFrames = 6;
/* The expected frame count is based on test's callstack. */
int expectedFrames = (VersionCheck.major() >= 24) ? 5 : 6;
String expectedMethodName = "park";

try {
Thread t = Thread.ofVirtual().name("yielded-stackwalker").start(() -> {
testThread1Ready = true;
LockSupport.park();
});
testThread1Ready = true;
LockSupport.park();
});

while (!testThread1Ready) {
Thread.sleep(10);
}
Expand Down Expand Up @@ -227,9 +232,12 @@ public void test_RunningVirtualThreadGetStackTrace() {
String expectedClassName = "org.openj9.test.jep425.VirtualThreadTests";

Thread t = Thread.ofVirtual().name("running-stackwalker").start(() -> {
testThread2_state = true;
while (testThread2_state);
});
testThread2_state = true;
while (testThread2_state) {
// busy wait
}
});

while (!testThread2_state) {
Thread.sleep(10);
}
Expand Down Expand Up @@ -353,7 +361,7 @@ public void test_verifyJVMTIMacros() {
Assert.fail("JVMTI_VTHREAD_STATE_SUSPENDED (" + JVMTI_VTHREAD_STATE_SUSPENDED + ") does not match VirtualThread.SUSPENDED (" + value + ")");
}
} catch (Exception e) {
Assert.fail("Unexpected exception occured : " + e.getMessage() , e);
Assert.fail("Unexpected exception occured : " + e.getMessage(), e);
}
}
}

0 comments on commit 5b6168a

Please sign in to comment.