You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2018/05/22 19:59:15 UTC

[38/50] [abbrv] lucene-solr:jira/solr-11779: Fix test that assumed the absence of thread context switch between calls.

Fix test that assumed the absence of thread context switch between calls.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/7c8fdcd1
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/7c8fdcd1
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/7c8fdcd1

Branch: refs/heads/jira/solr-11779
Commit: 7c8fdcd1b60e6f5e7f9689ae320526fee1bfdb91
Parents: 4603541
Author: Andrzej Bialecki <ab...@apache.org>
Authored: Mon May 21 12:12:14 2018 +0200
Committer: Andrzej Bialecki <ab...@apache.org>
Committed: Mon May 21 12:12:14 2018 +0200

----------------------------------------------------------------------
 .../org/apache/solr/common/util/TimeSource.java | 21 ++++++++++++++++++++
 .../apache/solr/common/util/TestTimeSource.java | 19 ++++++++++--------
 2 files changed, 32 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/7c8fdcd1/solr/solrj/src/java/org/apache/solr/common/util/TimeSource.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/TimeSource.java b/solr/solrj/src/java/org/apache/solr/common/util/TimeSource.java
index d7b5b78..69aa40f 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/TimeSource.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/TimeSource.java
@@ -52,6 +52,12 @@ public abstract class TimeSource {
     }
 
     @Override
+    long[] getTimeAndEpochNs() {
+      long time = getTimeNs();
+      return new long[] {time, time};
+    }
+
+    @Override
     public void sleep(long ms) throws InterruptedException {
       Thread.sleep(ms);
     }
@@ -88,6 +94,12 @@ public abstract class TimeSource {
     }
 
     @Override
+    long[] getTimeAndEpochNs() {
+      long time = getTimeNs();
+      return new long[] {time, epochStart + time - nanoStart};
+    }
+
+    @Override
     public void sleep(long ms) throws InterruptedException {
       Thread.sleep(ms);
     }
@@ -126,6 +138,12 @@ public abstract class TimeSource {
     }
 
     @Override
+    long[] getTimeAndEpochNs() {
+      long time = getTimeNs();
+      return new long[] {time, epochStart + time - nanoStart};
+    }
+
+    @Override
     public void sleep(long ms) throws InterruptedException {
       ms = Math.round((double)ms / multiplier);
       Thread.sleep(ms);
@@ -198,6 +216,9 @@ public abstract class TimeSource {
    */
   public abstract long getEpochTimeNs();
 
+  // for unit testing
+  abstract long[] getTimeAndEpochNs();
+
   /**
    * Sleep according to this source's notion of time. Eg. accelerated time source such as
    * {@link SimTimeSource} will sleep proportionally shorter, according to its multiplier.

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/7c8fdcd1/solr/solrj/src/test/org/apache/solr/common/util/TestTimeSource.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestTimeSource.java b/solr/solrj/src/test/org/apache/solr/common/util/TestTimeSource.java
index a6cfb68..ad38a83 100644
--- a/solr/solrj/src/test/org/apache/solr/common/util/TestTimeSource.java
+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestTimeSource.java
@@ -33,8 +33,13 @@ public class TestTimeSource extends SolrTestCaseJ4 {
   }
 
   private void doTestEpochTime(TimeSource ts) throws Exception {
-    long prevTime = ts.getTimeNs();
-    long prevEpochTime = ts.getEpochTimeNs();
+
+    // XXX the method below doesn't work reliably because
+    // XXX there could be a long thread context switch between these two calls:
+    // long prevTime = ts.getTimeNs();
+    // long prevEpochTime = ts.getEpochTimeNs();
+
+    long[] prevTimeAndEpoch = ts.getTimeAndEpochNs();
     long delta = 500000000; // 500 ms
     long maxDiff = 200000;
     if (ts instanceof TimeSource.SimTimeSource) {
@@ -42,14 +47,12 @@ public class TestTimeSource extends SolrTestCaseJ4 {
     }
     for (int i = 0; i < 10; i++) {
       ts.sleep(500);
-      long curTime = ts.getTimeNs();
-      long curEpochTime = ts.getEpochTimeNs();
-      long diff = prevTime + delta - curTime;
+      long[] curTimeAndEpoch = ts.getTimeAndEpochNs();
+      long diff = prevTimeAndEpoch[0] + delta - curTimeAndEpoch[0];
       assertTrue(ts + " time diff=" + diff, diff < maxDiff);
-      diff = prevEpochTime + delta - curEpochTime;
+      diff = prevTimeAndEpoch[1] + delta - curTimeAndEpoch[1];
       assertTrue(ts + " epochTime diff=" + diff, diff < maxDiff);
-      prevTime = curTime;
-      prevEpochTime = curEpochTime;
+      prevTimeAndEpoch = curTimeAndEpoch;
     }
   }
 }