modules/graphics/src/test/java/test/javafx/concurrent/AbstractTask.java

Print this page
rev 9250 : 8134762: Refactor Javafx graphics module tests for clear separation of tests
Reviewed-by:


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.concurrent;
  27 
  28 import java.util.concurrent.Semaphore;


  29 
  30 /**
  31  * For testing purposes, we use this subclass of Task that will fake out the
  32  * runLater and isFxApplicationThread calls, such that we can actually run
  33  * and test Task in a single-threaded manner.
  34  * <p>
  35  * In addition, for many tests I need to be able to put the task into a specific
  36  * state and then check properties. For example, put the task into the
  37  * scheduled state, then check whether listeners have fired. Or put the task
  38  * into the cancelled state after it had been in the running state, and then
  39  * check some properties.
  40  * <p>
  41  * Because we will actually run the task in some background thread some times,
  42  * but will run the task sequentially in other tests, we need to have a
  43  * mechanism where either can happen. What we will do is use a semaphore for
  44  * each state. As the state is entered, we will give back a permit for that
  45  * state. Any code wishing to pick up at the right point can then just acquire
  46  * the permit to wait for the state to occur and then join.
  47  */
  48 public abstract class AbstractTask extends Task<String> {
  49     public final Semaphore scheduledSemaphore = new Semaphore(0);
  50     public final Semaphore runningSemaphore = new Semaphore(0);
  51     public final Semaphore succeededSemaphore = new Semaphore(0);
  52     public final Semaphore cancelledSemaphore = new Semaphore(0);
  53     public final Semaphore failedSemaphore = new Semaphore(0);
  54 
  55     Thread appThread;
  56     ServiceTestBase test;
  57     
  58     // Simulates scheduling the concurrent for execution
  59     public void simulateSchedule() {
  60         setState(State.SCHEDULED);
  61     }
  62 
  63     // For most tests, we want to pretend that we are on the FX app thread, always.
  64     @Override boolean isFxApplicationThread() {
  65         return appThread == null || Thread.currentThread() == appThread;
  66     }
  67 
  68     // For most tests, we want to just run this stuff immediately
  69     @Override void runLater(Runnable r) {
  70         if (test != null) {
  71             test.eventQueue.add(r);
  72         } else {
  73             r.run();
  74         }
  75     }
  76 
  77     @Override protected void scheduled() {
  78         scheduledSemaphore.release();
  79     }
  80 
  81     @Override protected void running() {
  82         runningSemaphore.release();
  83     }
  84 
  85     @Override protected void succeeded() {
  86         succeededSemaphore.release();
  87     }
  88 
  89     @Override protected void cancelled() {
  90         cancelledSemaphore.release();
  91     }
  92 
  93     @Override protected void failed() {
  94         failedSemaphore.release();




  95     }
  96 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.javafx.concurrent;
  27 
  28 import java.util.concurrent.Semaphore;
  29 import javafx.concurrent.Task;
  30 import javafx.concurrent.TaskShim;
  31 
  32 /**
  33  * For testing purposes, we use this subclass of Task that will fake out the
  34  * runLater and isFxApplicationThread calls, such that we can actually run
  35  * and test Task in a single-threaded manner.
  36  * <p>
  37  * In addition, for many tests I need to be able to put the task into a specific
  38  * state and then check properties. For example, put the task into the
  39  * scheduled state, then check whether listeners have fired. Or put the task
  40  * into the cancelled state after it had been in the running state, and then
  41  * check some properties.
  42  * <p>
  43  * Because we will actually run the task in some background thread some times,
  44  * but will run the task sequentially in other tests, we need to have a
  45  * mechanism where either can happen. What we will do is use a semaphore for
  46  * each state. As the state is entered, we will give back a permit for that
  47  * state. Any code wishing to pick up at the right point can then just acquire
  48  * the permit to wait for the state to occur and then join.
  49  */
  50 public abstract class AbstractTask extends TaskShim<String> {
  51     public final Semaphore scheduledSemaphore = new Semaphore(0);
  52     public final Semaphore runningSemaphore = new Semaphore(0);
  53     public final Semaphore succeededSemaphore = new Semaphore(0);
  54     public final Semaphore cancelledSemaphore = new Semaphore(0);
  55     public final Semaphore failedSemaphore = new Semaphore(0);
  56 
  57     Thread appThread;
  58     ServiceTestBase test;
  59     
  60     // Simulates scheduling the concurrent for execution
  61     public void simulateSchedule() {
  62         shim_setState(State.SCHEDULED);
  63     }
  64 
  65     // For most tests, we want to pretend that we are on the FX app thread, always.
  66     @Override public boolean isFxApplicationThread() {
  67         return appThread == null || Thread.currentThread() == appThread;
  68     }
  69 
  70     // For most tests, we want to just run this stuff immediately
  71     @Override public void runLater(Runnable r) {
  72         if (test != null) {
  73             test.eventQueue.add(r);
  74         } else {
  75             r.run();
  76         }
  77     }
  78 
  79     @Override protected void scheduled() {
  80         scheduledSemaphore.release();
  81     }
  82 
  83     @Override protected void running() {
  84         runningSemaphore.release();
  85     }
  86 
  87     @Override protected void succeeded() {
  88         succeededSemaphore.release();
  89     }
  90 
  91     @Override protected void cancelled() {
  92         cancelledSemaphore.release();
  93     }
  94 
  95     @Override protected void failed() {
  96         failedSemaphore.release();
  97     }
  98 
  99     public ServiceTestBase set_test(ServiceTestBase v) {
 100         return test = v;
 101     }
 102 }