8373293: Change the exception handling in TestNestHostErrorWithMultiThread.java

Reviewed-by: jpai, iklam
This commit is contained in:
David Holmes 2025-12-09 05:15:47 +00:00
parent 35fe0b1101
commit 020e3f9591

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -40,14 +41,12 @@ import java.util.concurrent.CountDownLatch;
public class TestNestHostErrorWithMultiThread {
public static void main(String args[]) {
public static void main(String args[]) throws Throwable {
CountDownLatch runLatch = new CountDownLatch(1);
CountDownLatch startLatch = new CountDownLatch(2);
Runnable test = new Test(runLatch, startLatch);
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
TestThread t1 = new TestThread(runLatch, startLatch);
TestThread t2 = new TestThread(runLatch, startLatch);
t1.start();
t2.start();
@ -59,39 +58,65 @@ public class TestNestHostErrorWithMultiThread {
t1.join();
t2.join();
Throwable threadException = t1.exception() != null ? t1.exception()
: t2.exception();
if (threadException != null) {
Throwable t = threadException;
try {
throw new Error("TestThread encountered unexpected exception", t);
}
catch (OutOfMemoryError oome) {
// If we encounter an OOME trying to create the wrapper Error,
// then just re-throw the original exception so we report it and
// not the secondary OOME.
throw t;
}
}
} catch (InterruptedException e) {
throw new Error("Unexpected interrupt");
}
}
static class Test implements Runnable {
static class TestThread extends Thread {
private CountDownLatch runLatch;
private CountDownLatch startLatch;
private Throwable exception;
Test(CountDownLatch runLatch, CountDownLatch startLatch) {
Throwable exception() {
return exception;
}
TestThread(CountDownLatch runLatch, CountDownLatch startLatch) {
this.runLatch = runLatch;
this.startLatch = startLatch;
}
@Override
public void run() {
// Don't allow any exceptions to escape - the main thread will
// report them.
try {
startLatch.countDown();
// Try to have all threads trigger the nesthost check at the same time
runLatch.await();
HostNoNestMember h = new HostNoNestMember();
h.test();
throw new Error("IllegalAccessError was not thrown as expected");
} catch (IllegalAccessError expected) {
String msg = "current type is not listed as a nest member";
if (!expected.getMessage().contains(msg)) {
throw new Error("Wrong " + expected.getClass().getSimpleName() +": \"" +
expected.getMessage() + "\" does not contain \"" +
msg + "\"", expected);
try {
startLatch.countDown();
// Try to have all threads trigger the nesthost check at the same time
runLatch.await();
HostNoNestMember h = new HostNoNestMember();
h.test();
throw new Error("IllegalAccessError was not thrown as expected");
} catch (IllegalAccessError expected) {
String msg = "current type is not listed as a nest member";
if (!expected.getMessage().contains(msg)) {
throw new Error("Wrong " + expected.getClass().getSimpleName() +": \"" +
expected.getMessage() + "\" does not contain \"" +
msg + "\"", expected);
}
System.out.println("OK - got expected exception: " + expected);
} catch (InterruptedException e) {
throw new Error("Unexpected interrupt", e);
}
System.out.println("OK - got expected exception: " + expected);
} catch (InterruptedException e) {
throw new Error("Unexpected interrupt");
} catch (Throwable t) {
exception = t;
}
}
}