2022-09-23 07:55:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, 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
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* @test
|
2022-09-27 11:43:59 +00:00
|
|
|
* @bug 8289610 8249627 8205132
|
|
|
|
* @summary Test that Thread stop/suspend/resume/countStackFrames throw UOE
|
|
|
|
* @run junit DegradedMethodsThrowUOE
|
2022-09-23 07:55:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.concurrent.locks.LockSupport;
|
2022-09-27 11:43:59 +00:00
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
2022-09-23 07:55:29 +00:00
|
|
|
|
2022-09-27 11:43:59 +00:00
|
|
|
class DegradedMethodsThrowUOE {
|
2022-09-23 07:55:29 +00:00
|
|
|
|
|
|
|
/**
|
2022-09-27 11:43:59 +00:00
|
|
|
* Returns a stream of operations on a Thread that should throw UOE.
|
2022-09-23 07:55:29 +00:00
|
|
|
*/
|
2022-09-27 11:43:59 +00:00
|
|
|
static Stream<Consumer<Thread>> ops() {
|
|
|
|
return Stream.<Consumer<Thread>>of(
|
|
|
|
Thread::stop,
|
|
|
|
Thread::suspend,
|
|
|
|
Thread::resume,
|
|
|
|
Thread::countStackFrames
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test degraded method on current thread.
|
|
|
|
*/
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("ops")
|
|
|
|
void testCurrentThread(Consumer<Thread> op) {
|
2022-09-23 07:55:29 +00:00
|
|
|
var thread = Thread.currentThread();
|
2022-09-27 11:43:59 +00:00
|
|
|
assertThrows(UnsupportedOperationException.class, () -> op.accept(thread));
|
2022-09-23 07:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-27 11:43:59 +00:00
|
|
|
* Test degraded method on an unstarted thread.
|
2022-09-23 07:55:29 +00:00
|
|
|
*/
|
2022-09-27 11:43:59 +00:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("ops")
|
|
|
|
void testUnstartedThread(Consumer<Thread> op) {
|
2022-09-23 07:55:29 +00:00
|
|
|
Thread thread = new Thread(() -> { });
|
2022-09-27 11:43:59 +00:00
|
|
|
assertThrows(UnsupportedOperationException.class, () -> op.accept(thread));
|
2022-09-23 07:55:29 +00:00
|
|
|
assertTrue(thread.getState() == Thread.State.NEW);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-27 11:43:59 +00:00
|
|
|
* Test degraded method on a thread spinning in a loop.
|
2022-09-23 07:55:29 +00:00
|
|
|
*/
|
2022-09-27 11:43:59 +00:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("ops")
|
|
|
|
void testRunnableThread(Consumer<Thread> op) throws Exception {
|
2022-09-23 07:55:29 +00:00
|
|
|
AtomicBoolean done = new AtomicBoolean();
|
|
|
|
Thread thread = new Thread(() -> {
|
|
|
|
while (!done.get()) {
|
|
|
|
Thread.onSpinWait();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
thread.start();
|
|
|
|
try {
|
2022-09-27 11:43:59 +00:00
|
|
|
assertThrows(UnsupportedOperationException.class, () -> op.accept(thread));
|
2022-09-23 07:55:29 +00:00
|
|
|
|
|
|
|
// thread should not terminate
|
|
|
|
boolean terminated = thread.join(Duration.ofMillis(500));
|
|
|
|
assertFalse(terminated);
|
|
|
|
} finally {
|
|
|
|
done.set(true);
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-27 11:43:59 +00:00
|
|
|
* Test degraded method on a thread that is parked.
|
2022-09-23 07:55:29 +00:00
|
|
|
*/
|
2022-09-27 11:43:59 +00:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("ops")
|
|
|
|
void testWaitingThread(Consumer<Thread> op) throws Exception {
|
2022-09-23 07:55:29 +00:00
|
|
|
Thread thread = new Thread(LockSupport::park);
|
|
|
|
thread.start();
|
|
|
|
try {
|
|
|
|
// wait for thread to park
|
|
|
|
while ((thread.getState() != Thread.State.WAITING)) {
|
|
|
|
Thread.sleep(10);
|
|
|
|
}
|
2022-09-27 11:43:59 +00:00
|
|
|
assertThrows(UnsupportedOperationException.class, () -> op.accept(thread));
|
2022-09-23 07:55:29 +00:00
|
|
|
assertTrue(thread.getState() == Thread.State.WAITING);
|
|
|
|
} finally {
|
|
|
|
LockSupport.unpark(thread);
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-27 11:43:59 +00:00
|
|
|
* Test degraded method on a terminated thread.
|
2022-09-23 07:55:29 +00:00
|
|
|
*/
|
2022-09-27 11:43:59 +00:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("ops")
|
|
|
|
void testTerminatedThread(Consumer<Thread> op) throws Exception {
|
2022-09-23 07:55:29 +00:00
|
|
|
Thread thread = new Thread(() -> { });
|
|
|
|
thread.start();
|
|
|
|
thread.join();
|
2022-09-27 11:43:59 +00:00
|
|
|
assertThrows(UnsupportedOperationException.class, () -> op.accept(thread));
|
2022-09-23 07:55:29 +00:00
|
|
|
assertTrue(thread.getState() == Thread.State.TERMINATED);
|
|
|
|
}
|
|
|
|
}
|