From 3b881ade562539697a4d278f0bb03b7185a76b42 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 12 May 2018 11:22:10 +0100
Subject: [PATCH v2 23/28] JOSMTestRules: synchronize worker thread after tests

---
 .../org/openstreetmap/josm/testutils/JOSMTestRules.java     | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
index b24fc3939..ee2e3471d 100644
--- a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
+++ b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
@@ -50,6 +50,8 @@ import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
 import org.openstreetmap.josm.tools.Territories;
 import org.openstreetmap.josm.tools.date.DateUtils;
 
+import org.awaitility.Awaitility;
+
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
 /**
@@ -573,11 +575,11 @@ public class JOSMTestRules implements TestRule {
     @SuppressFBWarnings("DM_GC")
     protected void after() throws ReflectiveOperationException {
         // Sync AWT Thread
-        GuiHelper.runInEDTAndWait(new Runnable() {
-            @Override
-            public void run() {
-            }
-        });
+        GuiHelper.runInEDTAndWait(() -> { });
+        // Sync worker thread
+        final boolean[] queueEmpty = {false};
+        MainApplication.worker.submit(() -> queueEmpty[0] = true);
+        Awaitility.await().forever().until(() -> queueEmpty[0]);
         // Remove all layers
         cleanLayerEnvironment();
         MemoryManagerTest.resetState(allowMemoryManagerLeaks);
@@ -658,6 +660,7 @@ public class JOSMTestRules implements TestRule {
                 if (exception != null) {
                     throw exception;
                 } else {
+                    Logging.debug("Thread state at timeout: {0}", Thread.getAllStackTraces());
                     throw new Exception(MessageFormat.format("Test timed out after {0}ms", timeout));
                 }
             }
-- 
2.11.0

