-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix #115 Concurrency issue when adding documents. include date on lock Tweaked logic * Fixed cache counts in tests due to locking * Address pr feedback
- Loading branch information
Showing
8 changed files
with
123 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,4 +58,3 @@ await repository.AddAsync(new GameReview | |
return services; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
tests/Foundatio.Repositories.Elasticsearch.Tests/DailyRepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Foundatio.Repositories.Elasticsearch.Tests.Repositories; | ||
using Foundatio.Repositories.Elasticsearch.Tests.Repositories.Models; | ||
using Foundatio.Repositories.Models; | ||
using Foundatio.Utility; | ||
using Microsoft.Extensions.Time.Testing; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Foundatio.Repositories.Elasticsearch.Tests; | ||
|
||
public sealed class DailyRepositoryTests : ElasticRepositoryTestBase | ||
{ | ||
private readonly IFileAccessHistoryRepository _fileAccessHistoryRepository; | ||
|
||
public DailyRepositoryTests(ITestOutputHelper output) : base(output) | ||
{ | ||
_fileAccessHistoryRepository = new FileAccessHistoryRepository(_configuration.DailyFileAccessHistory); | ||
} | ||
|
||
public override async Task InitializeAsync() | ||
{ | ||
await base.InitializeAsync(); | ||
await RemoveDataAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task AddAsyncWithCustomDateIndex() | ||
{ | ||
var utcNow = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
var history = await _fileAccessHistoryRepository.AddAsync(new FileAccessHistory { Path = "path1", AccessedDateUtc = utcNow }, o => o.ImmediateConsistency()); | ||
Assert.NotNull(history?.Id); | ||
|
||
var result = await _fileAccessHistoryRepository.FindOneAsync(f => f.Id(history.Id)); | ||
Assert.Equal("file-access-history-daily-v1-2023.01.01", result.Data.GetString("index")); | ||
} | ||
|
||
[Fact] | ||
public async Task AddAsyncWithCurrentDateViaDocumentsAdding() | ||
{ | ||
_configuration.TimeProvider = new FakeTimeProvider(new DateTimeOffset(2023, 02, 1, 0, 0, 0, TimeSpan.Zero)); | ||
|
||
try | ||
{ | ||
// NOTE: This has to be async handler as there is no way to remove a sync handler. | ||
_fileAccessHistoryRepository.DocumentsAdding.AddHandler(OnDocumentsAdding); | ||
|
||
var history = await _fileAccessHistoryRepository.AddAsync(new FileAccessHistory { Path = "path2" }, o => o.ImmediateConsistency()); | ||
Assert.NotNull(history?.Id); | ||
|
||
var result = await _fileAccessHistoryRepository.FindOneAsync(f => f.Id(history.Id)); | ||
Assert.Equal("file-access-history-daily-v1-2023.02.01", result.Data.GetString("index")); | ||
} | ||
finally | ||
{ | ||
_fileAccessHistoryRepository.DocumentsAdding.RemoveHandler(OnDocumentsAdding); | ||
} | ||
} | ||
|
||
private Task OnDocumentsAdding(object sender, DocumentsEventArgs<FileAccessHistory> arg) | ||
{ | ||
foreach (var document in arg.Documents) | ||
{ | ||
if (document.AccessedDateUtc == DateTime.MinValue || document.AccessedDateUtc > _configuration.TimeProvider.GetUtcNow().UtcDateTime) | ||
document.AccessedDateUtc = _configuration.TimeProvider.GetUtcNow().UtcDateTime; | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
[Fact] | ||
public async Task CanAddAsync() | ||
{ | ||
var history = await _fileAccessHistoryRepository.AddAsync(new FileAccessHistory { AccessedDateUtc = DateTime.UtcNow }); | ||
Assert.NotNull(history?.Id); | ||
} | ||
|
||
[Fact] | ||
public Task AddAsyncConcurrentUpdates() | ||
{ | ||
return Parallel.ForEachAsync(Enumerable.Range(0, 50), async (i, _) => | ||
{ | ||
var history = await _fileAccessHistoryRepository.AddAsync(new FileAccessHistory { AccessedDateUtc = DateTime.UtcNow }); | ||
Assert.NotNull(history?.Id); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ies.Elasticsearch.Tests/Repositories/Configuration/Indexes/DailyFileAccessHistoryIndex.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Foundatio.Repositories.Elasticsearch.Configuration; | ||
using Foundatio.Repositories.Elasticsearch.Tests.Repositories.Models; | ||
using Nest; | ||
|
||
namespace Foundatio.Repositories.Elasticsearch.Tests.Repositories.Configuration.Indexes; | ||
|
||
public sealed class DailyFileAccessHistoryIndex : DailyIndex<FileAccessHistory> | ||
{ | ||
public DailyFileAccessHistoryIndex(IElasticConfiguration configuration) : base(configuration, "file-access-history-daily", 1, d => ((FileAccessHistory)d).AccessedDateUtc) | ||
{ | ||
} | ||
|
||
public override CreateIndexDescriptor ConfigureIndex(CreateIndexDescriptor idx) | ||
{ | ||
return base.ConfigureIndex(idx.Settings(s => s.NumberOfReplicas(0).NumberOfShards(1))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters