Skip to content

Commit 2dace0f

Browse files
MikeAlhayekgvkries
andauthored
Fix Lucene Flaky test (OrchardCMS#18080)
--------- Co-authored-by: Georg von Kries <gvk@creativbox.net>
1 parent 9fb3468 commit 2dace0f

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

src/OrchardCore/OrchardCore.Search.AzureAI.Core/Services/AzureAISearchIndexManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task<bool> CreateAsync(IndexProfile indexProfile)
3535
{
3636
if (await ExistsAsync(indexProfile.IndexFullName))
3737
{
38-
return true;
38+
return false;
3939
}
4040

4141
try
@@ -73,7 +73,7 @@ public async Task<SearchIndex> GetAsync(string indexFullName)
7373

7474
var response = await client.GetIndexAsync(indexFullName);
7575

76-
return response?.Value;
76+
return response.Value;
7777
}
7878
catch (RequestFailedException e)
7979
{

src/OrchardCore/OrchardCore.Search.Elasticsearch.Core/Services/ElasticsearchIndexManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ public async Task<bool> CreateAsync(IndexProfile index)
137137
{
138138
_logger.LogWarning("There were issues creating an index in Elasticsearch");
139139
}
140+
141+
return false;
140142
}
141143

142144
await _indexEvents.InvokeAsync((handler, ctx) => handler.CreatedAsync(ctx), context, _logger);
143145

144-
return response.Acknowledged;
146+
return true;
145147
}
146148

147149
public async Task<string> GetIndexSettingsAsync(string indexFullName)
@@ -162,6 +164,8 @@ public async Task<string> GetIndexSettingsAsync(string indexFullName)
162164
{
163165
_logger.LogWarning("There were issues retrieving index settings from Elasticsearch");
164166
}
167+
168+
return null;
165169
}
166170

167171
var setting = response.Indices[indexFullName].Settings;
@@ -187,6 +191,8 @@ public async Task<string> GetIndexInfoAsync(string indexFullName)
187191
{
188192
_logger.LogWarning("There were issues retrieving index info from Elasticsearch");
189193
}
194+
195+
return null;
190196
}
191197

192198
var info = response.Indices[indexFullName];

src/OrchardCore/OrchardCore.Search.Lucene.Core/LuceneIndexStore.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ namespace OrchardCore.Lucene.Core;
2020

2121
public sealed class LuceneIndexStore : ILuceneIndexStore, IDisposable
2222
{
23-
private readonly ConcurrentDictionary<string, IndexReaderPool> _indexPools = new(StringComparer.OrdinalIgnoreCase);
24-
private readonly ConcurrentDictionary<string, IndexWriterWrapper> _writers = new(StringComparer.OrdinalIgnoreCase);
25-
private readonly ConcurrentDictionary<string, DateTime> _timestamps = new(StringComparer.OrdinalIgnoreCase);
23+
private readonly ConcurrentDictionary<string, IndexReaderPool> _indexPools = new();
24+
private readonly ConcurrentDictionary<string, IndexWriterWrapper> _writers = new();
25+
private readonly ConcurrentDictionary<string, DateTime> _timestamps = new();
2626

27-
private readonly object _directoryLock = new();
2827
private readonly object _lock = new();
2928

3029
private readonly string _rootPath;
@@ -77,11 +76,10 @@ public Task<bool> RemoveAsync(IndexProfile index)
7776

7877
lock (_lock)
7978
{
80-
if (_writers.TryRemove(index.Id, out var writer))
79+
if (_writers.TryGetValue(index.Id, out var writer))
8180
{
8281
writer.IsClosing = true;
8382
writer.Dispose();
84-
removed = true;
8583
}
8684

8785
if (_indexPools.TryRemove(index.Id, out var reader))
@@ -101,6 +99,8 @@ public Task<bool> RemoveAsync(IndexProfile index)
10199
}
102100
catch { }
103101
}
102+
103+
removed = _writers.TryRemove(index.Id, out _);
104104
}
105105

106106
return Task.FromResult(removed);
@@ -168,7 +168,6 @@ private Task WriteAsync(IndexProfile index, Action<IndexWriter> action, bool clo
168168
}
169169
}
170170

171-
writer.IsClosing = true;
172171
writer.Dispose();
173172

174173
_timestamps.AddOrUpdate(index.Id, _clock.UtcNow, (key, oldValue) => _clock.UtcNow);
@@ -183,11 +182,6 @@ private Task WriteAsync(IndexProfile index, Action<IndexWriter> action, bool clo
183182

184183
if (writer.IsClosing)
185184
{
186-
if (_writers.TryRemove(index.Id, out var removedWriter))
187-
{
188-
removedWriter.Dispose();
189-
}
190-
191185
return Task.CompletedTask;
192186
}
193187

@@ -213,11 +207,15 @@ private IndexReaderPool.IndexReaderLease GetReader(IndexProfile index)
213207
{
214208
var path = new DirectoryInfo(GetFullPath(index.IndexFullName));
215209

216-
var directory = FSDirectory.Open(path);
210+
// Lucene is not thread safe on this call.
211+
lock (_lock)
212+
{
213+
var directory = FSDirectory.Open(path);
217214

218-
var reader = DirectoryReader.Open(directory);
215+
var reader = DirectoryReader.Open(directory);
219216

220-
return new IndexReaderPool(reader);
217+
return new IndexReaderPool(reader);
218+
}
221219
});
222220

223221
return pool.Acquire();
@@ -237,11 +235,7 @@ private FSDirectory CreateDirectory(string indexFullName)
237235
path.Create();
238236
}
239237

240-
// Lucene is not thread safe on this call.
241-
lock (_directoryLock)
242-
{
243-
return FSDirectory.Open(path);
244-
}
238+
return FSDirectory.Open(path);
245239
}
246240
}
247241

src/OrchardCore/OrchardCore/Modules/Overrides/HttpClient/ActiveHandlerTrackingEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ActiveHandlerTrackingEntry(
3131
Scope = scope;
3232
Lifetime = lifetime;
3333

34-
_lock = new object();
34+
_lock = new();
3535
}
3636

3737
public LifetimeTrackingHttpMessageHandler Handler { get; private set; }

test/OrchardCore.Tests/Apis/ContentManagement/DeploymentPlans/ContentStepLuceneQueryTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public async Task ShouldUpdateLuceneIndexesOnImport()
1414

1515
// Setup
1616
await context.InitializeAsync();
17+
await context.WaitForOutstandingDeferredTasksAsync(TestContext.Current.CancellationToken);
1718

1819
// Act
1920
var recipe = BlogPostDeploymentContext.GetContentStepRecipe(context.OriginalBlogPost, jItem =>
@@ -33,10 +34,7 @@ public async Task ShouldUpdateLuceneIndexesOnImport()
3334
data.Add(secondContentItem);
3435

3536
await context.PostRecipeAsync(recipe);
36-
37-
// Indexing of the content item happens in the deferred-task and may not be immediate available,
38-
// so we wait until the indexing is done before querying.
39-
await context.WaitForOutstandingDeferredTasksAsync(TestContext.Current.CancellationToken);
37+
await context.WaitForHttpBackgroundJobsAsync(TestContext.Current.CancellationToken);
4038

4139
// Test
4240
var result = await context

0 commit comments

Comments
 (0)