< prev index next >
test/hotspot/jtreg/runtime/clinit/ClassInitBarrier.java
Print this page
@@ -138,34 +138,42 @@
}
}
static void run() {
execute(ExceptionInInitializerError.class, () -> triggerInitialization(A.class));
-
ensureFinished();
+ runTests(); // after initialization is over
}
}
// ============================================================================================================== //
static void execute(Class<? extends Throwable> expectedExceptionClass, Runnable action) {
try {
action.run();
- if (THROW) throw new AssertionError("no exception thrown");
+ if (THROW) throw failure("no exception thrown");
} catch (Throwable e) {
if (THROW) {
if (e.getClass() == expectedExceptionClass) {
// expected
} else {
String msg = String.format("unexpected exception thrown: expected %s, caught %s",
expectedExceptionClass.getName(), e.getClass().getName());
- throw new AssertionError(msg, e);
+ throw failure(msg, e);
}
} else {
- throw new AssertionError("no exception expected", e);
+ throw failure("no exception expected", e);
+ }
+ }
}
+
+ private static AssertionError failure(String msg) {
+ return new AssertionError(phase + ": " + msg);
}
+
+ private static AssertionError failure(String msg, Throwable e) {
+ return new AssertionError(phase + ": " + msg, e);
}
static final List<Thread> BLOCKED_THREADS = Collections.synchronizedList(new ArrayList<>());
static final Consumer<Thread> ON_BLOCK = BLOCKED_THREADS::add;
@@ -291,31 +299,57 @@
V get();
}
static final AtomicInteger NON_BLOCKING_COUNTER = new AtomicInteger(0);
static final AtomicInteger NON_BLOCKING_ACTIONS = new AtomicInteger(0);
- static final Factory<Runnable> NON_BLOCKING = () -> disposableAction(Phase.IN_PROGRESS, NON_BLOCKING_COUNTER, NON_BLOCKING_ACTIONS);
+ static final Factory<Runnable> NON_BLOCKING = () -> disposableAction(phase, NON_BLOCKING_COUNTER, NON_BLOCKING_ACTIONS);
static final AtomicInteger BLOCKING_COUNTER = new AtomicInteger(0);
static final AtomicInteger BLOCKING_ACTIONS = new AtomicInteger(0);
static final Factory<Runnable> BLOCKING = () -> disposableAction(Phase.FINISHED, BLOCKING_COUNTER, BLOCKING_ACTIONS);
static void checkBlockingAction(TestCase0 r) {
- r.run(NON_BLOCKING.get()); // same thread
+ switch (phase) {
+ case IN_PROGRESS: {
+ // Barrier during class initalization.
+ r.run(NON_BLOCKING.get()); // initializing thread
checkBlocked(ON_BLOCK, ON_FAILURE, r); // different thread
+ break;
+ }
+ case FINISHED: {
+ // No barrier after class initalization is over.
+ r.run(NON_BLOCKING.get()); // initializing thread
+ checkNotBlocked(r); // different thread
+ break;
+ }
+ case INIT_FAILURE: {
+ // Exception is thrown after class initialization failed.
+ TestCase0 test = action -> execute(NoClassDefFoundError.class, () -> r.run(action));
+
+ test.run(NON_BLOCKING.get()); // initializing thread
+ checkNotBlocked(test); // different thread
+ break;
+ }
+ default: throw new Error("wrong phase: " + phase);
+ }
}
static void checkNonBlockingAction(TestCase0 r) {
- r.run(NON_BLOCKING.get());
+ r.run(NON_BLOCKING.get()); // initializing thread
checkNotBlocked(r); // different thread
}
static <T> void checkNonBlockingAction(T recv, TestCase1<T> r) {
- r.run(recv, NON_BLOCKING.get()); // same thread
+ r.run(recv, NON_BLOCKING.get()); // initializing thread
checkNotBlocked((action) -> r.run(recv, action)); // different thread
}
+ static void checkFailingAction(TestCase0 r) {
+ r.run(NON_BLOCKING.get()); // initializing thread
+ checkNotBlocked(r); // different thread
+ }
+
static void triggerInitialization(Class<?> cls) {
try {
Class<?> loadedClass = Class.forName(cls.getName(), true, cls.getClassLoader());
if (loadedClass != cls) {
throw new Error("wrong class");
@@ -354,11 +388,19 @@
throw new Error(e);
}
}
static void checkNotBlocked(TestCase0 r) {
- Thread thr = new Thread(() -> r.run(NON_BLOCKING.get()));
+ final Thread thr = new Thread(() -> r.run(NON_BLOCKING.get()));
+ final Throwable[] ex = new Throwable[1];
+ thr.setUncaughtExceptionHandler((t, e) -> {
+ if (thr != t) {
+ ex[0] = new Error("wrong thread: " + thr + " vs " + t);
+ } else {
+ ex[0] = e;
+ }
+ });
thr.start();
try {
thr.join(15_000);
if (thr.isAlive()) {
@@ -366,10 +408,14 @@
throw new AssertionError("blocked");
}
} catch (InterruptedException e) {
throw new Error(e);
}
+
+ if (ex[0] != null) {
+ throw new AssertionError("no exception expected", ex[0]);
+ }
}
static void maybeThrow() {
if (THROW) {
changePhase(Phase.INIT_FAILURE);
< prev index next >