Skip to content

Commit

Permalink
Test review of Store, apache#259
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Jan 25, 2024
1 parent 1f84445 commit 65954a9
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 81 deletions.
48 changes: 25 additions & 23 deletions src/Lucene.Net.Tests/Store/TestBufferedIndexInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ namespace Lucene.Net.Store
[TestFixture]
public class TestBufferedIndexInput : LuceneTestCase
{
private static void WriteBytes(FileInfo aFile, long size)
{
using FileStream ostream = new FileStream(aFile.FullName, FileMode.Create);
for (int i = 0; i < size; i++)
{
ostream.WriteByte(Byten(i));
}
ostream.Flush();
}
// LUCENENET: unused, commenting out until it is needed
// private static void WriteBytes(FileInfo aFile, long size)
// {
// using FileStream ostream = new FileStream(aFile.FullName, FileMode.Create);
// for (int i = 0; i < size; i++)
// {
// ostream.WriteByte(Byten(i));
// }
// ostream.Flush();
// }

private const long TEST_FILE_LENGTH = 100 * 1024;

Expand Down Expand Up @@ -79,17 +80,18 @@ public virtual void TestReadBytes()
RunReadBytes(input, BufferedIndexInput.BUFFER_SIZE, Random);
}

private void RunReadBytesAndClose(IndexInput input, int bufferSize, Random r)
{
try
{
RunReadBytes(input, bufferSize, r);
}
finally
{
input.Dispose();
}
}
// LUCENENET: unused, commenting out until it is needed
// private void RunReadBytesAndClose(IndexInput input, int bufferSize, Random r)
// {
// try
// {
// RunReadBytes(input, bufferSize, r);
// }
// finally
// {
// input.Dispose();
// }
// }

private void RunReadBytes(IndexInput input, int bufferSize, Random r)
{
Expand Down Expand Up @@ -155,7 +157,7 @@ private void CheckReadBytes(IndexInput input, int size, int pos)
Assert.AreEqual(pos + size, input.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
for (int i = 0; i < size; i++)
{
Assert.AreEqual(Byten(pos + i), (byte)buffer[offset + i], "pos=" + i + " filepos=" + (pos + i));
Assert.AreEqual(Byten(pos + i), buffer[offset + i], "pos=" + i + " filepos=" + (pos + i));
}
}

Expand Down Expand Up @@ -262,7 +264,7 @@ public virtual void TestSetBufferSize()
new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))
.SetOpenMode(OpenMode.CREATE)
.SetMergePolicy(NewLogMergePolicy(false)));

for (int i = 0; i < 37; i++)
{
var doc = new Document();
Expand Down Expand Up @@ -387,4 +389,4 @@ public override long FileLength(string name)
}
}
}
}
}
3 changes: 1 addition & 2 deletions src/Lucene.Net.Tests/Store/TestCopyBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NUnit.Framework;
using RandomizedTesting.Generators;
using System;
using System.IO;
using Assert = Lucene.Net.TestFramework.Assert;
using Console = Lucene.Net.Util.SystemConsole;

Expand Down Expand Up @@ -200,4 +199,4 @@ public override void Run()
}
}
}
}
}
21 changes: 16 additions & 5 deletions src/Lucene.Net.Tests/Store/TestDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public class TestDirectory : LuceneTestCase
public virtual void TestDetectClose()
{
DirectoryInfo tempDir = CreateTempDir(GetType().Name);
Directory[] dirs = new Directory[] { new RAMDirectory(), new SimpleFSDirectory(tempDir), new NIOFSDirectory(tempDir) };
Directory[] dirs = new Directory[]
{
new RAMDirectory(),
new SimpleFSDirectory(tempDir),
new NIOFSDirectory(tempDir)
};

foreach (Directory dir in dirs)
{
Expand All @@ -62,7 +67,12 @@ public virtual void TestDetectClose()
public virtual void TestDoubleDispose()
{
DirectoryInfo tempDir = CreateTempDir(GetType().Name);
Directory[] dirs = new Directory[] { new RAMDirectory(), new SimpleFSDirectory(tempDir), new NIOFSDirectory(tempDir) };
Directory[] dirs = new Directory[]
{
new RAMDirectory(),
new SimpleFSDirectory(tempDir),
new NIOFSDirectory(tempDir)
};

foreach (Directory dir in dirs)
{
Expand Down Expand Up @@ -120,7 +130,8 @@ public override void Run()

try
{
using (IndexOutput output = outerBDWrapper.CreateOutput(fileName, NewIOContext(Random))) { }
// LUCENENET: using statement instead of manual close/Dispose call
using (IndexOutput _ = outerBDWrapper.CreateOutput(fileName, NewIOContext(Random))) { }
Assert.IsTrue(SlowFileExists(outerBDWrapper, fileName));
}
catch (Exception e) when (e.IsIOException())
Expand Down Expand Up @@ -347,7 +358,7 @@ public virtual void TestCopySubdir()
//(new File(path, "subdir")).mkdirs();
System.IO.Directory.CreateDirectory(new DirectoryInfo(Path.Combine(path.FullName, "subdir")).FullName);
Directory fsDir = new SimpleFSDirectory(path, null);
Assert.AreEqual(0, (new RAMDirectory(fsDir, NewIOContext(Random))).ListAll().Length);
Assert.AreEqual(0, new RAMDirectory(fsDir, NewIOContext(Random)).ListAll().Length);
}
finally
{
Expand Down Expand Up @@ -526,4 +537,4 @@ private static void Search(Index.IndexReader r, int times)
}
}
}
}
}
8 changes: 5 additions & 3 deletions src/Lucene.Net.Tests/Store/TestFileSwitchDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public virtual void TestBasic()
// for now we wire Lucene40Codec because we rely upon its specific impl
bool oldValue = OldFormatImpersonationIsActive;
OldFormatImpersonationIsActive = true;
IndexWriter writer = new IndexWriter(fsd, (new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))).SetMergePolicy(NewLogMergePolicy(false)).SetCodec(Codec.ForName("Lucene40")).SetUseCompoundFile(false));
IndexWriter writer = new IndexWriter(fsd,
new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))
.SetMergePolicy(NewLogMergePolicy(false)).SetCodec(Codec.ForName("Lucene40")).SetUseCompoundFile(false));
TestIndexWriterReader.CreateIndexNoClose(true, "ram", writer);
IndexReader reader = DirectoryReader.Open(writer, true);
Assert.AreEqual(100, reader.MaxDoc);
Expand Down Expand Up @@ -136,7 +138,7 @@ private static bool ContainsFile(Directory directory, string file) // LUCENENET
public virtual void TestDirectoryFilter()
{
Directory dir = NewFSSwitchDirectory(Collections.EmptySet<string>());
string name = "file";
const string name = "file"; // LUCENENET: made const
try
{
dir.CreateOutput(name, NewIOContext(Random)).Dispose();
Expand Down Expand Up @@ -187,4 +189,4 @@ private void CreateSequenceFile(Directory dir, string name, sbyte start, int siz
os.Dispose();
}
}
}
}
5 changes: 3 additions & 2 deletions src/Lucene.Net.Tests/Store/TestHugeRamFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace Lucene.Net.Store
[TestFixture]
public class TestHugeRamFile : LuceneTestCase
{
private static readonly long MAX_VALUE = (long)2 * (long)int.MaxValue;
// LUCENENET: made const, using long literal instead of cast to long
private const long MAX_VALUE = 2L * int.MaxValue;

/// <summary>
/// Fake a huge ram file by using the same byte buffer for all
Expand Down Expand Up @@ -121,4 +122,4 @@ public virtual void TestHugeFile()
}
}
}
}
}
12 changes: 2 additions & 10 deletions src/Lucene.Net.Tests/Store/TestLock.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using NUnit.Framework;
using System;
using System.IO;
using Assert = Lucene.Net.TestFramework.Assert;

namespace Lucene.Net.Store
Expand Down Expand Up @@ -30,7 +29,7 @@ public class TestLock : LuceneTestCase
[Test]
public virtual void TestObtain()
{
LockMock @lock = new LockMock(this);
LockMock @lock = new LockMock();
Lock.LOCK_POLL_INTERVAL = 10;

try
Expand All @@ -46,13 +45,6 @@ public virtual void TestObtain()

private class LockMock : Lock
{
private readonly TestLock outerInstance;

public LockMock(TestLock outerInstance)
{
this.outerInstance = outerInstance;
}

public int LockAttempts;

public override bool Obtain()
Expand All @@ -72,4 +64,4 @@ public override bool IsLocked()
}
}
}
}
}
34 changes: 8 additions & 26 deletions src/Lucene.Net.Tests/Store/TestLockFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using J2N.Threading;
using Lucene.Net.Documents;
using Lucene.Net.Index.Extensions;
using Lucene.Net.Support.Threading;
using NUnit.Framework;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -52,7 +51,7 @@ public class TestLockFactory : LuceneTestCase
public virtual void TestCustomLockFactory()
{
Directory dir = new MockDirectoryWrapper(Random, new RAMDirectory());
MockLockFactory lf = new MockLockFactory(this);
MockLockFactory lf = new MockLockFactory();
dir.SetLockFactory(lf);

// Lock prefix should have been set:
Expand Down Expand Up @@ -175,12 +174,12 @@ public virtual void _testStressLocks(LockFactory lockFactory, DirectoryInfo inde
Directory dir = NewFSDirectory(indexDir, lockFactory);

// First create a 1 doc index:
IndexWriter w = new IndexWriter(dir, (new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))).SetOpenMode(OpenMode.CREATE));
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetOpenMode(OpenMode.CREATE));
AddDoc(w);
w.Dispose();

WriterThread writer = new WriterThread(this, 100, dir);
SearcherThread searcher = new SearcherThread(this, 100, dir);
SearcherThread searcher = new SearcherThread(100, dir);
writer.Start();
searcher.Start();

Expand Down Expand Up @@ -233,7 +232,7 @@ public virtual void TestNativeFSLockFactoryLockExists()
var lockFile = new FileInfo(Path.Combine(tempDir.FullName, "test.lock"));
using (lockFile.Create()){};

var l = (new NativeFSLockFactory(tempDir)).MakeLock("test.lock");
var l = new NativeFSLockFactory(tempDir).MakeLock("test.lock");
Assert.IsTrue(l.Obtain(), "failed to obtain lock, got exception: {0}", l.FailureReason);
l.Dispose();
Assert.IsFalse(l.IsLocked(), "failed to release lock, got exception: {0}", l.FailureReason);
Expand Down Expand Up @@ -310,7 +309,7 @@ public override void Run()
{
try
{
writer = new IndexWriter(dir, (new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))).SetOpenMode(OpenMode.APPEND));
writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetOpenMode(OpenMode.APPEND));
}
catch (Exception e) when (e.IsIOException())
{
Expand Down Expand Up @@ -369,15 +368,12 @@ public override void Run()

private class SearcherThread : ThreadJob
{
private readonly TestLockFactory outerInstance;

private readonly Directory dir;
private readonly int numIteration;
public bool HitException { get; private set; } = false;

public SearcherThread(TestLockFactory outerInstance, int numIteration, Directory dir)
public SearcherThread(int numIteration, Directory dir)
{
this.outerInstance = outerInstance;
this.numIteration = numIteration;
this.dir = dir;
}
Expand Down Expand Up @@ -430,13 +426,6 @@ public override void Run()

public class MockLockFactory : LockFactory
{
private readonly TestLockFactory outerInstance;

public MockLockFactory(TestLockFactory outerInstance)
{
this.outerInstance = outerInstance;
}

public bool LockPrefixSet;
public IDictionary<string, Lock> LocksCreated = /*CollectionsHelper.SynchronizedMap(*/new Dictionary<string, Lock>()/*)*/;
public int MakeLockCount = 0;
Expand All @@ -454,7 +443,7 @@ public override Lock MakeLock(string lockName)
{
lock (this)
{
Lock @lock = new MockLock(this);
Lock @lock = new MockLock();
LocksCreated[lockName] = @lock;
MakeLockCount++;
return @lock;
Expand All @@ -467,13 +456,6 @@ public override void ClearLock(string specificLockName)

public class MockLock : Lock
{
private readonly TestLockFactory.MockLockFactory outerInstance;

public MockLock(TestLockFactory.MockLockFactory outerInstance)
{
this.outerInstance = outerInstance;
}

public int LockAttempts;

public override bool Obtain()
Expand Down Expand Up @@ -501,4 +483,4 @@ private void AddDoc(IndexWriter writer)
writer.AddDocument(doc);
}
}
}
}
3 changes: 1 addition & 2 deletions src/Lucene.Net.Tests/Store/TestMockDirectoryWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using NUnit.Framework;
using System;
using System.IO;
using Assert = Lucene.Net.TestFramework.Assert;

namespace Lucene.Net.Store
Expand Down Expand Up @@ -111,4 +110,4 @@ public void TestDiskFull()
dir.Dispose();
}
}
}
}
4 changes: 2 additions & 2 deletions src/Lucene.Net.Tests/Store/TestMultiMMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,12 @@ public void TestDisposeIndexInput()
File.WriteAllText(fileName, string.Empty, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) /* No BOM */);

MMapDirectory mmapDir = new MMapDirectory(dir);
using (var indexInput = mmapDir.OpenInput(name, NewIOContext(Random)))
using (var _ = mmapDir.OpenInput(name, NewIOContext(Random)))
{
} // Dispose

// Now it should be possible to delete the file. This is the condition we are testing for.
File.Delete(fileName);
}
}
}
}
4 changes: 2 additions & 2 deletions src/Lucene.Net.Tests/Store/TestNRTCachingDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public virtual void VerifyCompiles()
Directory fsDir = FSDirectory.Open(new DirectoryInfo("/path/to/index"));
NRTCachingDirectory cachedFSDir = new NRTCachingDirectory(fsDir, 2.0, 25.0);
IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
IndexWriter writer = new IndexWriter(cachedFSDir, conf);
IndexWriter _ = new IndexWriter(cachedFSDir, conf); // LUCENENET: discarding unused variable, was `writer`
}

[Test]
Expand Down Expand Up @@ -232,4 +232,4 @@ private void CreateSequenceFile(Directory dir, string name, sbyte start, int siz
os.Dispose();
}
}
}
}
Loading

0 comments on commit 65954a9

Please sign in to comment.