You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2015/12/24 18:54:40 UTC

[3/6] jena git commit: JENA-1106: Use jayway.awaitility for alarm clock tests

JENA-1106: Use jayway.awaitility for alarm clock tests


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/87f85a05
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/87f85a05
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/87f85a05

Branch: refs/heads/master
Commit: 87f85a05900ecc2de7aa26fcda8182ed67f34d6c
Parents: 793584c
Author: Andy Seaborne <an...@apache.org>
Authored: Thu Dec 24 17:07:52 2015 +0000
Committer: Andy Seaborne <an...@apache.org>
Committed: Thu Dec 24 17:18:47 2015 +0000

----------------------------------------------------------------------
 jena-base/pom.xml                               | 25 +++++-----
 .../apache/jena/atlas/lib/TestAlarmClock.java   | 52 +++++++++++---------
 jena-parent/pom.xml                             | 10 ++--
 3 files changed, 49 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/87f85a05/jena-base/pom.xml
----------------------------------------------------------------------
diff --git a/jena-base/pom.xml b/jena-base/pom.xml
index 5c20a79..353ffb6 100644
--- a/jena-base/pom.xml
+++ b/jena-base/pom.xml
@@ -35,35 +35,38 @@
   <dependencies>
     
     <dependency>
-      <groupId>junit</groupId>
-      <artifactId>junit</artifactId>
-      <scope>test</scope>
-    </dependency>
-    
-    <dependency>
       <groupId>org.apache.jena</groupId>
       <artifactId>jena-shaded-guava</artifactId>
       <version>3.1.0-SNAPSHOT</version>
     </dependency>
 
-    <!-- Remove when jena-base is used -->
     <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-csv</artifactId>
     </dependency> 
-    
+
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+    </dependency>
+
     <!-- supports persistent data structures -->
     <dependency>
       <groupId>com.github.andrewoma.dexx</groupId>
       <artifactId>dexx-collections</artifactId>
     </dependency>
     
-    <!-- Remove when jena-base is used -->
     <dependency>
-      <groupId>org.apache.commons</groupId>
-      <artifactId>commons-lang3</artifactId>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
     </dependency>
 
+    <dependency>
+      <groupId>com.jayway.awaitility</groupId>
+      <artifactId>awaitility</artifactId>
+      <scope>test</scope>
+    </dependency>    
     
   </dependencies>
 

http://git-wip-us.apache.org/repos/asf/jena/blob/87f85a05/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java
----------------------------------------------------------------------
diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java
index 8cee5f2..50037d0 100644
--- a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java
+++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java
@@ -18,19 +18,21 @@
 
 package org.apache.jena.atlas.lib ;
 
+import static com.jayway.awaitility.Awaitility.await ;
 import static org.apache.jena.atlas.lib.Lib.sleep ;
 
+import java.util.concurrent.TimeUnit ;
+import static java.util.concurrent.TimeUnit.* ;
 import java.util.concurrent.atomic.AtomicInteger ;
 
 import org.apache.jena.atlas.junit.BaseTest ;
-import org.apache.jena.base.Sys ;
 import org.junit.Test ;
 
 public class TestAlarmClock extends BaseTest {
     /* Issues with MS Windows.
      * 
      * Running some of these tests on windows is unreliable; sometimes they pass,
-     * sometimes one fails.
+     * sometimes one or more fails.
      *  
      * This seems to be that when the CI server (ASF Jenkins, Windows VM)
      * is under load then the ScheduledThreadPoolExecutor used by AlarmClock 
@@ -39,24 +41,13 @@ public class TestAlarmClock extends BaseTest {
      * But setting the times so high for this slows the tests down a lot
      * and makes some of them fairly pointless.
      * 
-     * alarm_03 is very sensitive.  A sleep of 200 is still not stable
-     * the callback is not called (10ms callback).  It usually passses if there is
-     * no other job on the machines, otherwise it fails >50% of the time.
-     * 
-     * Failures are masking the success/failure of unrelated development changes.
-     * 
-     * So skip some tests on windows.  
+     * The use of awaitility helps this - the timeouts can be set quite long
+     * and the polling done means the full wait time does not happen normally.  
      */
 
     private AtomicInteger count    = new AtomicInteger(0) ;
     private Runnable      callback = ()->count.getAndIncrement() ;
     
-    // Loaded CI.
-    private static boolean mayBeErratic = Sys.isWindows ;
-    
-    private int timeout(int time1, int time2) {
-        return mayBeErratic ? time2 : time1 ;
-    }
     @Test
     public void alarm_01() {
         AlarmClock alarmClock = new AlarmClock() ;
@@ -67,13 +58,22 @@ public class TestAlarmClock extends BaseTest {
         alarmClock.release() ;
     }
 
+    private void awaitUntil(int value, long timePeriod, TimeUnit units) {
+        await()
+        .atMost(timePeriod, units)
+        .until(() -> {
+            return count.get() == value ;
+        }) ;
+    }
+    
     @Test
     public void alarm_02() {
         AlarmClock alarmClock = new AlarmClock() ;
         // Short - happens.
         Alarm a = alarmClock.add(callback, 10) ;
-        sleep(timeout(100, 250)) ;
-        assertEquals(1, count.get()) ;
+        
+        awaitUntil(1, 500, MILLISECONDS) ;
+        
         // try to cancel anyway.
         alarmClock.cancel(a) ;
         alarmClock.release() ;
@@ -84,9 +84,9 @@ public class TestAlarmClock extends BaseTest {
         AlarmClock alarmClock = new AlarmClock() ;
         Alarm a1 = alarmClock.add(callback, 10) ;
         Alarm a2 = alarmClock.add(callback, 1000000) ;
-        sleep(timeout(100, 300)) ;
-        // ping1 went off.
-        assertEquals(1, count.get()) ;
+        
+        awaitUntil(1, 500, MILLISECONDS) ;
+        
         alarmClock.cancel(a2) ;
         alarmClock.release() ;
     }
@@ -96,9 +96,9 @@ public class TestAlarmClock extends BaseTest {
         AlarmClock alarmClock = new AlarmClock() ;
         Alarm a1 = alarmClock.add(callback, 10) ;
         Alarm a2 = alarmClock.add(callback, 20) ;
-        sleep(timeout(150, 300)) ;
-        // ping1 went off. ping2 went off.
-        assertEquals(2, count.get()) ;
+        
+        awaitUntil(2, 500, MILLISECONDS) ;
+
         alarmClock.release() ;
     }
 
@@ -107,7 +107,11 @@ public class TestAlarmClock extends BaseTest {
         AlarmClock alarmClock = new AlarmClock() ;
         Alarm a = alarmClock.add(callback, 50) ;
         alarmClock.reset(a, 20000) ;
-        sleep(timeout(100, 250)) ;
+        
+        sleep(150) ;
+        
+        // Did not go off.
+        assertEquals(0, count.get()) ;
         alarmClock.cancel(a);
         alarmClock.release() ;
     }

http://git-wip-us.apache.org/repos/asf/jena/blob/87f85a05/jena-parent/pom.xml
----------------------------------------------------------------------
diff --git a/jena-parent/pom.xml b/jena-parent/pom.xml
index 1a5c349..c03ad54 100644
--- a/jena-parent/pom.xml
+++ b/jena-parent/pom.xml
@@ -70,6 +70,9 @@
     <ver.solr>4.9.1</ver.solr>
     <ver.spatial4j>0.4.1</ver.spatial4j>
 
+    <ver.mockito>1.9.5</ver.mockito>
+    <ver.awaitility>1.6.4</ver.awaitility>
+
     <jdk.version>1.8</jdk.version>
     <targetJdk>${jdk.version}</targetJdk> <!-- MPMD-86 workaround -->
 
@@ -356,13 +359,14 @@
       <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-all</artifactId>
-        <version>1.9.5</version>
+        <version>${ver.mockito}</version>
         <scope>test</scope>
       </dependency>
-      <dependency>
+ 
+     <dependency>
         <groupId>com.jayway.awaitility</groupId>
         <artifactId>awaitility</artifactId>
-        <version>1.6.4</version>
+        <version>${ver.awaitility}</version>
       </dependency>
     </dependencies>