You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2017/11/01 21:02:48 UTC

[cxf] 02/05: Fix randomly failing test

This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch 3.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 99059e0a061fa6125b2d153561587055b11edde0
Author: Daniel Kulp <dk...@apache.org>
AuthorDate: Wed Oct 18 12:29:36 2017 -0400

    Fix randomly failing test
    
    (cherry picked from commit ced2110c9c5f3d6c38ebd8eb3ff97e16af7eefaf)
    
    # Conflicts:
    #	core/src/test/java/org/apache/cxf/workqueue/AutomaticWorkQueueTest.java
---
 .../cxf/workqueue/AutomaticWorkQueueImpl.java      | 21 +++++--------
 .../cxf/workqueue/AutomaticWorkQueueTest.java      | 34 ++++++++++++++++------
 2 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/workqueue/AutomaticWorkQueueImpl.java b/core/src/main/java/org/apache/cxf/workqueue/AutomaticWorkQueueImpl.java
index b6ecd69..1489f6e 100644
--- a/core/src/main/java/org/apache/cxf/workqueue/AutomaticWorkQueueImpl.java
+++ b/core/src/main/java/org/apache/cxf/workqueue/AutomaticWorkQueueImpl.java
@@ -211,22 +211,15 @@ public class AutomaticWorkQueueImpl implements AutomaticWorkQueue {
             }
             mainLock = l;
 
+
             try {
-                //java 5/6
-                addWorkerMethod = ThreadPoolExecutor.class.getDeclaredMethod("addIfUnderMaximumPoolSize",
-                                                                             Runnable.class);
-                addWorkerArgs = new Object[] {null};
-            } catch (Throwable t) {
-                try {
-                    //java 7
-                    addWorkerMethod = ThreadPoolExecutor.class.getDeclaredMethod("addWorker",
-                                                                                 Runnable.class, Boolean.TYPE);
-                    addWorkerArgs = new Object[] {null, Boolean.FALSE};
-                } catch (Throwable t2) {
-                    //nothing we cando
-                }
+                //java 7
+                addWorkerMethod = ThreadPoolExecutor.class.getDeclaredMethod("addWorker",
+                                                                             Runnable.class, Boolean.TYPE);
+                addWorkerArgs = new Object[] {null, Boolean.FALSE};
+            } catch (Throwable t2) {
+                //nothing we cando
             }
-
         }
         return executor;
     }
diff --git a/core/src/test/java/org/apache/cxf/workqueue/AutomaticWorkQueueTest.java b/core/src/test/java/org/apache/cxf/workqueue/AutomaticWorkQueueTest.java
index 3229ef9..49cbbae 100644
--- a/core/src/test/java/org/apache/cxf/workqueue/AutomaticWorkQueueTest.java
+++ b/core/src/test/java/org/apache/cxf/workqueue/AutomaticWorkQueueTest.java
@@ -144,6 +144,15 @@ public class AutomaticWorkQueueTest extends Assert {
         assertEquals(0, workqueue.getSize());
     }
 
+    int numRunning(BlockingWorkItem[] workItems) {
+        int count = 0;
+        for (BlockingWorkItem item : workItems) {
+            if (item.isRunning()) {
+                count++;
+            }
+        }
+        return count;
+    }
     @Test
     @Ignore("The test is failed on openjdk")
     public void testEnqueueImmediate() {
@@ -181,14 +190,22 @@ public class AutomaticWorkQueueTest extends Assert {
                 }
             }
 
-            while (workqueue.getActiveCount() < DEFAULT_HIGH_WATER_MARK) {
+            int max = 0;
+            int numRun = numRunning(workItems);
+            while ((workqueue.getActiveCount() < DEFAULT_HIGH_WATER_MARK 
+                    || numRun < DEFAULT_HIGH_WATER_MARK 
+                    || workqueue.getSize() > 0)
+                && max < 10) {
                 try {
                     Thread.sleep(250);
                 } catch (InterruptedException ex) {
                     // ignore
                 }
+                numRun = numRunning(workItems);
             }
-            
+            numRun = numRunning(workItems);
+            assertEquals(DEFAULT_HIGH_WATER_MARK, numRun);
+
             for (int i = 0; i < DEFAULT_MAX_QUEUE_SIZE; i++) {
                 fillers[i] = new BlockingWorkItem();
                 try {
@@ -198,13 +215,6 @@ public class AutomaticWorkQueueTest extends Assert {
                 }
             }
 
-            // give threads a chance to start executing the work items
-            try {
-                Thread.sleep(250);
-            } catch (InterruptedException ex) {
-                // ignore
-            }
-
             assertTrue(workqueue.toString(), workqueue.isFull());
             assertEquals(workqueue.toString(), DEFAULT_HIGH_WATER_MARK, workqueue.getPoolSize());
             assertEquals(workqueue.toString(), DEFAULT_HIGH_WATER_MARK, workqueue.getActiveCount());
@@ -462,9 +472,12 @@ public class AutomaticWorkQueueTest extends Assert {
     }
 
     public class BlockingWorkItem implements Runnable {
+        volatile boolean running;
+        
         private boolean unblocked;
 
         public void run() {
+            running = true;
             synchronized (this) {
                 while (!unblocked) {
                     try {
@@ -476,6 +489,9 @@ public class AutomaticWorkQueueTest extends Assert {
             }
         }
 
+        boolean isRunning() {
+            return running;
+        }
         void unblock() {
             synchronized (this) {
                 unblocked = true;

-- 
To stop receiving notification emails like this one, please contact
"commits@cxf.apache.org" <co...@cxf.apache.org>.