From 5ce0a708f31b3c19d8f332bbcada4655fd0518f1 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 2 Jun 2018 23:09:22 +0100
Subject: [PATCH v2 26/28] TestUtils: add syncEDTAndWorkerThreads
---
test/unit/org/openstreetmap/josm/TestUtils.java | 32 +++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/test/unit/org/openstreetmap/josm/TestUtils.java b/test/unit/org/openstreetmap/josm/TestUtils.java
index 53e995a30..1817db0f8 100644
|
a
|
b
|
import java.util.Arrays;
|
| 25 | 25 | import java.util.Collection; |
| 26 | 26 | import java.util.Comparator; |
| 27 | 27 | import java.util.Objects; |
| | 28 | import java.util.concurrent.ExecutionException; |
| | 29 | import java.util.concurrent.ThreadPoolExecutor; |
| 28 | 30 | import java.util.stream.Stream; |
| 29 | 31 | |
| 30 | 32 | import org.junit.Assume; |
| … |
… |
import org.openstreetmap.josm.data.osm.OsmUtils;
|
| 36 | 38 | import org.openstreetmap.josm.data.osm.Relation; |
| 37 | 39 | import org.openstreetmap.josm.data.osm.RelationMember; |
| 38 | 40 | import org.openstreetmap.josm.data.osm.Way; |
| | 41 | import org.openstreetmap.josm.gui.MainApplication; |
| 39 | 42 | import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor; |
| 40 | 43 | import org.openstreetmap.josm.gui.progress.CancelHandler; |
| 41 | 44 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| 42 | 45 | import org.openstreetmap.josm.gui.progress.ProgressTaskId; |
| | 46 | import org.openstreetmap.josm.gui.util.GuiHelper; |
| 43 | 47 | import org.openstreetmap.josm.io.Compression; |
| 44 | 48 | import org.openstreetmap.josm.testutils.FakeGraphics; |
| 45 | 49 | import org.openstreetmap.josm.tools.JosmRuntimeException; |
| … |
… |
public final class TestUtils {
|
| 458 | 462 | fail(e.toString()); |
| 459 | 463 | } |
| 460 | 464 | } |
| | 465 | |
| | 466 | /** |
| | 467 | * Waits until any asynchronous operations launched by the test on the EDT or worker threads have |
| | 468 | * (almost certainly) completed. |
| | 469 | */ |
| | 470 | public static void syncEDTAndWorkerThreads() { |
| | 471 | boolean workerQueueEmpty = false; |
| | 472 | while (!workerQueueEmpty) { |
| | 473 | try { |
| | 474 | // once our own task(s) have made it to the front of their respective queue(s), |
| | 475 | // they're both executing at the same time and we know there aren't any outstanding |
| | 476 | // worker tasks, then presumably the only way there could be incomplete operations |
| | 477 | // is if the EDT had launched a deferred task to run on itself or perhaps set up a |
| | 478 | // swing timer - neither are particularly common patterns in JOSM (?) |
| | 479 | // |
| | 480 | // there shouldn't be a risk of creating a deadlock in doing this as there shouldn't |
| | 481 | // (...couldn't?) be EDT operations waiting on the results of a worker task. |
| | 482 | workerQueueEmpty = MainApplication.worker.submit( |
| | 483 | () -> GuiHelper.runInEDTAndWaitAndReturn( |
| | 484 | () -> ((ThreadPoolExecutor) MainApplication.worker).getQueue().isEmpty() |
| | 485 | ) |
| | 486 | ).get(); |
| | 487 | } catch (InterruptedException | ExecutionException e) { |
| | 488 | // inconclusive - retry... |
| | 489 | workerQueueEmpty = false; |
| | 490 | } |
| | 491 | } |
| | 492 | } |
| 461 | 493 | } |