You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by la...@apache.org on 2023/04/23 04:06:35 UTC

[lucenenet] branch master updated: Fix slow test: use different sleep method if resolution is low (#838)

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

laimis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git


The following commit(s) were added to refs/heads/master by this push:
     new e4da973ec Fix slow test: use different sleep method if resolution is low (#838)
e4da973ec is described below

commit e4da973ecc1b8e5ee85929fc6ffb968db2ba61ca
Author: Laimonas Simutis <la...@gmail.com>
AuthorDate: Sat Apr 22 21:06:29 2023 -0700

    Fix slow test: use different sleep method if resolution is low (#838)
---
 src/Lucene.Net.TestFramework/Index/RandomCodec.cs | 14 +++++++-------
 src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs    | 23 ++++++++++++++++++-----
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/Lucene.Net.TestFramework/Index/RandomCodec.cs b/src/Lucene.Net.TestFramework/Index/RandomCodec.cs
index c6b5a1b30..d3a182818 100644
--- a/src/Lucene.Net.TestFramework/Index/RandomCodec.cs
+++ b/src/Lucene.Net.TestFramework/Index/RandomCodec.cs
@@ -96,10 +96,10 @@ namespace Lucene.Net.Index
                 if (Debugging.AssertsEnabled) Debugging.Assert(previousMappings.Count < 10000, "test went insane");
             }
 
-            //if (LuceneTestCase.VERBOSE)
-            //{
-            Console.WriteLine("RandomCodec.GetPostingsFormatForField(\"" + name + "\") returned '" + codec.Name + "' with underlying type '" + codec.GetType().ToString() + "'.");
-            //}
+            if (LuceneTestCase.Verbose)
+            {
+                Console.WriteLine("RandomCodec.GetPostingsFormatForField(\"" + name + "\") returned '" + codec.Name + "' with underlying type '" + codec.GetType().ToString() + "'.");
+            }
 
             return codec;
         }
@@ -119,10 +119,10 @@ namespace Lucene.Net.Index
                 if (Debugging.AssertsEnabled) Debugging.Assert(previousDVMappings.Count < 10000, "test went insane");
             }
 
-            //if (LuceneTestCase.VERBOSE)
-            //{
+            if (LuceneTestCase.Verbose)
+            {
                 Console.WriteLine("RandomCodec.GetDocValuesFormatForField(\"" + name + "\") returned '" + codec.Name + "' with underlying type '" + codec.GetType().ToString() + "'.");
-            //}
+            }
 
             return codec;
         }
diff --git a/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs b/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
index 5855dc495..642b277fa 100644
--- a/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
+++ b/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
@@ -1,7 +1,7 @@
 // Lucene version compatibility level 4.8.1
-using Lucene.Net.Support.Threading;
 using RandomizedTesting.Generators;
 using System;
+using System.Diagnostics;
 using System.Threading;
 
 namespace Lucene.Net.Facet
@@ -36,10 +36,10 @@ namespace Lucene.Net.Facet
     public class SlowRAMDirectory : RAMDirectory
     {
         private const int IO_SLEEP_THRESHOLD = 50;
-
+        
         internal Random random;
         private int sleepMillis;
-
+        
         public virtual void SetSleepMillis(int sleepMillis)
         {
             this.sleepMillis = sleepMillis;
@@ -80,7 +80,7 @@ namespace Lucene.Net.Facet
 
             try
             {
-                Thread.Sleep(sTime);
+                Sleep(sTime);
             }
             catch (Exception ie) when (ie.IsInterruptedException())
             {
@@ -88,6 +88,19 @@ namespace Lucene.Net.Facet
             }
         }
 
+        // LUCENENET specific - We can't use Thread.Sleep which depends on the clock
+        // interrupt frequency, and that frequency might be too low for low values like 1 millisecond.
+        private static void Sleep(int milliseconds)
+        {
+            long ticks = (long)((Stopwatch.Frequency / (double)1000) * milliseconds); // ticks per millisecond * milliseconds = total delay ticks
+            long initialTick = Stopwatch.GetTimestamp();
+            long targetTick = initialTick + ticks;
+            while (Stopwatch.GetTimestamp() < targetTick)
+            {
+                Thread.SpinWait(1);
+            }
+        }
+
         /// <summary>
         /// Make a private random. </summary>
         internal virtual Random ForkRandom()
@@ -188,7 +201,7 @@ namespace Lucene.Net.Facet
             private readonly IndexOutput io;
             private int numWrote;
             private readonly Random rand;
-
+            
             public SlowIndexOutput(SlowRAMDirectory outerInstance, IndexOutput io)
             {
                 this.outerInstance = outerInstance;