8366659: Refactored tests.

This commit is contained in:
Anton Artemov 2025-11-19 16:46:28 +01:00
parent 8c82d39c34
commit 08b481cdc0
5 changed files with 133 additions and 276 deletions

View File

@ -60,47 +60,9 @@ import java.io.PrintStream;
public class SuspendWithObjectMonitorWait1 extends SuspendWithObjectMonitorWaitBase {
public static void main(String[] args) throws Exception {
if (args.length > 2) {
System.err.println("Invalid number of arguments, there are too many arguments.");
usage();
}
try {
System.loadLibrary(AGENT_LIB);
log("Loaded library: " + AGENT_LIB);
} catch (UnsatisfiedLinkError ule) {
log("Failed to load library: " + AGENT_LIB);
log("java.library.path: " + System.getProperty("java.library.path"));
throw ule;
}
int timeMax = 0;
for (int argIndex = 0; argIndex < args.length; argIndex++) {
if ("-p".equals(args[argIndex])) {
// Handle optional -p arg regardless of position.
printDebug = true;
continue;
}
if (argIndex < args.length) {
// timeMax is an optional arg.
try {
timeMax = Integer.parseUnsignedInt(args[argIndex]);
} catch (NumberFormatException nfe) {
System.err.println("'" + args[argIndex] +
"': invalid time_max value.");
usage();
}
} else {
timeMax = DEF_TIME_MAX;
}
}
System.exit(run(timeMax, System.out) + exit_delta);
}
public static int run(int timeMax, PrintStream out) {
return (new SuspendWithObjectMonitorWait1()).doWork1(timeMax, out);
@Override
public int run(int timeMax, PrintStream out) {
return doWork1(timeMax, out);
}
// Default scenario, the resumer thread is always able to grab the threadLock once notified by the main thread.
@ -116,30 +78,10 @@ public class SuspendWithObjectMonitorWait1 extends SuspendWithObjectMonitorWaitB
testState = TS_INIT; // starting the test loop
// launch the waiter thread
synchronized (barrierLaunch) {
waiter = new SuspendWithObjectMonitorWaitWorker("waiter");
waiter.start();
while (testState != TS_WAITER_RUNNING) {
try {
barrierLaunch.wait(0); // wait until it is running
} catch (InterruptedException ex) {
}
}
}
waiter = launchWaiter(0);
// launch the resumer thread
synchronized (barrierLaunch) {
resumer = new SuspendWithObjectMonitorWaitWorker("resumer", waiter);
resumer.start();
while (testState != TS_RESUMER_RUNNING) {
try {
barrierLaunch.wait(0); // wait until it is running
} catch (InterruptedException ex) {
}
}
}
resumer = launchResumer(waiter);
checkTestState(TS_RESUMER_RUNNING);
@ -181,33 +123,9 @@ public class SuspendWithObjectMonitorWait1 extends SuspendWithObjectMonitorWaitB
// - resumption of the waiter thread
// - a threadLock enter in the freshly resumed waiter thread
//
barrierResumerNotify();
synchronized (barrierResumer) {
checkTestState(TS_CALL_SUSPEND);
// tell resumer thread to resume waiter thread
testState = TS_READY_TO_RESUME;
barrierResumer.notify();
// Can't call checkTestState() here because the
// resumer thread may have already resumed the
// waiter thread.
}
try {
resumer.join(JOIN_MAX * 1000);
if (resumer.isAlive()) {
System.err.println("Failure at " + count + " loops.");
throw new InternalError("resumer thread is stuck");
}
waiter.join(JOIN_MAX * 1000);
if (waiter.isAlive()) {
System.err.println("Failure at " + count + " loops.");
throw new InternalError("waiter thread is stuck");
}
} catch (InterruptedException ex) {
}
shutDown(waiter ,resumer);
checkTestState(TS_WAITER_DONE);
}

View File

@ -69,48 +69,9 @@ import java.io.PrintStream;
public class SuspendWithObjectMonitorWait2 extends SuspendWithObjectMonitorWaitBase {
public static void main(String[] args) throws Exception {
if (args.length > 2) {
System.err.println("Invalid number of arguments, there are too many arguments.");
usage();
}
try {
System.loadLibrary(AGENT_LIB);
log("Loaded library: " + AGENT_LIB);
} catch (UnsatisfiedLinkError ule) {
log("Failed to load library: " + AGENT_LIB);
log("java.library.path: " + System.getProperty("java.library.path"));
throw ule;
}
int timeMax = 0;
for (int argIndex = 0; argIndex < args.length; argIndex++) {
if ("-p".equals(args[argIndex])) {
// Handle optional -p arg regardless of position.
printDebug = true;
continue;
}
if (argIndex < args.length) {
// timeMax is an optional arg.
try {
timeMax = Integer.parseUnsignedInt(args[argIndex]);
} catch (NumberFormatException nfe) {
System.err.println("'" + args[argIndex] +
"': invalid time_max value.");
usage();
}
} else {
timeMax = DEF_TIME_MAX;
}
}
System.exit(run(timeMax, System.out) + exit_delta);
}
public static int run(int timeMax, PrintStream out) {
return (new SuspendWithObjectMonitorWait2()).doWork2(timeMax, out);
@Override
public int run(int timeMax, PrintStream out) {
return doWork2(timeMax, out);
}
// Notify the resumer while holding the threadLock.
@ -126,30 +87,10 @@ public class SuspendWithObjectMonitorWait2 extends SuspendWithObjectMonitorWaitB
testState = TS_INIT; // starting the test loop
// launch the waiter thread
synchronized (barrierLaunch) {
waiter = new SuspendWithObjectMonitorWaitWorker("waiter");
waiter.start();
while (testState != TS_WAITER_RUNNING) {
try {
barrierLaunch.wait(0); // wait until it is running
} catch (InterruptedException ex) {
}
}
}
waiter = launchWaiter(0);
// launch the resumer thread
synchronized (barrierLaunch) {
resumer = new SuspendWithObjectMonitorWaitWorker("resumer", waiter);
resumer.start();
while (testState != TS_RESUMER_RUNNING) {
try {
barrierLaunch.wait(0); // wait until it is running
} catch (InterruptedException ex) {
}
}
}
resumer = launchResumer(waiter);
checkTestState(TS_RESUMER_RUNNING);
@ -191,39 +132,15 @@ public class SuspendWithObjectMonitorWait2 extends SuspendWithObjectMonitorWaitB
// - resumption of the waiter thread
// - a threadLock enter in the freshly resumed waiter thread
//
synchronized (barrierResumer) {
checkTestState(TS_CALL_SUSPEND);
// tell resumer thread to resume waiter thread
testState = TS_READY_TO_RESUME;
barrierResumer.notify();
// Can't call checkTestState() here because the
// resumer thread may have already resumed the
// waiter thread.
}
barrierResumerNotify();
try {
// Delay for 1-second while holding the threadLock to force the
// resumer thread to block on entering the threadLock.
Thread.sleep(1000);
} catch(Exception e) {}
}
try {
resumer.join(JOIN_MAX * 1000);
if (resumer.isAlive()) {
System.err.println("Failure at " + count + " loops.");
throw new InternalError("resumer thread is stuck");
}
waiter.join(JOIN_MAX * 1000);
if (waiter.isAlive()) {
System.err.println("Failure at " + count + " loops.");
throw new InternalError("waiter thread is stuck");
}
} catch (InterruptedException ex) {
} catch (Exception e) {}
}
shutDown(waiter ,resumer);
checkTestState(TS_WAITER_DONE);
}

View File

@ -75,48 +75,9 @@ import java.io.PrintStream;
public class SuspendWithObjectMonitorWait3 extends SuspendWithObjectMonitorWaitBase {
public static void main(String[] args) throws Exception {
if (args.length > 2) {
System.err.println("Invalid number of arguments, there are too many arguments.");
usage();
}
try {
System.loadLibrary(AGENT_LIB);
log("Loaded library: " + AGENT_LIB);
} catch (UnsatisfiedLinkError ule) {
log("Failed to load library: " + AGENT_LIB);
log("java.library.path: " + System.getProperty("java.library.path"));
throw ule;
}
int timeMax = 0;
for (int argIndex = 0; argIndex < args.length; argIndex++) {
if ("-p".equals(args[argIndex])) {
// Handle optional -p arg regardless of position.
printDebug = true;
continue;
}
if (argIndex < args.length) {
// timeMax is an optional arg.
try {
timeMax = Integer.parseUnsignedInt(args[argIndex]);
} catch (NumberFormatException nfe) {
System.err.println("'" + args[argIndex] +
"': invalid time_max value.");
usage();
}
} else {
timeMax = DEF_TIME_MAX;
}
}
System.exit(run(timeMax, System.out) + exit_delta);
}
public static int run(int timeMax, PrintStream out) {
return (new SuspendWithObjectMonitorWait3()).doWork3(timeMax, out);
@Override
public int run(int timeMax, PrintStream out) {
return doWork3(timeMax, out);
}
// Suspend on the re-entry path of wait.
@ -132,30 +93,10 @@ public class SuspendWithObjectMonitorWait3 extends SuspendWithObjectMonitorWaitB
testState = TS_INIT; // starting the test loop
// launch the waiter thread
synchronized (barrierLaunch) {
waiter = new SuspendWithObjectMonitorWaitWorker("waiter", 100);
waiter.start();
while (testState != TS_WAITER_RUNNING) {
try {
barrierLaunch.wait(0); // wait until it is running
} catch (InterruptedException ex) {
}
}
}
waiter = launchWaiter(100);
// launch the resumer thread
synchronized (barrierLaunch) {
resumer = new SuspendWithObjectMonitorWaitWorker("resumer", waiter);
resumer.start();
while (testState != TS_RESUMER_RUNNING) {
try {
barrierLaunch.wait(0); // wait until it is running
} catch (InterruptedException ex) {
}
}
}
resumer = launchResumer(waiter);
checkTestState(TS_RESUMER_RUNNING);
@ -201,18 +142,7 @@ public class SuspendWithObjectMonitorWait3 extends SuspendWithObjectMonitorWaitB
// - resumption of the waiter thread
// - a threadLock enter in the freshly resumed waiter thread
//
synchronized (barrierResumer) {
checkTestState(TS_CALL_SUSPEND);
// tell resumer thread to resume waiter thread
testState = TS_READY_TO_RESUME;
barrierResumer.notify();
// Can't call checkTestState() here because the
// resumer thread may have already resumed the
// waiter thread.
}
barrierResumerNotify();
try {
// Delay for 1-second while holding the threadLock to force the
// resumer thread to block on entering the threadLock.
@ -220,20 +150,7 @@ public class SuspendWithObjectMonitorWait3 extends SuspendWithObjectMonitorWaitB
} catch (Exception e) {}
}
try {
resumer.join(JOIN_MAX * 1000);
if (resumer.isAlive()) {
System.err.println("Failure at " + count + " loops.");
throw new InternalError("resumer thread is stuck");
}
waiter.join(JOIN_MAX * 1000);
if (waiter.isAlive()) {
System.err.println("Failure at " + count + " loops.");
throw new InternalError("waiter thread is stuck");
}
} catch (InterruptedException ex) {
}
shutDown(waiter ,resumer);
checkTestState(TS_WAITER_DONE);
}

View File

@ -21,6 +21,8 @@
* questions.
*/
import java.io.PrintStream;
public class SuspendWithObjectMonitorWaitBase {
protected static final String AGENT_LIB = "SuspendWithObjectMonitorWait";
protected static final int exit_delta = 95;
@ -74,4 +76,112 @@ public class SuspendWithObjectMonitorWaitBase {
+ "expected=" + exp + " actual=" + testState);
}
}
public SuspendWithObjectMonitorWaitWorker launchWaiter(long waitTimeout) {
SuspendWithObjectMonitorWaitWorker waiter;
// launch the waiter thread
synchronized (barrierLaunch) {
waiter = new SuspendWithObjectMonitorWaitWorker("waiter", waitTimeout);
waiter.start();
while (testState != TS_WAITER_RUNNING) {
try {
barrierLaunch.wait(0); // wait until it is running
} catch (InterruptedException ex) {
}
}
}
return waiter;
}
public SuspendWithObjectMonitorWaitWorker launchResumer(SuspendWithObjectMonitorWaitWorker waiter) {
SuspendWithObjectMonitorWaitWorker resumer;
synchronized (barrierLaunch) {
resumer = new SuspendWithObjectMonitorWaitWorker("resumer", waiter);
resumer.start();
while (testState != TS_RESUMER_RUNNING) {
try {
barrierLaunch.wait(0); // wait until it is running
} catch (InterruptedException ex) {
}
}
}
return resumer;
}
public void barrierResumerNotify() {
synchronized (barrierResumer) {
checkTestState(TS_CALL_SUSPEND);
// tell resumer thread to resume waiter thread
testState = TS_READY_TO_RESUME;
barrierResumer.notify();
// Can't call checkTestState() here because the
// resumer thread may have already resumed the
// waiter thread.
}
}
public void shutDown(SuspendWithObjectMonitorWaitWorker resumer, SuspendWithObjectMonitorWaitWorker waiter) {
try {
resumer.join(JOIN_MAX * 1000);
if (resumer.isAlive()) {
System.err.println("Failure at " + count + " loops.");
throw new InternalError("resumer thread is stuck");
}
waiter.join(JOIN_MAX * 1000);
if (waiter.isAlive()) {
System.err.println("Failure at " + count + " loops.");
throw new InternalError("waiter thread is stuck");
}
} catch (InterruptedException ex) {
}
}
public int run(int timeMax, PrintStream out) {
return 0;
}
public static void main(String[] args) throws Exception {
if (args.length > 2) {
System.err.println("Invalid number of arguments, there are too many arguments.");
usage();
}
try {
System.loadLibrary(AGENT_LIB);
log("Loaded library: " + AGENT_LIB);
} catch (UnsatisfiedLinkError ule) {
log("Failed to load library: " + AGENT_LIB);
log("java.library.path: " + System.getProperty("java.library.path"));
throw ule;
}
int timeMax = 0;
for (int argIndex = 0; argIndex < args.length; argIndex++) {
if ("-p".equals(args[argIndex])) {
// Handle optional -p arg regardless of position.
printDebug = true;
continue;
}
if (argIndex < args.length) {
// timeMax is an optional arg.
try {
timeMax = Integer.parseUnsignedInt(args[argIndex]);
} catch (NumberFormatException nfe) {
System.err.println("'" + args[argIndex] +
"': invalid time_max value.");
usage();
}
} else {
timeMax = DEF_TIME_MAX;
}
}
SuspendWithObjectMonitorWaitBase test = new SuspendWithObjectMonitorWaitBase();
int result = test.run(timeMax, System.out);
System.exit(result + exit_delta);
}
}

View File

@ -21,15 +21,10 @@
* questions.
*/
class SuspendWithObjectMonitorWaitWorker extends Thread {
public class SuspendWithObjectMonitorWaitWorker extends Thread {
private SuspendWithObjectMonitorWaitWorker target; // target for resume operation
private final long waitTimeout;
public SuspendWithObjectMonitorWaitWorker(String name) {
super(name);
this.waitTimeout = 0;
}
public SuspendWithObjectMonitorWaitWorker(String name, long waitTimeout) {
super(name);
this.waitTimeout = waitTimeout;