1 /*
   2  * Copyright (c) 2011, 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  * @summary This is a utility for coordinating the flow of events on different
  26             threads.
  27  * @summary com.apple.junit.utils
  28  */
  29 package test.java.awt.regtesthelpers;
  30 
  31 public class Waypoint {
  32 
  33     static final String MSG = "Waypoint timed out";
  34     // Wait up to five seconds for our clear() to be called
  35     static final int TIMEOUT = 5000;
  36     boolean clear = false;
  37 
  38     public Waypoint() {
  39 
  40     }
  41 
  42     //
  43     //    Pause for either TIMEOUT millis or until clear() is called
  44     //
  45     synchronized public void requireClear() throws RuntimeException {
  46         requireClear(MSG, TIMEOUT);
  47     }
  48 
  49     synchronized public void requireClear(long timeout)
  50             throws RuntimeException {
  51         requireClear(MSG, timeout);
  52     }
  53 
  54     synchronized public void requireClear(String timeOutMsg)
  55             throws RuntimeException {
  56         requireClear(timeOutMsg, TIMEOUT);
  57     }
  58 
  59     synchronized public void requireClear(String timeOutMsg, long timeout)
  60             throws RuntimeException {
  61         long endtime = System.currentTimeMillis() + timeout;
  62         try {
  63             while (isClear() == false) {
  64                 if (System.currentTimeMillis() < endtime) {
  65                     wait(200);
  66                 } else {
  67                     break;
  68                 }
  69             }
  70 
  71             if (!isClear()) {
  72                 throw new RuntimeException(timeOutMsg);
  73             }
  74         } catch (InterruptedException ix) {
  75         }
  76     }
  77 
  78     //
  79     //    Called when it is valid to procede past the waypoint
  80     //
  81     synchronized public void clear() {
  82         clear = true;
  83         notify();
  84     }
  85 
  86     //
  87     //    Should be checked after a call to requireClear() to make
  88     //    sure that we did not time out.
  89     //
  90     synchronized public boolean isClear() {
  91         return clear;
  92     }
  93 
  94     synchronized public boolean isValid() {
  95         return clear;
  96     }
  97 
  98     //
  99     //    For re-use of a waypoint.  Be careful.
 100     //
 101     synchronized public void reset() {
 102         clear = false;
 103     }
 104 
 105 }