< prev index next > test/jdk/jdk/internal/misc/ThreadFlock/ThreadFlockTest.java
Print this page
latch.countDown();
}
}
}
- /**
- * Test wakeup is flock confined.
- */
- @ParameterizedTest
- @MethodSource("factories")
- void testWakeupConfined(ThreadFactory factory) throws Exception {
- try (var flock = ThreadFlock.open(null)) {
- // thread in flock
- testWakeupConfined(flock, task -> {
- Thread thread = factory.newThread(task);
- return flock.start(thread);
- });
-
- // thread not in flock
- testWakeupConfined(flock, task -> {
- Thread thread = factory.newThread(task);
- thread.start();
- return thread;
- });
- }
- }
-
- /**
- * Test that a thread created with the given factory cannot wakeup the
- * given flock.
- */
- private void testWakeupConfined(ThreadFlock flock,
- Function<Runnable, Thread> factory) throws Exception {
- var exception = new AtomicReference<Exception>();
- Thread thread = factory.apply(() -> {
- try {
- flock.wakeup();
- } catch (Exception e) {
- exception.set(e);
- }
- });
- thread.join();
- Throwable cause = exception.get();
- if (flock.containsThread(thread)) {
- assertNull(cause);
- } else {
- assertTrue(cause instanceof WrongThreadException);
- }
- }
-
/**
* Test close with no threads running.
*/
@Test
void testCloseWithNoThreads() {
assertTrue(Thread.interrupted()); // clear interrupt
}
assertNull(exception.get());
}
- /**
- * Test shutdown is confined to threads in the flock.
- */
- @ParameterizedTest
- @MethodSource("factories")
- void testShutdownConfined(ThreadFactory factory) throws Exception {
- try (var flock = ThreadFlock.open(null)) {
- // thread in flock
- testShutdownConfined(flock, task -> {
- Thread thread = factory.newThread(task);
- return flock.start(thread);
- });
-
- // thread in flock
- try (var flock2 = ThreadFlock.open(null)) {
- testShutdownConfined(flock, task -> {
- Thread thread = factory.newThread(task);
- return flock2.start(thread);
- });
- }
-
- // thread not contained in flock
- testShutdownConfined(flock, task -> {
- Thread thread = factory.newThread(task);
- thread.start();
- return thread;
- });
- }
- }
-
- /**
- * Test that a thread created with the given factory cannot shut down the
- * given flock.
- */
- private void testShutdownConfined(ThreadFlock flock,
- Function<Runnable, Thread> factory) throws Exception {
- var exception = new AtomicReference<Exception>();
- Thread thread = factory.apply(() -> {
- try {
- flock.shutdown();
- } catch (Exception e) {
- exception.set(e);
- }
- });
- thread.join();
- Throwable cause = exception.get();
- if (flock.containsThread(thread)) {
- assertNull(cause);
- } else {
- assertTrue(cause instanceof WrongThreadException);
- }
- }
-
/**
* Test that closing an enclosing thread flock closes a nested thread flocks.
*/
@Test
void testStructureViolation() {
< prev index next >