1 /*
   2  * Copyright (c) 2007, 2012, 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  */
  26 
  27 package org.graalvm.compiler.jtt.threads;
  28 
  29 import org.junit.Assert;
  30 import org.junit.Test;
  31 
  32 import org.graalvm.compiler.jtt.JTTTest;
  33 
  34 //Test all, mainly monitors
  35 public class Thread_isInterrupted02 extends JTTTest {
  36 
  37     private static final Object start = new Object();
  38     private static final Object end = new Object();
  39     private static int waitTime;
  40 
  41     @SuppressWarnings("unused")
  42     public static boolean test(int i, int time) throws InterruptedException {
  43         waitTime = time;
  44         final Thread thread = new Thread();
  45         synchronized (thread) {
  46             // start the thread and wait for it
  47             thread.setDaemon(true); // in case the thread gets stuck
  48             thread.start();
  49             while (!thread.wait1Condition) {
  50                 thread.wait(10000);
  51             }
  52         }
  53         synchronized (start) {
  54             thread.interrupt();
  55             thread.sentInterrupt = true;
  56         }
  57         synchronized (end) {
  58             while (!thread.wait2Condition) {
  59                 end.wait(10000);
  60             }
  61         }
  62         return thread.interrupted;
  63     }
  64 
  65     private static class Thread extends java.lang.Thread {
  66 
  67         private boolean interrupted;
  68         private boolean sentInterrupt;
  69         private boolean wait1Condition;
  70         private boolean wait2Condition;
  71 
  72         @Override
  73         public void run() {
  74             try {
  75                 synchronized (start) {
  76                     synchronized (this) {
  77                         // signal test thread that we are running
  78                         wait1Condition = true;
  79                         notify();
  80                     }
  81                     // wait for the condition, which should be interrupted
  82                     while (!sentInterrupt) {
  83                         if (waitTime == 0) {
  84                             start.wait();
  85                         } else {
  86                             start.wait(waitTime);
  87                         }
  88                         if (Thread.interrupted()) {
  89                             throw new InterruptedException();
  90                         }
  91                     }
  92                     Assert.fail("should not reach here - was not interrupted");
  93                 }
  94             } catch (InterruptedException e) {
  95                 // interrupted successfully.
  96                 interrupted = true;
  97                 synchronized (end) {
  98                     // notify the other thread we are done
  99                     wait2Condition = true;
 100                     end.notify();
 101                 }
 102             }
 103         }
 104     }
 105 
 106     @Test(timeout = 20000)
 107     public void run0() throws Throwable {
 108         runTest("test", 0, 0);
 109     }
 110 
 111     @Test(timeout = 20000)
 112     public void run1() throws Throwable {
 113         runTest("test", 1, 500);
 114     }
 115 
 116 }