jdk/test/com/sun/jdi/InterruptHangTest.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 24973 8c4bc3fa4c4e
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
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *  @bug 6459476
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *  @summary Debuggee is blocked,  looks like running
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 *  @author jjh
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *  @run compile -g InterruptHangTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *  @run main InterruptHangTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Debuggee has two threads.  Debugger keeps stepping in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * the first thread.  The second thread keeps interrupting the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * thread.  If a long time goes by with the debugger not getting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * a step event, the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
class InterruptHangTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
    public static String sync = "sync";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
        int answer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
        System.out.println("Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
        Interruptor interruptorThread = new Interruptor(Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
        synchronized(sync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
            interruptorThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
                sync.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
            } catch (InterruptedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
                System.out.println("Debuggee interruptee: interrupted before starting loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        // Debugger will keep stepping thru this loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        for (int ii = 0; ii < 200; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            answer++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                // Give other thread a chance to run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            } catch (InterruptedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                System.out.println("Debuggee interruptee: interrupted at iteration: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                   + ii);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        // Kill the interrupter thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        interruptorThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println("Goodbye from InterruptHangTarg!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
class Interruptor extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    Thread interruptee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    Interruptor(Thread interruptee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.interruptee = interruptee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        synchronized(InterruptHangTarg.sync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            InterruptHangTarg.sync.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int ii = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            ii++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            interruptee.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            } catch (InterruptedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                System.out.println("Debuggee Interruptor: finished after " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                   ii + " iterrupts");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
public class InterruptHangTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    Thread timerThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    String sync = "sync";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static int nSteps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    InterruptHangTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        new InterruptHangTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /********** event handlers **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void stepCompleted(StepEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        synchronized(sync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            nSteps++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        println("Got StepEvent " + nSteps + " at line " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                event.location().method() + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                event.location().lineNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (nSteps == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            timerThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        BreakpointEvent bpe = startToMain("InterruptHangTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        EventRequestManager erm = vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * Set event requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        StepRequest request = erm.createStepRequest(mainThread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                                    StepRequest.STEP_LINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                                    StepRequest.STEP_OVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // Will be started by the step event handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        timerThread = new Thread("test timer") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    int mySteps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            Thread.sleep(20000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            synchronized(sync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                System.out.println("steps = " + nSteps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                if (mySteps == nSteps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                    // no step for 10 secs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                    failure("failure: Debuggee appears to be hung");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                    vm().exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                            mySteps = nSteps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        } catch (InterruptedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * resume the target listening for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        timerThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            println("InterruptHangTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new Exception("InterruptHangTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}