1 /*
   2  * Copyright (c) 2010, 2013, 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 test.javafx.concurrent;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import java.util.concurrent.CyclicBarrier;
  31 import javafx.concurrent.Task;
  32 
  33 import org.junit.Before;
  34 import org.junit.Test;
  35 
  36 import static org.junit.Assert.*;
  37 
  38 /**
  39  * In this test, the Task is going to attempt to totally swamp the
  40  * Event Queue by posting message updates very rapidly (that is,
  41  * the background threaded code isn't going to worry about it,
  42  * it is just going to send progress updates thousands of times
  43  * each second, and it is up to the Task implementation to
  44  * coalesce these down into a very sustainable number of entries
  45  * on the event queue. Basically, there should only ever be one
  46  * event on the event queue which gets the most recent value).
  47  */
  48 public class TaskSwampEventQueueTest {
  49     private CyclicBarrier barrier;
  50     private List<Runnable> eventQueue;
  51     private Task task;
  52     private Thread th;
  53 
  54     @Before public void setup() {
  55         barrier = new CyclicBarrier(2);
  56         eventQueue = new ArrayList<>();
  57         task = new AbstractTask() {
  58             @Override protected String call() throws Exception {
  59                 for (int i=0; i<1000; i++) {
  60                     updateProgress(i, 2000);
  61                 }
  62                 barrier.await(); // I will wait here until the test code is read
  63                 barrier.await(); // I will wait here until the test code tells me to continue
  64                 for (int i=1000; i<=2000; i++) {
  65                     updateProgress(i, 2000);
  66                 }
  67                 barrier.await(); // I'm done basically
  68                 return "Sentinel";
  69             }
  70 
  71             @Override public boolean isFxApplicationThread() {
  72                 return Thread.currentThread() != th;
  73             }
  74 
  75             @Override public void runLater(Runnable r) {
  76                 eventQueue.add(r);
  77             }
  78         };
  79     }
  80 
  81     @Test public void numberOfEventsOnTheEventQueueShouldNeverBeLarge() throws Exception {
  82         th = new Thread(task);
  83         th.start();
  84 
  85         barrier.await();
  86         // There may actually 2 runnables on the queue, the first is the one that updates
  87         // the "state" of the Task, and the second is the progress update.
  88         assertTrue(eventQueue.size() == 2 || eventQueue.size() == 1);
  89         while (eventQueue.size() > 0) eventQueue.remove(0).run();
  90         assertEquals(1000 - 1, task.getWorkDone(), 0);
  91         assertEquals(2000, task.getTotalWork(), 0);
  92         barrier.await();
  93         barrier.await();
  94         // There may be another 2 runnables on the queue, the first is the progress update,
  95         // the second sets the value & updates the state of the Task.
  96         assertTrue(eventQueue.size() == 2 || eventQueue.size() == 1);
  97         while (eventQueue.size() > 0) eventQueue.remove(0).run();
  98         assertEquals(2000, task.getWorkDone(), 0);
  99         assertEquals(2000, task.getTotalWork(), 0);
 100     }
 101 }