Skip to content

Commit

Permalink
Fix slow test: use different sleep method if resolution is low (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
laimis authored Apr 23, 2023
1 parent f6b33b9 commit e4da973
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/Lucene.Net.TestFramework/Index/RandomCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ public override PostingsFormat GetPostingsFormatForField(string name)
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;
}
Expand All @@ -119,10 +119,10 @@ public override DocValuesFormat GetDocValuesFormatForField(string name)
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;
}
Expand Down
23 changes: 18 additions & 5 deletions src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -80,14 +80,27 @@ internal virtual void DoSleep(Random random, int length)

try
{
Thread.Sleep(sTime);
Sleep(sTime);
}
catch (Exception ie) when (ie.IsInterruptedException())
{
throw new Util.ThreadInterruptedException(ie);
}
}

// 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()
Expand Down Expand Up @@ -188,7 +201,7 @@ private class SlowIndexOutput : IndexOutput
private readonly IndexOutput io;
private int numWrote;
private readonly Random rand;

public SlowIndexOutput(SlowRAMDirectory outerInstance, IndexOutput io)
{
this.outerInstance = outerInstance;
Expand Down

0 comments on commit e4da973

Please sign in to comment.