Skip to content

Commit 15bda28

Browse files
feat: better test modes
1. use enum instead of bool 2. For Snapshot, old true = new AllRepositoriesDisposed, behavior unchanged 3. For Snapshot, new RepositoryFactoryDisposed - delete all snapshots only when the repository has been disposed 4. For Transaction, old true = new AllRepositoriesDisposed, behavior is extended, previously transactions would abort as soon as the repository was disposed 5. For Transaction, new RepositoryFactoryDiposed - abort transaction only when the repository factory has been disposed 6. lots of refactor on repositories to make them cleaner
1 parent 4f2c6f1 commit 15bda28

File tree

73 files changed

+1682
-1405
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1682
-1405
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace EntityDb.Abstractions.Annotations
4+
{
5+
/// <summary>
6+
/// Represents data for a single entity that has already been committed, along with relevant information not contained in the data.
7+
/// </summary>
8+
/// <typeparam name="TData">The type of data.</typeparam>
9+
public interface IEntityAnnotation<TData>
10+
{
11+
/// <summary>
12+
/// The transaction id associated with the data.
13+
/// </summary>
14+
Guid TransactionId { get; }
15+
16+
/// <summary>
17+
/// The transaction timestamp associated with the data.
18+
/// </summary>
19+
DateTime TransactionTimeStamp { get; }
20+
21+
/// <summary>
22+
/// The entity id associated with the data.
23+
/// </summary>
24+
Guid EntityId { get; }
25+
26+
/// <summary>
27+
/// The entity version number associated with the data.
28+
/// </summary>
29+
ulong EntityVersionNumber { get; }
30+
31+
/// <summary>
32+
/// The data.
33+
/// </summary>
34+
TData Data { get; }
35+
}
36+
}

src/EntityDb.Abstractions/Commands/IAnnotatedCommand.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/EntityDb.Abstractions/Snapshots/ISnapshotRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,12 @@ public interface ISnapshotRepository<TEntity> : IDisposable, IAsyncDisposable
2323
/// <param name="entity">The entity.</param>
2424
/// <returns><c>true</c> if the insert succeeded, or <c>false</c> if the insert failed.</returns>
2525
Task<bool> PutSnapshot(Guid entityId, TEntity entity);
26+
27+
/// <summary>
28+
/// Deletes multiple <typeparamref name="TEntity"/> snapshots.
29+
/// </summary>
30+
/// <param name="entityIds">The id of the entitie snapshots to delete.</param>
31+
/// <returns><c>true</c> if the deletes all succeeded, or <c>false</c> if any deletes failed.</returns>
32+
Task<bool> DeleteSnapshots(Guid[] entityIds);
2633
}
2734
}

src/EntityDb.Abstractions/Snapshots/ISnapshotRepositoryFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23

34
namespace EntityDb.Abstractions.Snapshots
45
{
56
/// <summary>
67
/// Represents a type used to create instances of <see cref="ISnapshotRepository{TEntity}" />
78
/// </summary>
89
/// <typeparam name="TEntity">The type of entity stored by the <see cref="ISnapshotRepository{TEntity}" />.</typeparam>
9-
public interface ISnapshotRepositoryFactory<TEntity>
10+
public interface ISnapshotRepositoryFactory<TEntity> : IDisposable, IAsyncDisposable
1011
{
1112
/// <summary>
1213
/// Create a new instance of <see cref="ISnapshotRepository{TEntity}" />

src/EntityDb.Abstractions/Transactions/ITransactionRepository.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using EntityDb.Abstractions.Commands;
1+
using EntityDb.Abstractions.Annotations;
2+
using EntityDb.Abstractions.Commands;
23
using EntityDb.Abstractions.Leases;
34
using EntityDb.Abstractions.Queries;
45
using EntityDb.Abstractions.Tags;
@@ -105,7 +106,7 @@ public interface ITransactionRepository<TEntity> : IDisposable, IAsyncDisposable
105106
/// </summary>
106107
/// <param name="commandQuery">The command query.</param>
107108
/// <returns>The annotated commands which are found by <paramref name="commandQuery" />.</returns>
108-
Task<IAnnotatedCommand<TEntity>[]> GetAnnotatedCommands(ICommandQuery commandQuery);
109+
Task<IEntityAnnotation<ICommand<TEntity>>[]> GetAnnotatedCommands(ICommandQuery commandQuery);
109110

110111
/// <summary>
111112
/// Inserts a single transaction with an atomic commit.

src/EntityDb.Abstractions/Transactions/ITransactionRepositoryFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23

34
namespace EntityDb.Abstractions.Transactions
45
{
56
/// <summary>
67
/// Represents a type used to create instances of <see cref="ITransactionRepository{TEntity}" />.
78
/// </summary>
89
/// <typeparam name="TEntity">The type of the entity stored by the <see cref="ITransactionRepository{TEntity}" />.</typeparam>
9-
public interface ITransactionRepositoryFactory<TEntity>
10+
public interface ITransactionRepositoryFactory<TEntity> : IDisposable, IAsyncDisposable
1011
{
1112
/// <summary>
1213
/// Creates a new instance of <see cref="ITransactionRepository{TEntity}" />.

src/EntityDb.Common.Tests/SnapshotTransactions/SnapshotTransactionsTestsBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ await serviceProvider.GetRequiredService<IEntityRepositoryFactory<TransactionEnt
7070

7171
var firstTransaction = await BuildTransaction(entityId, 1, expectedSnapshotVersion, serviceProvider);
7272

73-
await entityRepository.PutTransaction(firstTransaction);
73+
var firstTransactionInserted = await entityRepository.PutTransaction(firstTransaction);
7474

7575
var secondTransaction = await BuildTransaction(entityId, expectedSnapshotVersion, expectedCurrentVersion,
7676
serviceProvider, entityRepository);
7777

78-
await entityRepository.PutTransaction(secondTransaction);
78+
var secondTransactionInserted = await entityRepository.PutTransaction(secondTransaction);
7979

8080
// ACT
8181

@@ -85,6 +85,9 @@ await serviceProvider.GetRequiredService<IEntityRepositoryFactory<TransactionEnt
8585

8686
// ASSERT
8787

88+
firstTransactionInserted.ShouldBeTrue();
89+
secondTransactionInserted.ShouldBeTrue();
90+
8891
snapshot.ShouldNotBeNull();
8992
snapshot.VersionNumber.ShouldBe(expectedSnapshotVersion);
9093
current.VersionNumber.ShouldBe(expectedCurrentVersion);

src/EntityDb.Common.Tests/Startup.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,14 @@ public static void ConfigureTestsBaseServices(IServiceCollection serviceCollecti
4141
{
4242
serviceCollection.Configure<SnapshotSessionOptions>("TestWrite", (options) =>
4343
{
44-
options.TestMode = true;
4544
});
4645

4746
serviceCollection.Configure<TransactionSessionOptions>("TestWrite", (options) =>
4847
{
49-
options.TestMode = true;
5048
});
5149

5250
serviceCollection.Configure<TransactionSessionOptions>("TestReadOnly", (options) =>
5351
{
54-
options.TestMode = true;
5552
options.ReadOnly = true;
5653
});
5754
}

src/EntityDb.Common.Tests/Transactions/TransactionTestsBase.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,18 +407,35 @@ private Guid[] GetSortedGuids(int numberOfGuids)
407407
}
408408

409409
[Fact]
410-
public async Task GivenReadOnlyMode_WhenPuttingTransaction_ThenThrow()
410+
public async Task GivenReadOnlyMode_WhenPuttingTransaction_ThenCannotWriteInReadOnlyModeExceptionIsLogged()
411411
{
412412
// ARRANGE
413413

414+
var loggerMock = new Mock<ILogger>(MockBehavior.Strict);
415+
416+
loggerMock
417+
.Setup(logger => logger.LogError(It.IsAny<CannotWriteInReadOnlyModeException>(), It.IsAny<string>()))
418+
.Verifiable();
419+
414420
var transaction = TransactionSeeder.Create(1, 1);
415421

416-
await using var transactionRepository = await CreateRepository("TestReadOnly");
422+
await using var transactionRepository = await CreateRepository("TestReadOnly", (serviceCollection) =>
423+
{
424+
serviceCollection.Configure<TransactionSessionOptions>("TestReadOnly", (options) =>
425+
{
426+
options.LoggerOverride = loggerMock.Object;
427+
});
428+
});
429+
430+
// ACT
431+
432+
var inserted = await transactionRepository.PutTransaction(transaction);
417433

418434
// ASSERT
419435

420-
await Should.ThrowAsync<CannotWriteInReadOnlyModeException>(async () =>
421-
await transactionRepository.PutTransaction(transaction));
436+
inserted.ShouldBeFalse();
437+
438+
loggerMock.Verify();
422439
}
423440

424441
[Fact]
@@ -482,7 +499,6 @@ public async Task
482499
{
483500
serviceCollection.Configure<TransactionSessionOptions>("TestWriteWithLoggerOverride", (options) =>
484501
{
485-
options.TestMode = true;
486502
options.LoggerOverride = loggerMock.Object;
487503
});
488504
});
@@ -520,7 +536,6 @@ public async Task
520536
{
521537
serviceCollection.Configure<TransactionSessionOptions>("TestWriteWithLoggerOverride", (options) =>
522538
{
523-
options.TestMode = true;
524539
options.LoggerOverride = loggerMock.Object;
525540
});
526541
});
@@ -621,7 +636,7 @@ public async Task GivenCommandInserted_WhenGettingAnnotatedCommand_ThenReturnAnn
621636
annotatedCommands[0].TransactionId.ShouldBe(expectedTransactionId);
622637
annotatedCommands[0].EntityId.ShouldBe(expectedEntityId);
623638
annotatedCommands[0].EntityVersionNumber.ShouldBe(1ul);
624-
annotatedCommands[0].Command.ShouldBe(expectedCommand);
639+
annotatedCommands[0].Data.ShouldBe(expectedCommand);
625640

626641
expectedTransactionTimeStamps.Contains(annotatedCommands[0].TransactionTimeStamp).ShouldBeTrue();
627642
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using EntityDb.Abstractions.Annotations;
2+
using System;
3+
4+
namespace EntityDb.Common.Annotations
5+
{
6+
internal record EntityAnnotation<TData>
7+
(
8+
Guid TransactionId,
9+
DateTime TransactionTimeStamp,
10+
Guid EntityId,
11+
ulong EntityVersionNumber,
12+
TData Data
13+
) : IEntityAnnotation<TData>;
14+
}

0 commit comments

Comments
 (0)