|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import jdk.test.lib.dcmd.CommandExecutor; |
| 25 | +import jdk.test.lib.dcmd.JMXExecutor; |
| 26 | +import jdk.test.lib.process.OutputAnalyzer; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.util.concurrent.CountDownLatch; |
| 30 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 31 | +import java.util.regex.Pattern; |
| 32 | + |
| 33 | +/* |
| 34 | + * @test |
| 35 | + * @summary Test of diagnostic command Thread.print with virtual threads |
| 36 | + * @requires vm.continuations |
| 37 | + * @library /test/lib |
| 38 | + * @run junit PrintMountedVirtualThread |
| 39 | + */ |
| 40 | +public class PrintMountedVirtualThread { |
| 41 | + |
| 42 | + public void run(CommandExecutor executor) throws InterruptedException { |
| 43 | + var shouldFinish = new AtomicBoolean(false); |
| 44 | + var started = new CountDownLatch(1); |
| 45 | + final Runnable runnable = new DummyRunnable(shouldFinish, started); |
| 46 | + try { |
| 47 | + Thread vthread = Thread.ofVirtual().name("Dummy Vthread").start(runnable); |
| 48 | + started.await(); |
| 49 | + /* Execute */ |
| 50 | + OutputAnalyzer output = executor.execute("Thread.print"); |
| 51 | + output.shouldMatch(".*at " + Pattern.quote(DummyRunnable.class.getName()) + "\\.run.*"); |
| 52 | + output.shouldMatch(".*at " + Pattern.quote(DummyRunnable.class.getName()) + "\\.compute.*"); |
| 53 | + output.shouldMatch("Mounted virtual thread " + "\"Dummy Vthread\"" + " #" + vthread.threadId()); |
| 54 | + |
| 55 | + } finally { |
| 56 | + shouldFinish.set(true); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void jmx() throws InterruptedException { |
| 62 | + run(new JMXExecutor()); |
| 63 | + } |
| 64 | + |
| 65 | + static class DummyRunnable implements Runnable { |
| 66 | + |
| 67 | + private final AtomicBoolean shouldFinish; |
| 68 | + private final CountDownLatch started; |
| 69 | + |
| 70 | + public DummyRunnable(AtomicBoolean shouldFinish, CountDownLatch started) { |
| 71 | + this.shouldFinish = shouldFinish; |
| 72 | + this.started = started; |
| 73 | + } |
| 74 | + |
| 75 | + public void run() { |
| 76 | + compute(); |
| 77 | + } |
| 78 | + |
| 79 | + void compute() { |
| 80 | + started.countDown(); |
| 81 | + while (!shouldFinish.get()) { |
| 82 | + Thread.onSpinWait(); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +} |
0 commit comments