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.
|
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.
|