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