1 /*
2  * Copyright (c) 2018, 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 /**
25  * @test
26  * @summary Verifies NotifyFramePop request is cleared if JVMTI_EVENT_FRAME_POP is disabled
27  * @library /test/lib
28  * @compile NotifyFramePopTest.java
29  * @run main/othervm/native -agentlib:NotifyFramePopTest NotifyFramePopTest
30  */
31 
32 import jtreg.SkippedException;
33 
34 public class NotifyFramePopTest {
35     static {
36         try {
37             System.loadLibrary("NotifyFramePopTest");
38         } catch (UnsatisfiedLinkError ex) {
39             System.err.println("Could not load NotifyFramePopTest library");
40             System.err.println("java.library.path:"
41                 + System.getProperty("java.library.path"));
42             throw ex;
43         }
44     }
45 
main(String args[])46     public static void main(String args[]) {
47         if (!canGenerateFramePopEvents()) {
48             throw new SkippedException("FramePop event is not supported");
49         }
50 
51         // Sanity testing that FRAME_POP works.
52         test("sanity", true, () -> {
53             setFramePopNotificationMode(true);
54             notifyFramePop(null);
55         });
56 
57         // Request notification and then disable FRAME_POP event notification.
58         // This should not prevent the notification for the frame being cleared
59         // when we return from the method.
60         test("requestAndDisable", false, () -> {
61             setFramePopNotificationMode(true);
62             notifyFramePop(null);
63             setFramePopNotificationMode(false);
64         });
65 
66         // Ensure there is no pending event
67         test("ensureCleared", false, () -> {
68             setFramePopNotificationMode(true);
69         });
70 
71         log("Test PASSED");
72     }
73 
canGenerateFramePopEvents()74     private native static boolean canGenerateFramePopEvents();
setFramePopNotificationMode(boolean enabled)75     private native static void setFramePopNotificationMode(boolean enabled);
notifyFramePop(Thread thread)76     private native static void notifyFramePop(Thread thread);
framePopReceived()77     private native static boolean framePopReceived();
78 
log(String msg)79     private static void log(String msg) {
80         System.out.println(msg);
81     }
82 
83     private interface Test {
test()84         void test();
85     }
86 
test(String name, boolean framePopExpected, Test theTest)87     private static void test(String name, boolean framePopExpected, Test theTest) {
88         log("test: " + name);
89         theTest.test();
90         boolean actual = framePopReceived();
91         if (framePopExpected != actual) {
92             throw new RuntimeException("unexpected notification:"
93                     + " FramePop expected: " + (framePopExpected ? "yes" : "no")
94                     + ", actually received: " + (actual ? "yes" : "no"));
95         }
96         log("  - OK (" + (actual ? "received" : "NOT received") + ")");
97     }
98 
99 }
100