Home | History | Annotate | Download | only in ThreadReference
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *
     15  *  See the License for the specific language governing permissions and
     16  *  limitations under the License.
     17  */
     18 
     19 /**
     20  * @author Vitaly A. Provodin, Anatoly F. Bondarenko
     21  */
     22 
     23 /**
     24  * Created on 15.02.2005
     25  */
     26 package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
     27 
     28 import org.apache.harmony.jpda.tests.framework.jdwp.exceptions.*;
     29 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
     30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
     31 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
     32 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
     33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
     34 
     35 
     36 /**
     37  * JDWP Unit test for ThreadReference.Resume command.
     38  */
     39 public class ResumeTest extends JDWPSyncTestCase {
     40 
     41     static final int testStatusPassed = 0;
     42     static final int testStatusFailed = -1;
     43     static final String debuggeeSignature =
     44             "Lorg/apache/harmony/jpda/tests/jdwp/ThreadReference/ResumeDebuggee;";
     45 
     46     @Override
     47     protected String getDebuggeeClassName() {
     48         return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.ResumeDebuggee";
     49     }
     50 
     51     /**
     52      * This testcase exercises ThreadReference.Resume command.
     53      * <BR>At first the test starts ResumeDebuggee which starts and runs some tested threads.
     54      * <BR> Then the tests checks that for every tested thread the ThreadReference.Resume
     55      * command acts according to spec:
     56      * <BR>&nbsp;&nbsp; - command does not cause any error if thread is not suspended;
     57      * <BR>&nbsp;&nbsp; - command does not resume thread actually if number of suspendings
     58      *   is greater than number of resumings;
     59      * <BR>&nbsp;&nbsp; - command resumes thread actually if number of suspendings
     60      *   is equal to number of resumings;
     61      */
     62     public void testResume001() {
     63         logWriter.println("==> testResume001: START...");
     64         String debuggeeMessage = synchronizer.receiveMessage();
     65         int testedThreadsNumber = 0;
     66         try {
     67             testedThreadsNumber = Integer.valueOf(debuggeeMessage).intValue();
     68         } catch (NumberFormatException exception) {
     69             logWriter.println
     70                 ("## FAILURE: Exception while getting number of started threads from debuggee = " + exception);
     71             setStaticIntField(debuggeeSignature, ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
     72             printErrorAndFail("\n## Can NOT get number of started threads from debuggee! ");
     73         }
     74         testedThreadsNumber++; // to add debuggee main thread
     75         logWriter.println("==>  Number of threads in debuggee to test = " + testedThreadsNumber);
     76         String[] testedThreadsNames = new String[testedThreadsNumber];
     77         long[] testedThreadsIDs = new long[testedThreadsNumber];
     78         String debuggeeMainThreadName = synchronizer.receiveMessage();
     79         for (int i = 0; i < testedThreadsNumber; i++) {
     80             if ( i < (testedThreadsNumber-1) ) {
     81                 testedThreadsNames[i] = ResumeDebuggee.THREAD_NAME_PATTERN + i;
     82             } else {
     83                 testedThreadsNames[i] = debuggeeMainThreadName;
     84             }
     85             testedThreadsIDs[i] = 0;
     86         }
     87 
     88         // getting ID of the tested thread
     89         ReplyPacket allThreadIDReply = null;
     90         try {
     91             allThreadIDReply = debuggeeWrapper.vmMirror.getAllThreadID();
     92         } catch (ReplyErrorCodeException exception) {
     93             logWriter.println
     94                 ("## FAILURE: Exception in vmMirror.getAllThreadID() = " + exception);
     95             setStaticIntField(debuggeeSignature, ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
     96             printErrorAndFail("\n## Can NOT get all ThreadID in debuggee! ");
     97         }
     98         int threads = allThreadIDReply.getNextValueAsInt();
     99         logWriter.println("==>  Number of all threads in debuggee = " + threads);
    100         for (int i = 0; i < threads; i++) {
    101             long threadID = allThreadIDReply.getNextValueAsThreadID();
    102             String threadName = null;
    103             try {
    104                 threadName = debuggeeWrapper.vmMirror.getThreadName(threadID);
    105             } catch (ReplyErrorCodeException exception) {
    106                 logWriter.println
    107                     ("==> WARNING: Can NOT get thread name for threadID = " + threadID);
    108                 continue;
    109             }
    110             int k = 0;
    111             for (; k < testedThreadsNumber; k++) {
    112                 if ( threadName.equals(testedThreadsNames[k]) ) {
    113                     testedThreadsIDs[k] = threadID;
    114                     break;
    115                 }
    116             }
    117         }
    118 
    119         boolean testedThreadNotFound = false;
    120         for (int i = 0; i < testedThreadsNumber; i++) {
    121             if ( testedThreadsIDs[i] == 0 ) {
    122                 logWriter.println("## FAILURE: Tested thread is not found out among debuggee threads!");
    123                 logWriter.println("##          Thread name = " + testedThreadsNames[i]);
    124                 testedThreadNotFound = true;
    125             }
    126         }
    127         if ( testedThreadNotFound ) {
    128             setStaticIntField(debuggeeSignature, ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
    129             printErrorAndFail("\n## Some of tested threads are not found!");
    130         }
    131 
    132         CommandPacket packet = null;
    133         ReplyPacket reply = null;
    134         String errorMessage = "";
    135 
    136         boolean resumeCommandFailed = false;
    137         boolean statusCommandFailed = false;
    138         boolean suspendStatusFailed = false;
    139         boolean suspendCommandFailed = false;
    140 
    141         logWriter.println
    142         ("\n==> Check that ThreadReference.Resume command does not cause any error if thread is not suspended...");
    143 
    144         for (int i = 0; i < testedThreadsNumber; i++) {
    145             logWriter.println("\n==> Check for Thread: threadID = " + testedThreadsIDs[i]
    146                     + "; threadName = " + testedThreadsNames[i]);
    147             logWriter.println("==> Send ThreadReference.Resume command...");
    148             packet = new CommandPacket(
    149                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    150                     JDWPCommands.ThreadReferenceCommandSet.ResumeCommand);
    151             packet.setNextValueAsThreadID(testedThreadsIDs[i]);
    152             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    153             if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Resume command") ) {
    154                 resumeCommandFailed = true;
    155             } else {
    156                 logWriter.println("==> OK - ThreadReference.Resume command without any error!");
    157             }
    158         }
    159 
    160         if ( resumeCommandFailed ) {
    161             setStaticIntField(debuggeeSignature, ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
    162             printErrorAndFail("\n## Error found out while ThreadReference.Resume command performing!");
    163         }
    164 
    165         logWriter.println
    166         ("\n==> Check that ThreadReference.Resume command resumes threads after VirtualMachine.Suspend command...");
    167 
    168         logWriter.println("\n==> Send VirtualMachine.Suspend command...");
    169         packet = new CommandPacket(
    170                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
    171                 JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
    172         reply = debuggeeWrapper.vmMirror.performCommand(packet);
    173         int errorCode = reply.getErrorCode();
    174         if ( errorCode !=  JDWPConstants.Error.NONE ) {
    175             setStaticIntField(debuggeeSignature, ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
    176             logWriter.println("## FAILURE: VirtualMachine.Suspend command returns error = " + errorCode
    177                     + "(" + JDWPConstants.Error.getName(errorCode) + ")");
    178             printErrorAndFail("## VirtualMachine.Suspend command FAILED!");
    179         } else {
    180             logWriter.println("==> VirtualMachine.Suspend command is OK!");
    181         }
    182 
    183         for (int i = 0; i < testedThreadsNumber; i++) {
    184             logWriter.println("\n==> Check for Thread: threadID = " + testedThreadsIDs[i]
    185                 + "; threadName = " + testedThreadsNames[i]);
    186 
    187             logWriter.println("==> Send ThreadReference.Status command...");
    188             packet = new CommandPacket(
    189                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    190                     JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
    191             packet.setNextValueAsReferenceTypeID(testedThreadsIDs[i]);
    192             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    193             if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Status command") ) {
    194                 statusCommandFailed = true;
    195                 continue;
    196             }
    197 
    198             int threadStatus = reply.getNextValueAsInt();
    199             int suspendStatus = reply.getNextValueAsInt();
    200 
    201             logWriter.println("==> threadStatus = " + threadStatus + "("
    202                     + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
    203             logWriter.println("==> suspendStatus = " + suspendStatus + "("
    204                     + JDWPConstants.SuspendStatus.getName(suspendStatus) + ")");
    205             if (suspendStatus
    206                     != JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
    207                 logWriter.println("## FAILURE: Unexpected suspendStatus for thread!");
    208                 logWriter.println("##          Expected suspendStatus  = "
    209                     + JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED
    210                     + "(" + JDWPConstants.SuspendStatus.getName
    211                     (JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) +")");
    212                 suspendStatusFailed = true;
    213                 continue;
    214             }
    215 
    216             logWriter.println("==> Send ThreadReference.Resume command...");
    217             packet = new CommandPacket(
    218                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    219                     JDWPCommands.ThreadReferenceCommandSet.ResumeCommand);
    220             packet.setNextValueAsThreadID(testedThreadsIDs[i]);
    221             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    222             if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Resume command") ) {
    223                 resumeCommandFailed = true;
    224                 continue;
    225             }
    226 
    227             logWriter.println("==> Send ThreadReference.Status command...");
    228             packet = new CommandPacket(
    229                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    230                     JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
    231             packet.setNextValueAsReferenceTypeID(testedThreadsIDs[i]);
    232             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    233             if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Status command") ) {
    234                 statusCommandFailed = true;
    235                 continue;
    236             }
    237 
    238             threadStatus = reply.getNextValueAsInt();
    239             suspendStatus = reply.getNextValueAsInt();
    240 
    241             logWriter.println("==> threadStatus = " + threadStatus + "("
    242                     + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
    243             logWriter.println("==> suspendStatus = " + suspendStatus + "("
    244                     + JDWPConstants.SuspendStatus.getName(suspendStatus) + ")");
    245             if (suspendStatus
    246                     == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
    247                 logWriter.println
    248                     ("## FAILURE: Thread still is suspended after ThreadReference.Resume command!");
    249                 suspendStatusFailed = true;
    250             }
    251         }
    252 
    253         logWriter.println("\n==> Send VirtualMachine.Resume command...");
    254         packet = new CommandPacket(
    255                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
    256                 JDWPCommands.VirtualMachineCommandSet.ResumeCommand);
    257         reply = debuggeeWrapper.vmMirror.performCommand(packet);
    258         errorCode = reply.getErrorCode();
    259         if ( errorCode !=  JDWPConstants.Error.NONE ) {
    260             setStaticIntField(debuggeeSignature, ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
    261             logWriter.println("## FAILURE: VirtualMachine.Resume command returns error = " + errorCode
    262                 + "(" + JDWPConstants.Error.getName(errorCode) + ")");
    263             printErrorAndFail("## VirtualMachine.Resume command FAILED!");
    264         } else {
    265             logWriter.println("==> VirtualMachine.Resume command is OK!");
    266         }
    267 
    268         if ( statusCommandFailed ) {
    269             errorMessage = errorMessage + "## Error found out while ThreadReference.Status command performing!\n";
    270         }
    271         if ( suspendStatusFailed ) {
    272             errorMessage = errorMessage + "## Unexpected suspendStatus found out!\n";
    273         }
    274         if ( resumeCommandFailed ) {
    275             errorMessage = errorMessage + "## Error found out while ThreadReference.Resume command performing!\n";
    276         }
    277         if ( ! errorMessage.equals("") ) {
    278             setStaticIntField(debuggeeSignature, ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
    279             printErrorAndFail("\ntestResume001 FAILED:\n" + errorMessage);
    280         }
    281 
    282         logWriter.println
    283         ("\n==> Check ThreadReference.Resume command after ThreadReference.Suspend command...");
    284 
    285         int suspendNumber = 3;
    286         for (int i = 0; i < testedThreadsNumber; i++) {
    287             logWriter.println("\n==> Check for Thread: threadID = " + testedThreadsIDs[i]
    288                 + "; threadName = " + testedThreadsNames[i]);
    289             logWriter.println("==> Send ThreadReference.Suspend commands: number of commandss = "
    290                     + suspendNumber + "...");
    291             int j = 0;
    292             for (; j < suspendNumber; j++) {
    293                 packet = new CommandPacket(
    294                         JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    295                         JDWPCommands.ThreadReferenceCommandSet.SuspendCommand);
    296                 packet.setNextValueAsThreadID(testedThreadsIDs[i]);
    297                 reply = debuggeeWrapper.vmMirror.performCommand(packet);
    298                 if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Suspend command") ) {
    299                     break;
    300                 }
    301             }
    302             if ( j < suspendNumber ) {
    303                 suspendCommandFailed = true;
    304                 continue;
    305             }
    306 
    307             logWriter.println("==> Send ThreadReference.Status command...");
    308             packet = new CommandPacket(
    309                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    310                     JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
    311             packet.setNextValueAsReferenceTypeID(testedThreadsIDs[i]);
    312             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    313             if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Status command") ) {
    314                 statusCommandFailed = true;
    315                 continue;
    316             }
    317 
    318             int threadStatus = reply.getNextValueAsInt();
    319             int suspendStatus = reply.getNextValueAsInt();
    320 
    321             logWriter.println("==> threadStatus = " + threadStatus + "("
    322                     + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
    323             logWriter.println("==> suspendStatus = " + suspendStatus + "("
    324                     + JDWPConstants.SuspendStatus.getName(suspendStatus) + ")");
    325             if (suspendStatus
    326                     != JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
    327                 logWriter.println("## FAILURE: Unexpected suspendStatus for thread!");
    328                 logWriter.println("##          Expected suspendStatus  = "
    329                     + JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED
    330                     + "(" + JDWPConstants.SuspendStatus.getName
    331                     (JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) +")");
    332                 suspendStatusFailed = true;
    333                 continue;
    334             }
    335 
    336             logWriter.println
    337             ("==> Send ThreadReference.Resume command 1 time - thread must NOT be actually resumed...");
    338             packet = new CommandPacket(
    339                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    340                     JDWPCommands.ThreadReferenceCommandSet.ResumeCommand);
    341             packet.setNextValueAsThreadID(testedThreadsIDs[i]);
    342             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    343             if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Resume command") ) {
    344                 resumeCommandFailed = true;
    345                 continue;
    346             }
    347 
    348             logWriter.println("==> Send ThreadReference.Status command...");
    349             packet = new CommandPacket(
    350                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    351                     JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
    352             packet.setNextValueAsReferenceTypeID(testedThreadsIDs[i]);
    353             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    354             if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Status command") ) {
    355                 statusCommandFailed = true;
    356                 continue;
    357             }
    358 
    359             threadStatus = reply.getNextValueAsInt();
    360             suspendStatus = reply.getNextValueAsInt();
    361 
    362             logWriter.println("==> threadStatus = " + threadStatus + "("
    363                     + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
    364             logWriter.println("==> suspendStatus = " + suspendStatus + "("
    365                     + JDWPConstants.SuspendStatus.getName(suspendStatus) + ")");
    366             if (suspendStatus
    367                     != JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
    368                 logWriter.println("## FAILURE: Unexpected suspendStatus for thread!");
    369                 logWriter.println("##          Expected suspendStatus  = "
    370                     + JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED
    371                     + "(" + JDWPConstants.SuspendStatus.getName
    372                     (JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) +")");
    373                 suspendStatusFailed = true;
    374                 continue;
    375             }
    376 
    377             logWriter.println("==> Send ThreadReference.Resume commands: number of commands = "
    378                     + (suspendNumber-1) + " - thread must be actually resumed");
    379             j = 0;
    380             for (; j < (suspendNumber-1); j++) {
    381                 packet = new CommandPacket(
    382                         JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    383                         JDWPCommands.ThreadReferenceCommandSet.ResumeCommand);
    384                 packet.setNextValueAsThreadID(testedThreadsIDs[i]);
    385                 reply = debuggeeWrapper.vmMirror.performCommand(packet);
    386                 if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Resume command") ) {
    387                     break;
    388                 }
    389             }
    390             if ( j < (suspendNumber-1) ) {
    391                 resumeCommandFailed = true;
    392                 continue;
    393             }
    394 
    395             logWriter.println("==> Send ThreadReference.Status command...");
    396             packet = new CommandPacket(
    397                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    398                     JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
    399             packet.setNextValueAsReferenceTypeID(testedThreadsIDs[i]);
    400             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    401             if ( ! checkReplyPacketWithoutFail(reply, "ThreadReference.Status command") ) {
    402                 statusCommandFailed = true;
    403                 continue;
    404             }
    405 
    406             threadStatus = reply.getNextValueAsInt();
    407             suspendStatus = reply.getNextValueAsInt();
    408 
    409             logWriter.println("==> threadStatus = " + threadStatus + "("
    410                     + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
    411             logWriter.println("==> suspendStatus = " + suspendStatus + "("
    412                     + JDWPConstants.SuspendStatus.getName(suspendStatus) + ")");
    413             if (suspendStatus
    414                     == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
    415                 logWriter.println
    416                     ("## FAILURE: Thread still is suspended after ThreadReference.Resume commands: "
    417                     + "number of total resume commands = "+ suspendNumber + " !");
    418                 suspendStatusFailed = true;
    419             }
    420         }
    421 
    422         if ( suspendCommandFailed ) {
    423             errorMessage = errorMessage + "## Error found out while ThreadReference.Suspend command performing!\n";
    424         }
    425         if ( resumeCommandFailed ) {
    426             errorMessage = errorMessage + "## Error found out while ThreadReference.Resume command performing!\n";
    427         }
    428         if ( statusCommandFailed ) {
    429             errorMessage = errorMessage + "## Error found out while ThreadReference.Status command performing!\n";
    430         }
    431         if ( suspendStatusFailed ) {
    432             errorMessage = errorMessage + "## Unexpected suspendStatus found out!\n";
    433         }
    434 
    435         setStaticIntField(debuggeeSignature, ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
    436         if ( ! errorMessage.equals("") ) {
    437             printErrorAndFail("\ntestResume001 FAILED:\n" + errorMessage);
    438         }
    439 
    440         logWriter.println("\n==> testResume001 - OK!");
    441     }
    442 }
    443