jdk/test/com/sun/jdi/MonitorEventTest.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6705945: com.sun.tools.javac.zip files do not have valid copyright Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *  @bug 4401399
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary Simple basic test of jdi Monitor request and event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @author Swamy Venkataramanappa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g MonitorEventTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main MonitorEventTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class MonitorTestTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static Object endingMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static Object startingMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static final long timeout = 30 * 6000; // milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static volatile boolean aboutEnterLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static void foo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println("Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        endingMonitor = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        startingMonitor = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        myThread t1 = new myThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        foo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        aboutEnterLock = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        synchronized(endingMonitor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // run thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                // start thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                synchronized (startingMonitor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    t1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    startingMonitor.wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                System.out.println("Interrupted exception " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            while(!(aboutEnterLock && t1.getState() == Thread.State.BLOCKED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }catch(Exception x){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    System.out.println(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            t1.join(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (Exception x){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.out.println("Exception while thread.join :" + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println("Test exiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
class myThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        synchronized(MonitorTestTarg.startingMonitor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            MonitorTestTarg.startingMonitor.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // contended enter wait until main thread release monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        MonitorTestTarg.aboutEnterLock = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        synchronized (MonitorTestTarg.endingMonitor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
public class MonitorEventTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    ReferenceType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    List monitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    MonitorContendedEnterRequest contendedEnterRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    MonitorWaitedRequest monitorWaitedRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    MonitorContendedEnteredRequest contendedEnteredRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    MonitorWaitRequest monitorWaitRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static int actualWaitCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static int actualWaitedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static int actualContendedEnterCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static int actualContendedEnteredCount= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    MonitorEventTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        new MonitorEventTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /********** event handlers **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void monitorContendedEnter(MonitorContendedEnterEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        actualContendedEnterCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void monitorContendedEntered(MonitorContendedEnteredEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        actualContendedEnteredCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void monitorWait(MonitorWaitEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        actualWaitCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public void monitorWaited(MonitorWaitedEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        actualWaitedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        BreakpointEvent bpe = startToMain("MonitorTestTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        targetClass = bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int initialSize = mainThread.frames().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (vm().canRequestMonitorEvents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            contendedEnterRequest = eventRequestManager().createMonitorContendedEnterRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            contendedEnterRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            contendedEnterRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            contendedEnteredRequest = eventRequestManager().createMonitorContendedEnteredRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            contendedEnteredRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            contendedEnteredRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            monitorWaitRequest = eventRequestManager().createMonitorWaitRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            monitorWaitRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            monitorWaitRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            monitorWaitedRequest = eventRequestManager().createMonitorWaitedRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            monitorWaitedRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            monitorWaitedRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            System.out.println("request monitor events not supported " );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        resumeTo("MonitorTestTarg", "foo", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * resume until end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * At least one of each type event should have recevied by this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (vm().canRequestMonitorEvents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (actualContendedEnterCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                failure("Did not receive any  contended enter event.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (actualContendedEnteredCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                failure("Did not receive any contended entered event. ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (actualWaitCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                failure("Did not receive any contended monitor wait event");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (actualWaitedCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                failure("Did not receive any contended monitor waited event");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            println("MonitorEventTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new Exception("MonitorEventTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}