1 /*
   2  * Copyright (c) 2010, 2014, 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.  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 javafx.beans.property.ObjectProperty;
  29 import javafx.beans.property.SimpleObjectProperty;
  30 import javafx.concurrent.mocks.SimpleTask;
  31 import java.util.concurrent.CountDownLatch;
  32 import java.util.concurrent.Executor;
  33 import java.util.concurrent.atomic.AtomicBoolean;
  34 import org.junit.Before;
  35 import org.junit.Test;
  36 import static org.junit.Assert.assertEquals;
  37 import static org.junit.Assert.assertFalse;
  38 import static org.junit.Assert.assertNull;
  39 import static org.junit.Assert.assertSame;
  40 import static org.junit.Assert.assertTrue;
  41 
  42 /**
  43  */
  44 public class ServiceTest {
  45     private Service<String> service;
  46 
  47     @Before public void setup() {
  48         // I don't use the AbstractService here because I don't want to
  49         // take advantage of the built in executor / threading stuff
  50         service = new Service<String>() {
  51             @Override protected Task<String> createTask() {
  52                 return new SimpleTask();
  53             }
  54 
  55             @Override void checkThread() { }
  56             @Override void runLater(Runnable r) {
  57                 r.run();
  58             }
  59         };
  60     }
  61 
  62     /******************************************************************
  63      * Executor Property                                              *
  64      *****************************************************************/
  65 
  66     /**
  67      * Tests that the executor property is null by default
  68      */
  69     @Test public void executorDefaultsToNull() {
  70         assertNull(service.getExecutor());
  71         assertNull(service.executorProperty().get());
  72     }
  73 
  74     /**
  75      * Tests that you can set the executor. This will set the executor
  76      * to some non-default setting and check that the same instance is
  77      * then set on the service
  78      */
  79     @Test public void executorCanBeSet() {
  80         final Executor e = command -> { };
  81         service.setExecutor(e);
  82         assertSame(e, service.getExecutor());
  83         assertSame(e, service.executorProperty().get());
  84     }
  85 
  86     /**
  87      * Tests that you can bind the executor property of a Service
  88      */
  89     @Test public void executorCanBeBound() {
  90         final Executor e = command -> { };
  91         ObjectProperty<Executor> other = new SimpleObjectProperty<Executor>(e);
  92         service.executorProperty().bind(other);
  93         assertSame(e, service.getExecutor());
  94         assertSame(e, service.executorProperty().get());
  95         other.set(null);
  96         assertNull(service.getExecutor());
  97         assertNull(service.executorProperty().get());
  98     }
  99 
 100     /**
 101      * Tests that if you specify a custom executor, then it is used when
 102      * you attempt to run a service
 103      */
 104     @Test public void executorIsUsed() {
 105         final AtomicBoolean results = new AtomicBoolean(false);
 106         final Executor e = command -> results.set(true);
 107         service.setExecutor(e);
 108         service.start();
 109         assertTrue(results.get());
 110     }
 111 
 112     /******************************************************************
 113      * Test initial values for properties                             *
 114      *****************************************************************/
 115 
 116     @Test public void stateDefaultsTo_READY() {
 117         assertSame(Worker.State.READY, service.getState());
 118         assertSame(Worker.State.READY, service.stateProperty().get());
 119     }
 120 
 121     @Test public void valueDefaultsToNull() {
 122         assertNull(service.getValue());
 123         assertNull(service.valueProperty().get());
 124     }
 125 
 126     @Test public void exceptionDefaultsToNull() {
 127         assertNull(service.getException());
 128         assertNull(service.exceptionProperty().get());
 129     }
 130 
 131     @Test public void workDoneDefaultsTo_NegativeOne() {
 132         assertEquals(-1, service.getWorkDone(), 0);
 133         assertEquals(-1, service.workDoneProperty().get(), 0);
 134     }
 135 
 136     @Test public void totalWorkDefaultsTo_NegativeOne() {
 137         assertEquals(-1, service.getTotalWork(), 0);
 138         assertEquals(-1, service.totalWorkProperty().get(), 0);
 139     }
 140 
 141     @Test public void progressDefaultsTo_NegativeOne() {
 142         assertEquals(-1, service.getProgress(), 0);
 143         assertEquals(-1, service.progressProperty().get(), 0);
 144     }
 145 
 146     @Test public void runningDefaultsToFalse() {
 147         assertFalse(service.isRunning());
 148         assertFalse(service.runningProperty().get());
 149     }
 150 
 151     @Test public void messageDefaultsToEmptyString() {
 152         assertEquals("", service.getMessage());
 153         assertEquals("", service.messageProperty().get());
 154     }
 155 
 156     @Test public void titleDefaultsToEmptyString() {
 157         assertEquals("", service.getTitle());
 158         assertEquals("", service.titleProperty().get());
 159     }
 160 
 161     /******************************************************************
 162      * Test that 32 simultaneous services will run concurrently       *
 163      *****************************************************************/
 164 
 165     // This test should be reliable. Each of the concurrent tasks takes 1 second to complete
 166     // and several micro / milliseconds to get setup and execute. So 2 seconds should be more
 167     // than enough time.
 168     @Test(timeout = 2000) public void testManyServicesRunConcurrently() throws Exception {
 169         final CountDownLatch latch = new CountDownLatch(32);
 170         for (int i=0; i<32; i++) {
 171             Service<Void> s = new Service<Void>() {
 172                 @Override void checkThread() { }
 173                 @Override void runLater(Runnable r) { r.run(); }
 174 
 175                 @Override protected Task<Void> createTask() {
 176                     return new Task<Void>() {
 177                         @Override protected Void call() throws Exception {
 178                             Thread.sleep(1000);
 179                             latch.countDown();
 180                             return null;
 181                         }
 182 
 183                         @Override void runLater(Runnable r) {
 184                             r.run();
 185                         }
 186 
 187                         @Override boolean isFxApplicationThread() {
 188                             return true;
 189                         }
 190                     };
 191 
 192                 }
 193             };
 194             s.start();
 195         }
 196         latch.await();
 197     }
 198 }