-
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathSyncedFileSystemDirectoryFactoryTests.cs
More file actions
229 lines (195 loc) · 9.89 KB
/
SyncedFileSystemDirectoryFactoryTests.cs
File metadata and controls
229 lines (195 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Examine.Lucene;
using Examine.Lucene.Analyzers;
using Examine.Lucene.Directories;
using Examine.Lucene.Providers;
using Lucene.Net.Codecs.Lucene46;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Directory = Lucene.Net.Store.Directory;
namespace Examine.Test.Examine.Lucene.Directories
{
[TestFixture]
[NonParallelizable]
public class SyncedFileSystemDirectoryFactoryTests : ExamineBaseTest
{
private const int ItemCount = 100;
[TestCase(true, false, true, SyncedFileSystemDirectoryFactory.CreateResult.NotClean | SyncedFileSystemDirectoryFactory.CreateResult.Fixed | SyncedFileSystemDirectoryFactory.CreateResult.OpenedSuccessfully)]
[TestCase(true, false, false, SyncedFileSystemDirectoryFactory.CreateResult.NotClean | SyncedFileSystemDirectoryFactory.CreateResult.CorruptCreatedNew)]
[TestCase(true, true, false, SyncedFileSystemDirectoryFactory.CreateResult.MissingSegments | SyncedFileSystemDirectoryFactory.CreateResult.CorruptCreatedNew)]
[TestCase(false, false, false, SyncedFileSystemDirectoryFactory.CreateResult.OpenedSuccessfully)]
[Test]
public void Given_ExistingCorruptIndex_When_CreatingDirectory_Then_IndexCreatedOrOpened(
bool corruptIndex,
bool removeSegments,
bool fixIndex,
Enum expected)
{
var mainPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
try
{
CreateIndex(mainPath, corruptIndex, removeSegments);
using var syncedDirFactory = new SyncedFileSystemDirectoryFactory(
new DirectoryInfo(tempPath),
new DirectoryInfo(mainPath),
new DefaultLockFactory(),
LoggerFactory,
Mock.Of<IOptionsMonitor<LuceneDirectoryIndexOptions>>(x => x.Get(TestIndex.TestIndexName) == new LuceneDirectoryIndexOptions()),
fixIndex);
using var index = new LuceneIndex(
LoggerFactory,
TestIndex.TestIndexName,
Mock.Of<IOptionsMonitor<LuceneDirectoryIndexOptions>>(x => x.Get(TestIndex.TestIndexName) == new LuceneDirectoryIndexOptions
{
DirectoryFactory = syncedDirFactory
}));
var result = syncedDirFactory.TryCreateDirectory(index, false, out var dir);
Assert.IsTrue(result.HasFlag(expected), $"{result} does not have flag {expected}");
}
finally
{
System.IO.Directory.Delete(mainPath, true);
System.IO.Directory.Delete(tempPath, true);
}
}
[Test]
public void Given_CorruptMainIndex_And_HealthyLocalIndex_When_CreatingDirectory_Then_LocalIndexSyncedToMain()
{
var mainPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
try
{
// create unhealthy index
CreateIndex(mainPath, true, false);
// create healthy index
CreateIndex(tempPath, false, false);
using (var syncedDirFactory = new SyncedFileSystemDirectoryFactory(
new DirectoryInfo(tempPath),
new DirectoryInfo(mainPath),
new DefaultLockFactory(),
LoggerFactory,
Mock.Of<IOptionsMonitor<LuceneDirectoryIndexOptions>>(x => x.Get(TestIndex.TestIndexName) == new LuceneDirectoryIndexOptions()),
false))
{
using var index = new LuceneIndex(
LoggerFactory,
TestIndex.TestIndexName,
Mock.Of<IOptionsMonitor<LuceneDirectoryIndexOptions>>(x => x.Get(TestIndex.TestIndexName) == new LuceneDirectoryIndexOptions
{
DirectoryFactory = syncedDirFactory
}));
var result = syncedDirFactory.TryCreateDirectory(index, false, out var dir);
Assert.IsTrue(result.HasFlag(SyncedFileSystemDirectoryFactory.CreateResult.SyncedFromLocal));
}
// Ensure the docs are there in main
using var mainIndex = new LuceneIndex(
LoggerFactory,
TestIndex.TestIndexName,
Mock.Of<IOptionsMonitor<LuceneDirectoryIndexOptions>>(x => x.Get(TestIndex.TestIndexName) == new LuceneDirectoryIndexOptions
{
DirectoryFactory = new GenericDirectoryFactory(_ => FSDirectory.Open(Path.Combine(mainPath, TestIndex.TestIndexName))),
}));
var searchResults = mainIndex.Searcher.CreateQuery().All().Execute();
Assert.AreEqual(ItemCount - 2, searchResults.TotalItemCount);
}
finally
{
System.IO.Directory.Delete(mainPath, true);
System.IO.Directory.Delete(tempPath, true);
}
}
[Test]
public void Given_CorruptMainIndex_And_CorruptLocalIndex_When_CreatingDirectory_Then_NewIndexesCreatedAndUsable()
{
var mainPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
try
{
// create unhealthy index
CreateIndex(mainPath, true, false);
// create unhealthy index
CreateIndex(tempPath, true, false);
using var syncedFactory = new SyncedFileSystemDirectoryFactory(
new DirectoryInfo(tempPath),
new DirectoryInfo(mainPath),
new DefaultLockFactory(),
LoggerFactory,
Mock.Of<IOptionsMonitor<LuceneDirectoryIndexOptions>>(x => x.Get(TestIndex.TestIndexName) == new LuceneDirectoryIndexOptions()),
false);
// Ensure the docs are there in main
using var mainIndex = new LuceneIndex(
LoggerFactory,
TestIndex.TestIndexName,
Mock.Of<IOptionsMonitor<LuceneDirectoryIndexOptions>>(x => x.Get(TestIndex.TestIndexName) == new LuceneDirectoryIndexOptions
{
DirectoryFactory = syncedFactory,
}));
var searchResults = mainIndex.Searcher.CreateQuery().All().Execute();
Assert.AreEqual(0, searchResults.TotalItemCount);
}
finally
{
System.IO.Directory.Delete(mainPath, true);
System.IO.Directory.Delete(tempPath, true);
}
}
private void CreateIndex(string rootPath, bool corruptIndex, bool removeSegments)
{
var logger = LoggerFactory.CreateLogger<SyncedFileSystemDirectoryFactoryTests>();
var indexPath = Path.Combine(rootPath, TestIndex.TestIndexName);
logger.LogInformation($"Creating index at {indexPath} with options: corruptIndex: {corruptIndex}, removeSegments: {removeSegments}");
using var luceneDir = FSDirectory.Open(indexPath);
using (var writer = new IndexWriter(luceneDir, new IndexWriterConfig(LuceneInfo.CurrentVersion, new CultureInvariantStandardAnalyzer())))
using (var indexer = GetTestIndex(writer))
using (indexer.WithThreadingMode(IndexThreadingMode.Synchronous))
{
var valueSets = new List<ValueSet>();
for (int i = 0; i < ItemCount; i++)
{
valueSets.Add(
new ValueSet(i.ToString(), "content",
new Dictionary<string, IEnumerable<object>>
{
{"item1", new List<object>(new[] {"value1"})},
{"item2", new List<object>(new[] {"value2"})}
}));
}
indexer.IndexItems(valueSets);
// Now delete some items
indexer.DeleteFromIndex(new[] { "1", "2" });
// double ensure we commit here
indexer.IndexWriter.IndexWriter.Commit();
indexer.IndexWriter.IndexWriter.WaitForMerges();
}
logger.LogInformation("Created index at " + luceneDir.Directory);
Assert.IsTrue(DirectoryReader.IndexExists(luceneDir));
if (corruptIndex)
{
CorruptIndex(luceneDir.Directory, removeSegments, logger);
}
}
private void CorruptIndex(DirectoryInfo dir, bool removeSegments, ILogger logger)
{
// index file extensions (no segments, no gen)
var indexFileExtensions = IndexFileNames.INDEX_EXTENSIONS
.Except(new[] { IndexFileNames.GEN_EXTENSION })
.ToArray();
// Get an index (non segments file) and delete it (corrupt index)
var indexFile = dir.GetFiles()
.Where(x => removeSegments
? x.Extension.Contains(Lucene46SegmentInfoFormat.SI_EXTENSION, StringComparison.OrdinalIgnoreCase)
: indexFileExtensions.Any(e => IndexFileNames.MatchesExtension(x.Extension, e)))
.First();
logger.LogInformation($"Deleting {indexFile.FullName}");
File.Delete(indexFile.FullName);
}
}
}