You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2019/12/17 20:54:44 UTC

[lucene-solr] branch master updated: Harden (Cloud)ExitableDirectoryReaderTest

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

hossman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/master by this push:
     new 8493cf1  Harden (Cloud)ExitableDirectoryReaderTest
8493cf1 is described below

commit 8493cf18cbfcbf020c467737e28c55e4b05c60f9
Author: Chris Hostetter <ho...@apache.org>
AuthorDate: Tue Dec 17 13:54:33 2019 -0700

    Harden (Cloud)ExitableDirectoryReaderTest
    
    Thread.sleep() is "subject to the precision and accuracy of system timers and schedulers."
    
    But tests using DelayingSearchComponent need to ensure that it sleeps *at least* as long as they request, in order to trigger the timeAllowed constraint
---
 .../solr/search/DelayingSearchComponent.java       | 31 ++++++++++++++++------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/solr/core/src/test/org/apache/solr/search/DelayingSearchComponent.java b/solr/core/src/test/org/apache/solr/search/DelayingSearchComponent.java
index eb64063..b7e8e4c 100644
--- a/solr/core/src/test/org/apache/solr/search/DelayingSearchComponent.java
+++ b/solr/core/src/test/org/apache/solr/search/DelayingSearchComponent.java
@@ -16,11 +16,12 @@
  */
 package org.apache.solr.search;
 
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.solr.handler.component.ResponseBuilder;
 import org.apache.solr.handler.component.SearchComponent;
 
-import java.io.IOException;
-
 /**
  * Search component used to add delay to each request.
  */
@@ -33,13 +34,27 @@ public class DelayingSearchComponent extends SearchComponent{
 
   @Override
   public void process(ResponseBuilder rb) throws IOException {
-    int sleep = rb.req.getParams().getInt("sleep",0);
-    try {
-      if (sleep > 0) {
-        Thread.sleep(sleep);
+    final long totalSleepMillis = rb.req.getParams().getLong("sleep",0);
+    if (totalSleepMillis > 0) {
+      final long totalSleepNanos = TimeUnit.NANOSECONDS.convert(totalSleepMillis, TimeUnit.MILLISECONDS);
+      final long startNanos = System.nanoTime();
+      try {
+        // Thread.sleep() (and derivatives) are not garunteed to sleep the full amount:
+        //   "subject to the precision and accuracy of system timers and schedulers."
+        // This is particularly problematic on Windows VMs, so we do a retry loop
+        // to ensure we sleep a total of at least as long as requested
+        //
+        // (Tests using this component do so explicitly to ensure 'timeAllowed'
+        // has exceeded in order to get their expected results, we would rather over-sleep
+        // then under sleep)
+        for (long sleepNanos = totalSleepNanos;
+             0 < sleepNanos;
+             sleepNanos = totalSleepNanos - (System.nanoTime() - startNanos)) {
+          TimeUnit.NANOSECONDS.sleep(sleepNanos);
+        }
+      } catch (InterruptedException e) {
+        // Do nothing?
       }
-    } catch (InterruptedException e) {
-      // Do nothing?
     }
   }