Fix for race in TestAwtPanels

So I discovered a race condition in TestAwtPanels. The proposed fix affects one file:

jmonkeyengine/jme3-examples/src/main/java/jme3test/awt/TestAwtPanels.java

and here is the diff:

--- HEAD
+++ Modified In Working Tree
@@ -14,6 +14,7 @@
 import java.awt.Toolkit;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
+import java.util.concurrent.CountDownLatch;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.swing.JFrame;
@@ -21,6 +22,7 @@
 
 public class TestAwtPanels extends SimpleApplication {
 
+    final private static CountDownLatch panelsAreReady = new CountDownLatch(1);
     private static TestAwtPanels app;
     private static AwtPanel panel, panel2;
     private static int panelsClosed = 0;
@@ -56,6 +58,16 @@
         
         SwingUtilities.invokeLater(new Runnable(){
             public void run(){
+                /*
+                 * Sleep 2 seconds to ensure there's no race condition.
+                 * The sleep is not required for correctness.
+                 */
+                try {
+                    Thread.sleep(2000);
+                } catch (InterruptedException exception) {
+                    return;
+                }
+
                 final AwtPanelsContext ctx = (AwtPanelsContext) app.getContext();
                 panel = ctx.createPanel(PaintMode.Accelerated);
                 panel.setPreferredSize(new Dimension(400, 300));
@@ -66,6 +78,10 @@
                 
                 createWindowForPanel(panel, 300);
                 createWindowForPanel(panel2, 700);
+                /*
+                 * Both panels are ready.
+                 */
+                panelsAreReady.countDown();
             }
         });
     }
@@ -80,7 +96,15 @@
         mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
         geom.setMaterial(mat);
         rootNode.attachChild(geom);
-        
+        /*
+         * Wait until both AWT panels are ready.
+         */
+        try {
+            panelsAreReady.await();
+        } catch (InterruptedException exception) {
+            throw new RuntimeException("Interrupted while waiting for panels", exception);
+        }
+
\ No newline at end of file
         panel.attachTo(true, viewPort);
         guiViewPort.setClearFlags(true, true, true);
         panel2.attachTo(false, guiViewPort);

Do I have permission to push this change to ‘master’?

2 Likes

Looks good to me.

1 Like

Pushed!