1 /*
   2  * Copyright (c) 2005, 2015, 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  * @bug 4401399
  27  * @summary Simple basic test of jdi Monitor request and event.
  28  * @author Swamy Venkataramanappa
  29  *
  30  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  31  * @run compile -g MonitorEventTest.java
  32  * @run driver MonitorEventTest
  33  */
  34 import com.sun.jdi.*;
  35 import com.sun.jdi.event.*;
  36 import com.sun.jdi.request.*;
  37 
  38 import java.util.*;
  39 
  40 /********** target program **********/
  41 
  42 class MonitorTestTarg {
  43     public static Object endingMonitor;
  44     public static Object startingMonitor;
  45     public static final long timeout = 30 * 6000; // milliseconds
  46 
  47     public static volatile boolean aboutEnterLock;
  48 
  49     static void foo() {
  50         System.out.println("Howdy!");
  51     }
  52 
  53     public static void main(String[] args){
  54         endingMonitor = new Object();
  55         startingMonitor = new Object();
  56 
  57         myThread t1 = new myThread();
  58         foo();
  59         aboutEnterLock = false;
  60 
  61         synchronized(endingMonitor) {
  62 
  63             // run thread
  64             try {
  65                 // start thread
  66                 synchronized (startingMonitor) {
  67                     t1.start();
  68                     startingMonitor.wait(timeout);
  69                 }
  70             } catch (InterruptedException e) {
  71                 System.out.println("Interrupted exception " + e);
  72             }
  73 
  74             Thread.yield();
  75             while(!(aboutEnterLock && t1.getState() == Thread.State.BLOCKED)) {
  76                 try {
  77                     Thread.sleep(1000);
  78                 }catch(Exception x){
  79                     System.out.println(x);
  80                 }
  81             }
  82         }
  83         try {
  84             t1.join(timeout);
  85         } catch (Exception x){
  86             System.out.println("Exception while thread.join :" + x);
  87         }
  88         System.out.println("Test exiting");
  89     }
  90 }
  91 
  92 class myThread extends Thread {
  93     public void run() {
  94         synchronized(MonitorTestTarg.startingMonitor) {
  95             MonitorTestTarg.startingMonitor.notify();
  96         }
  97 
  98         // contended enter wait until main thread release monitor
  99         MonitorTestTarg.aboutEnterLock = true;
 100         synchronized (MonitorTestTarg.endingMonitor) {
 101         }
 102     }
 103 }
 104 
 105 
 106     /********** test program **********/
 107 
 108 public class MonitorEventTest extends TestScaffold {
 109     ReferenceType targetClass;
 110     ThreadReference mainThread;
 111     List monitors;
 112     MonitorContendedEnterRequest contendedEnterRequest;
 113     MonitorWaitedRequest monitorWaitedRequest;
 114     MonitorContendedEnteredRequest contendedEnteredRequest;
 115     MonitorWaitRequest monitorWaitRequest;
 116 
 117     static int actualWaitCount = 0;
 118     static int actualWaitedCount = 0;
 119     static int actualContendedEnterCount = 0;
 120     static int actualContendedEnteredCount= 0;
 121 
 122     MonitorEventTest (String args[]) {
 123         super(args);
 124     }
 125 
 126     public static void main(String[] args)      throws Exception {
 127         new MonitorEventTest(args).startTests();
 128     }
 129 
 130     /********** event handlers **********/
 131 
 132     public void monitorContendedEnter(MonitorContendedEnterEvent event) {
 133 
 134         actualContendedEnterCount++;
 135     }
 136 
 137     public void monitorContendedEntered(MonitorContendedEnteredEvent event) {
 138 
 139         actualContendedEnteredCount++;
 140 
 141     }
 142 
 143     public void monitorWait(MonitorWaitEvent event) {
 144 
 145         actualWaitCount++;
 146 
 147     }
 148     public void monitorWaited(MonitorWaitedEvent event) {
 149 
 150         actualWaitedCount++;
 151 
 152     }
 153 
 154 
 155 
 156     /********** test core **********/
 157 
 158     protected void runTests() throws Exception {
 159         /*
 160          * Get to the top of main()
 161          * to determine targetClass and mainThread
 162          */
 163         BreakpointEvent bpe = startToMain("MonitorTestTarg");
 164         targetClass = bpe.location().declaringType();
 165         mainThread = bpe.thread();
 166 
 167         int initialSize = mainThread.frames().size();
 168         if (vm().canRequestMonitorEvents()) {
 169             contendedEnterRequest = eventRequestManager().createMonitorContendedEnterRequest();
 170             contendedEnterRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
 171             contendedEnterRequest.enable();
 172             contendedEnteredRequest = eventRequestManager().createMonitorContendedEnteredRequest();
 173             contendedEnteredRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
 174             contendedEnteredRequest.enable();
 175             monitorWaitRequest = eventRequestManager().createMonitorWaitRequest();
 176             monitorWaitRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
 177             monitorWaitRequest.enable();
 178             monitorWaitedRequest = eventRequestManager().createMonitorWaitedRequest();
 179             monitorWaitedRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
 180             monitorWaitedRequest.enable();
 181         } else {
 182             System.out.println("request monitor events not supported " );
 183         }
 184 
 185 
 186         resumeTo("MonitorTestTarg", "foo", "()V");
 187 
 188         /*
 189          * resume until end
 190          */
 191         listenUntilVMDisconnect();
 192 
 193         /*
 194          * At least one of each type event should have recevied by this test.
 195          */
 196         if (vm().canRequestMonitorEvents()) {
 197             if (actualContendedEnterCount == 0) {
 198                 failure("Did not receive any  contended enter event.");
 199             }
 200             if (actualContendedEnteredCount == 0) {
 201                 failure("Did not receive any contended entered event. ");
 202             }
 203             if (actualWaitCount == 0) {
 204                 failure("Did not receive any contended monitor wait event");
 205             }
 206             if (actualWaitedCount == 0) {
 207                 failure("Did not receive any contended monitor waited event");
 208             }
 209         }
 210 
 211 
 212         /*
 213          * deal with results of test
 214          * if anything has called failure("foo") testFailed will be true
 215          */
 216         if (!testFailed) {
 217             println("MonitorEventTest: passed");
 218         } else {
 219             throw new Exception("MonitorEventTest: failed");
 220         }
 221     }
 222 }