Skip to content

Commit

Permalink
Test review of TestSnapshotDeletionPolicy; revert SetUp change on Tes…
Browse files Browse the repository at this point in the history
…tSegmentTermDocs and add comment
  • Loading branch information
paulirwin committed Mar 10, 2024
1 parent 74d4272 commit ce0c5b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/Lucene.Net.Tests/Index/TestSegmentTermDocs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ namespace Lucene.Net.Index
[TestFixture]
public class TestSegmentTermDocs : LuceneTestCase
{
private Document testDoc = new Document();
private Document testDoc; // LUCENENET: = new Document(); moved to SetUp
private Directory dir;
private SegmentCommitInfo info;

[SetUp]
public override void SetUp()
{
base.SetUp();
testDoc = new Document();
dir = NewDirectory();
DocHelper.SetupDoc(testDoc);
info = DocHelper.WriteDoc(Random, dir, testDoc);
Expand Down
53 changes: 22 additions & 31 deletions src/Lucene.Net.Tests/Index/TestSnapshotDeletionPolicy.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 @@ -44,9 +43,9 @@ namespace Lucene.Net.Index
[TestFixture]
public class TestSnapshotDeletionPolicy : LuceneTestCase
{
public const string INDEX_PATH = "test.snapshots";
// public const string INDEX_PATH = "test.snapshots"; // LUCENENET: unused constant

protected internal virtual IndexWriterConfig GetConfig(Random random, IndexDeletionPolicy dp)
protected IndexWriterConfig GetConfig(Random random, IndexDeletionPolicy dp)
{
IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
if (dp != null)
Expand All @@ -56,13 +55,13 @@ protected internal virtual IndexWriterConfig GetConfig(Random random, IndexDelet
return conf;
}

protected internal virtual void CheckSnapshotExists(Directory dir, IndexCommit c)
protected void CheckSnapshotExists(Directory dir, IndexCommit c)
{
string segFileName = c.SegmentsFileName;
Assert.IsTrue(SlowFileExists(dir, segFileName), "segments file not found in directory: " + segFileName);
}

protected internal virtual void CheckMaxDoc(IndexCommit commit, int expectedMaxDoc)
protected void CheckMaxDoc(IndexCommit commit, int expectedMaxDoc)
{
IndexReader reader = DirectoryReader.Open(commit);
try
Expand All @@ -75,7 +74,9 @@ protected internal virtual void CheckMaxDoc(IndexCommit commit, int expectedMaxD
}
}

protected internal virtual void PrepareIndexAndSnapshots(SnapshotDeletionPolicy sdp, IndexWriter writer, int numSnapshots)
protected IList<IndexCommit> snapshots; // LUCENENET: = new JCG.List<IndexCommit>(); moved to SetUp

protected void PrepareIndexAndSnapshots(SnapshotDeletionPolicy sdp, IndexWriter writer, int numSnapshots)
{
for (int i = 0; i < numSnapshots; i++)
{
Expand All @@ -86,9 +87,9 @@ protected internal virtual void PrepareIndexAndSnapshots(SnapshotDeletionPolicy
}
}

protected internal virtual SnapshotDeletionPolicy DeletionPolicy => new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
protected SnapshotDeletionPolicy DeletionPolicy => new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());

protected internal virtual void AssertSnapshotExists(Directory dir, SnapshotDeletionPolicy sdp, int numSnapshots, bool checkIndexCommitSame)
protected void AssertSnapshotExists(Directory dir, SnapshotDeletionPolicy sdp, int numSnapshots, bool checkIndexCommitSame)
{
for (int i = 0; i < numSnapshots; i++)
{
Expand All @@ -106,8 +107,6 @@ protected internal virtual void AssertSnapshotExists(Directory dir, SnapshotDele
}
}

protected internal IList<IndexCommit> snapshots;

[SetUp]
public override void SetUp()
{
Expand All @@ -130,7 +129,9 @@ private void RunTest(Random random, Directory dir)
long stopTime = (J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) + 1000; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results

SnapshotDeletionPolicy dp = DeletionPolicy;
IndexWriter writer = new IndexWriter(dir, (IndexWriterConfig)NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetIndexDeletionPolicy(dp).SetMaxBufferedDocs(2));
IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
.SetIndexDeletionPolicy(dp)
.SetMaxBufferedDocs(2));

// Verify we catch misuse:
try
Expand All @@ -145,7 +146,7 @@ private void RunTest(Random random, Directory dir)
dp = (SnapshotDeletionPolicy)writer.Config.IndexDeletionPolicy;
writer.Commit();

ThreadJob t = new ThreadAnonymousClass(stopTime, writer, NewField);
ThreadJob t = new ThreadAnonymousClass(stopTime, writer);

t.Start();

Expand Down Expand Up @@ -180,18 +181,11 @@ private sealed class ThreadAnonymousClass : ThreadJob
{
private readonly long stopTime;
private readonly IndexWriter writer;
private readonly Func<string, string, FieldType, Field> newFieldFunc;

/// <param name="newFieldFunc">
/// LUCENENET specific
/// Passed in because <see cref="LuceneTestCase.NewField(string, string, FieldType)"/>
/// is no longer static.
/// </param>
public ThreadAnonymousClass(long stopTime, IndexWriter writer, Func<string, string, FieldType, Field> newFieldFunc)

public ThreadAnonymousClass(long stopTime, IndexWriter writer)
{
this.stopTime = stopTime;
this.writer = writer;
this.newFieldFunc = newFieldFunc;
}

public override void Run()
Expand All @@ -201,7 +195,7 @@ public override void Run()
customType.StoreTermVectors = true;
customType.StoreTermVectorPositions = true;
customType.StoreTermVectorOffsets = true;
doc.Add(newFieldFunc("content", "aaa", customType));
doc.Add(NewField("content", "aaa", customType));
do
{
for (int i = 0; i < 27; i++)
Expand Down Expand Up @@ -269,7 +263,7 @@ private void CopyFiles(Directory dir, IndexCommit cp)
// we take to do the backup, the IndexWriter will
// never delete the files in the snapshot:
ICollection<string> files = cp.FileNames;
foreach (String fileName in files)
foreach (string fileName in files)
{
// NOTE: in a real backup you would not use
// readFile; you would need to use something else
Expand Down Expand Up @@ -320,7 +314,7 @@ private void ReadFile(Directory dir, string name)
[Test]
public virtual void TestBasicSnapshots()
{
int numSnapshots = 3;
const int numSnapshots = 3;

// Create 3 snapshots: snapshot0, snapshot1, snapshot2
Directory dir = NewDirectory();
Expand Down Expand Up @@ -357,7 +351,7 @@ public virtual void TestMultiThreadedSnapshotting()
for (int i = 0; i < threads.Length; i++)
{
int finalI = i;
threads[i] = new ThreadAnonymousClass2(this, writer, sdp, snapshots, finalI);
threads[i] = new ThreadAnonymousClass2(writer, sdp, snapshots, finalI);
threads[i].Name = "t" + i;
}

Expand Down Expand Up @@ -387,16 +381,13 @@ public virtual void TestMultiThreadedSnapshotting()

private sealed class ThreadAnonymousClass2 : ThreadJob
{
private readonly TestSnapshotDeletionPolicy outerInstance;

private readonly IndexWriter writer;
private readonly SnapshotDeletionPolicy sdp;
private readonly IndexCommit[] snapshots;
private readonly int finalI;

public ThreadAnonymousClass2(TestSnapshotDeletionPolicy outerInstance, IndexWriter writer, SnapshotDeletionPolicy sdp, IndexCommit[] snapshots, int finalI)
public ThreadAnonymousClass2(IndexWriter writer, SnapshotDeletionPolicy sdp, IndexCommit[] snapshots, int finalI)
{
this.outerInstance = outerInstance;
this.writer = writer;
this.sdp = sdp;
this.snapshots = snapshots;
Expand All @@ -421,7 +412,7 @@ public override void Run()
[Test]
public virtual void TestRollbackToOldSnapshot()
{
int numSnapshots = 2;
const int numSnapshots = 2;
Directory dir = NewDirectory();

SnapshotDeletionPolicy sdp = DeletionPolicy;
Expand Down Expand Up @@ -517,4 +508,4 @@ public virtual void TestMissingCommits()
dir.Dispose();
}
}
}
}

0 comments on commit ce0c5b8

Please sign in to comment.