summaryrefslogtreecommitdiff
path: root/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/VirtualMachine/ResumeTest.java
blob: 813b9349bff54942ce89cfde992e9e52172db6d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Vitaly A. Provodin, Anatoly F. Bondarenko
 */

/**
 * Created on 10.02.2005
 */
package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;

import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
import org.apache.harmony.jpda.tests.framework.jdwp.exceptions.ReplyErrorCodeException;
import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;

import java.util.ArrayList;
import java.util.List;


/**
 * JDWP Unit test for VirtualMachine.Resume command.
 */
public class ResumeTest extends JDWPSyncTestCase {

    static final String debuggeeSignature =
        "Lorg/apache/harmony/jpda/tests/jdwp/VirtualMachine/ResumeDebuggee;";

    @Override
    protected String getDebuggeeClassName() {
        return ResumeDebuggee.class.getName();
    }

    @Override
    protected void internalTearDown() {
        // We need to finish the tested threads before detaching.
        logWriter.println("Finish debuggee tested threads");
        setStaticIntField(debuggeeSignature,
                ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
        super.internalTearDown();
    }

    /**
     * This testcase exercises VirtualMachine.Resume command.
     * <BR>At first the test starts ResumeDebuggee which starts and runs some tested threads.
     * <BR> Then the test performs VirtualMachine.Suspend command and checks with help of
     * ThreadReference.Status command that all debuggee tested threads are suspended.
     * <BR> Then the test performs VirtualMachine.Resume command and checks with help of
     * ThreadReference.Status command that all debuggee tested threads are resumed.
     */
    public void testResume001() {
        logWriter.println("==> testResume001: START...");

        // The error messages in case of test failure.
        List<String> errorMessages = new ArrayList<String>();

        // All the threads we're interested in.
        ThreadInfo[] threadInfos = createThreadInfos();

        // Suspend all threads with VirtualMachine.Suspend command.
        suspendAll();

        // Check all threads are suspended now.
        logWriter.println("\n==> Check that all tested threads are suspended " +
                "after VirtualMachine.Suspend command...");
        checkThreadStatus(threadInfos, true, errorMessages);

        // Resume all threads with VirtualMachine.Resume command.
        resumeAll();

        // Check all threads are NOT suspended anymore.
        logWriter.println("\n==> Check that all tested threads are resumed " +
                "after VirtualMachine.Resume command...");
        checkThreadStatus(threadInfos, false, errorMessages);

        if (!errorMessages.isEmpty()) {
            // Print error messages first.
            for (String errorMessage : errorMessages) {
                logWriter.printError(errorMessage + "\n");
            }
            printErrorAndFail("\ntestResume001 FAILED");
        } else {
            logWriter.println("\n==> testResume001 - OK!");
        }
    }

    /**
     * This testcase exercises VirtualMachine.Resume command.
     * <BR>At first the test starts ResumeDebuggee which starts and runs some
     * tested threads.
     * <BR> Then the test performs VirtualMachine.Suspend command twice and
     * checks, with help of ThreadReference.Status command, that all debuggee
     * tested threads are suspended.
     * <BR> Then the test performs VirtualMachine.Resume command and checks
     * that all debuggee tested threads are still suspended.
     * <BR> Then the test performs VirtualMachine.Resume command again and
     * checks that all debuggee tested threads are resumed.
     */
    public void testResume002() {
        logWriter.println("==> testResume002: START...");

        // The error messages in case of test failure.
        List<String> errorMessages = new ArrayList<String>();

        // All the threads we're interested in.
        ThreadInfo[] threadInfos = createThreadInfos();

        // Suspend all threads with VirtualMachine.Suspend command.
        suspendAll();

        // Check all threads are suspended now.
        logWriter.println("\n==> Check that all tested threads are suspended " +
                "after VirtualMachine.Suspend command...");
        checkThreadStatus(threadInfos, true, errorMessages);

        // Suspend all threads again.
        suspendAll();

        // Check all threads are still suspended.
        logWriter.println("\n==> Check that all tested threads are still " +
                "suspended after another VirtualMachine.Suspend command...");
        checkThreadStatus(threadInfos, true, errorMessages);

        // Resume all threads with VirtualMachine.Resume command.
        resumeAll();

        // Check all threads are still suspended.
        logWriter.println("\n==> Check that all tested threads are still " +
                "suspended after VirtualMachine.Resume command...");
        checkThreadStatus(threadInfos, true, errorMessages);

        // Resume all threads again.
        resumeAll();

        // Check all threads are NOT suspended anymore.
        logWriter.println("\n==> Check that all tested threads are resumed " +
                "after VirtualMachine.Resume command...");
        checkThreadStatus(threadInfos, false, errorMessages);

        if (!errorMessages.isEmpty()) {
            // Print error messages first.
            for (String errorMessage : errorMessages) {
                logWriter.printError(errorMessage + "\n");
            }
            printErrorAndFail("\ntestResume002 FAILED");
        } else {
            logWriter.println("\n==> testResume002 - OK!");
        }
    }

    /**
     * This testcase exercises VirtualMachine.Resume command.
     * <BR>At first the test starts ResumeDebuggee which starts and runs some
     * tested threads.
     * <BR> Then the test performs VirtualMachine.Resume command and checks it
     * does not cause any error if we do not perform VirtualMachine.Suspend
     * before.
     * <BR> Then the test performs VirtualMachine.Suspend command and checks
     * that all debuggee tested threads are suspended.
     * <BR> Then the test performs VirtualMachine.Resume command and checks
     * that all debuggee tested threads are resumed.
     */
    public void testResume003() {
        logWriter.println("==> testResume002: START...");

        // The error messages in case of test failure.
        List<String> errorMessages = new ArrayList<String>();

        // All the threads we're interested in.
        ThreadInfo[] threadInfos = createThreadInfos();

        // Resume all threads: should be a no-op.
        resumeAll();

        // Check all threads are NOT suspended.
        logWriter.println("\n==> Check that no tested thread is suspended " +
                "after VirtualMachine.Resume command...");
        checkThreadStatus(threadInfos, false, errorMessages);

        // Suspend all threads with VirtualMachine.Suspend command.
        suspendAll();

        // Check all threads are suspended now.
        logWriter.println("\n==> Check that all tested threads are suspended " +
                "after VirtualMachine.Suspend command...");
        checkThreadStatus(threadInfos, true, errorMessages);

        // Resume all threads with VirtualMachine.Resume command.
        resumeAll();

        // Check all threads are NOT suspended anymore.
        logWriter.println("\n==> Check that all tested threads are resumed " +
                "after VirtualMachine.Resume command...");
        checkThreadStatus(threadInfos, false, errorMessages);

        if (!errorMessages.isEmpty()) {
            // Print error messages first.
            for (String errorMessage : errorMessages) {
                logWriter.printError(errorMessage + "\n");
            }
            printErrorAndFail("\ntestResume002 FAILED");
        } else {
            logWriter.println("\n==> testResume002 - OK!");
        }
    }
    private static class ThreadInfo {
        final String threadName;
        long threadId = 0;

        public ThreadInfo(String threadName) {
            this.threadName = threadName;
        }
    }

    /**
     * Suspends all threads using VirtualMachine.Suspend command.
     */
    private void suspendAll() {
        logWriter.println("\n==> Send VirtualMachine.Suspend command...");
        CommandPacket packet = new CommandPacket(
                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
                JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
        checkReplyPacket(reply, "VirtualMachine.Suspend");
        logWriter.println("==> VirtualMachine.Suspend command - OK.");
    }

    /**
     * Resumes all threads using VirtualMachine.Resume command.
     */
    private void resumeAll() {
        logWriter.println("\n==> Send VirtualMachine.Resume command...");
        CommandPacket packet = new CommandPacket(
                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
                JDWPCommands.VirtualMachineCommandSet.ResumeCommand);
        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
        checkReplyPacket(reply, "VirtualMachine.Resume");
        logWriter.println("==> VirtualMachine.Resume command - OK.");
    }

    /**
     * Returns the number of threads used in the tests (including the main
     * thread).
     */
    private int getThreadsNumber() {
        String debuggeeMessage = synchronizer.receiveMessage();
        int testedThreadsNumber = 0;
        try {
            testedThreadsNumber = Integer.valueOf(debuggeeMessage).intValue();
        } catch (NumberFormatException exception) {
            logWriter.println("## FAILURE: Exception while getting number of"
                    + " started threads from debuggee = " + exception);
            printErrorAndFail("\n## Can NOT get number of started threads "
                    + "from debuggee! ");
        }
        return testedThreadsNumber + 1;  // to add debuggee main thread
    }

    /**
     * Creates ThreadInfo array containing information about each tested thread:
     * thread name and thread JDWP id.
     */
    private ThreadInfo[] createThreadInfos() {
        int testedThreadsNumber = getThreadsNumber();
        logWriter.println("==>  Number of threads in debuggee to test = "
                + testedThreadsNumber);
        ThreadInfo[] threadInfos = new ThreadInfo[testedThreadsNumber];

        String debuggeeMainThreadName = synchronizer.receiveMessage();
        // Initialize all threads
        for (int i = 0, e = threadInfos.length - 1; i < e; ++i) {
            threadInfos[i] = new ThreadInfo(ResumeDebuggee.THREAD_NAME_PATTERN + i);
        }
        threadInfos[threadInfos.length - 1] = new ThreadInfo(debuggeeMainThreadName);

        // Getting ID of the tested thread using VirtualMachine.AllThreads.
        ReplyPacket allThreadIDReply = null;
        try {
            allThreadIDReply = debuggeeWrapper.vmMirror.getAllThreadID();
        } catch (ReplyErrorCodeException exception) {
            logWriter.println
                ("## FAILURE: Exception in vmMirror.getAllThreadID() = " + exception);
            printErrorAndFail("\n## Can NOT get all ThreadID in debuggee! ");
        }
        int threads = allThreadIDReply.getNextValueAsInt();
        logWriter.println("==>  Number of all threads in debuggee = " + threads);
        for (int i = 0; i < threads; i++) {
            long threadID = allThreadIDReply.getNextValueAsThreadID();
            String threadName = null;
            try {
                threadName = debuggeeWrapper.vmMirror.getThreadName(threadID);
            } catch (ReplyErrorCodeException exception) {
                logWriter.println
                    ("==> WARNING: Can NOT get thread name for threadID = " + threadID);
                continue;
            }
            for (ThreadInfo threadInfo : threadInfos) {
                if (threadInfo.threadName.equals(threadName) ) {
                    threadInfo.threadId = threadID;
                    break;
                }
            }
        }

        // Check we found thread id for each thread.
        boolean testedThreadNotFound = false;
        for (ThreadInfo threadInfo : threadInfos) {
            if (threadInfo.threadId == 0) {
                logWriter.println("## FAILURE: Tested thread is not found out "
                        + "among debuggee threads!");
                logWriter.println("##          Thread name = "
                        + threadInfo.threadName);
                testedThreadNotFound = true;
            }
        }
        if (testedThreadNotFound) {
            printErrorAndFail("\n## Some of tested threads are not found!");
        }

        return threadInfos;
    }

    /**
     * Checks suspend status of each tested thread is the expected one.
     *
     * @param threadInfos
     *          the thread information
     * @param isSuspended
     *          if true, thread must be suspended; otherwise thread
     *          must not be suspended.
     * @param errorMessages
     *          a list of String to append error message.
     */
    private void checkThreadStatus(ThreadInfo[] threadInfos,
            boolean isSuspended, List<String> errorMessages) {
        boolean statusCommandFailed = false;
        boolean suspendStatusFailed = false;

        for (ThreadInfo threadInfo : threadInfos) {
            logWriter.println("\n==> Check for Thread: threadID = "
                    + threadInfo.threadId
                    + " (" + threadInfo.threadName + ")");

            logWriter.println("==> Send ThreadReference.Status command...");
            CommandPacket packet = new CommandPacket(
                    JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
                    JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
            packet.setNextValueAsThreadID(threadInfo.threadId);
            ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
            if (!checkReplyPacketWithoutFail(reply, "ThreadReference.Status command")) {
                logWriter.println("Can't get thread status for thread " +
                        threadInfo.threadId +
                        " \"" + threadInfo.threadName + "\"");
                statusCommandFailed = true;
                continue;
            }

            int threadStatus = reply.getNextValueAsInt();
            int suspendStatus = reply.getNextValueAsInt();

            logWriter.println("==> threadStatus = " + threadStatus + " ("
                    + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
            logWriter.println("==> suspendStatus = " + suspendStatus + " ("
                    + JDWPConstants.SuspendStatus.getName(suspendStatus) + ")");

            boolean isThreadSuspended =
                    (suspendStatus == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED);
            if (isThreadSuspended != isSuspended) {
                logWriter.println("## FAILURE: Unexpected suspendStatus for " +
                        "checked thread " + threadInfo.threadId +
                        " \"" + threadInfo.threadName + "\"");
                logWriter.println("##          Expected suspendStatus  = "
                        + JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED
                        + "(" + JDWPConstants.SuspendStatus.getName
                        (JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) +")");
                suspendStatusFailed = true;
                continue;
            }
        }

        if (statusCommandFailed) {
            errorMessages.add("## Error found out while ThreadReference.Status "
                    + "command performing!");
        }
        if (suspendStatusFailed) {
            errorMessages.add("## Unexpected suspendStatus found out!");
        }
    }
}