Ticket #16010: v2-0026-TestUtils-add-syncEDTAndWorkerThreads.patch

File v2-0026-TestUtils-add-syncEDTAndWorkerThreads.patch, 3.1 KB (added by ris, 3 years ago)
  • test/unit/org/openstreetmap/josm/TestUtils.java

    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; 
    2525import java.util.Collection;
    2626import java.util.Comparator;
    2727import java.util.Objects;
     28import java.util.concurrent.ExecutionException;
     29import java.util.concurrent.ThreadPoolExecutor;
    2830import java.util.stream.Stream;
    2931
    3032import org.junit.Assume;
    import org.openstreetmap.josm.data.osm.OsmUtils; 
    3638import org.openstreetmap.josm.data.osm.Relation;
    3739import org.openstreetmap.josm.data.osm.RelationMember;
    3840import org.openstreetmap.josm.data.osm.Way;
     41import org.openstreetmap.josm.gui.MainApplication;
    3942import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
    4043import org.openstreetmap.josm.gui.progress.CancelHandler;
    4144import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    4245import org.openstreetmap.josm.gui.progress.ProgressTaskId;
     46import org.openstreetmap.josm.gui.util.GuiHelper;
    4347import org.openstreetmap.josm.io.Compression;
    4448import org.openstreetmap.josm.testutils.FakeGraphics;
    4549import org.openstreetmap.josm.tools.JosmRuntimeException;
    public final class TestUtils { 
    458462            fail(e.toString());
    459463        }
    460464    }
     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    }
    461493}