Skip to content

Commit

Permalink
Merge pull request #74 from marcwittke/hotfix/5.1.7
Browse files Browse the repository at this point in the history
missing flush after domain event rainsing/handling fixed
  • Loading branch information
marcwittke committed Jun 25, 2019
2 parents 174fe08 + 6362c48 commit add8c10
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public virtual void Begin()
public void Complete()
{
Logger.Debug("Completing unit of work #" + _instanceId);
Flush();
Flush(); // we have to flush before raising events, therefore the handlers find the latest changes in the DB
_eventAggregator.RaiseEvents();
Flush(); // event handlers change the DB state, so we have to flush again
Commit();
AsyncHelper.RunSync(()=>_eventBusScope.RaiseEvents());
_isCompleted = true;
Expand Down
78 changes: 68 additions & 10 deletions tests/Backend.Fx.EfCorePersistence.Tests/TheEfUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
using System.Data.Common;
using System.Linq;
using System.Reflection;
using Backend.Fx.BuildingBlocks;
using Backend.Fx.EfCorePersistence.Tests.DummyImpl.Domain;
using Backend.Fx.EfCorePersistence.Tests.DummyImpl.Persistence;
using Backend.Fx.EfCorePersistence.Tests.Fixtures;
using Backend.Fx.Environment.Authentication;
using Backend.Fx.Environment.DateAndTime;
using Backend.Fx.Environment.MultiTenancy;
using Backend.Fx.Patterns.Authorization;
using Backend.Fx.Patterns.EventAggregation.Domain;
using Backend.Fx.Patterns.EventAggregation.Integration;
using FakeItEasy;
Expand All @@ -19,7 +23,9 @@ namespace Backend.Fx.EfCorePersistence.Tests
public class TheEfUnitOfWork
{
private readonly DatabaseFixture _fixture;

private static int _nextTenantId = 2675;
private readonly int _tenantId = _nextTenantId++;

public TheEfUnitOfWork()
{
_fixture = new SqliteDatabaseFixture();
Expand All @@ -30,9 +36,9 @@ public TheEfUnitOfWork()
[Fact]
public void OpensTransaction()
{
using(var dbs = _fixture.UseDbSession())
using (var dbs = _fixture.UseDbSession())
{
using (var sut = new EfUnitOfWork(new FrozenClock(), CurrentIdentityHolder.CreateSystem(),
using (var sut = new EfUnitOfWork(new FrozenClock(), CurrentIdentityHolder.CreateSystem(),
A.Fake<IDomainEventAggregator>(), A.Fake<IEventBusScope>(), dbs.DbContext, dbs.Connection))
{
Assert.Null(dbs.DbContext.Database.CurrentTransaction);
Expand All @@ -44,7 +50,7 @@ public void OpensTransaction()
sut.Complete();
}

Assert.Throws<InvalidOperationException>(()=> dbs.DbContext.Database.CurrentTransaction.Commit());
Assert.Throws<InvalidOperationException>(() => dbs.DbContext.Database.CurrentTransaction.Commit());
}

using (var dbs = _fixture.UseDbSession())
Expand All @@ -58,9 +64,9 @@ public void RollsBackTransactionOnDisposal()
{
using (var dbs = _fixture.UseDbSession())
{
using (var sut = new EfUnitOfWork(new FrozenClock(),
CurrentIdentityHolder.CreateSystem(),
A.Fake<IDomainEventAggregator>(),
using (var sut = new EfUnitOfWork(new FrozenClock(),
CurrentIdentityHolder.CreateSystem(),
A.Fake<IDomainEventAggregator>(),
A.Fake<IEventBusScope>(),
dbs.DbContext,
dbs.Connection))
Expand Down Expand Up @@ -113,7 +119,7 @@ public void ClearingTransactionOnRelationalConnectionViaReflectionWorks()
{
using (var tx = dbs.Connection.BeginTransaction())
{
dbs.DbContext.Database.UseTransaction((DbTransaction) tx);
dbs.DbContext.Database.UseTransaction((DbTransaction)tx);
dbs.DbContext.Bloggers.Add(new Blogger(1, "bbb", "fff"));
dbs.DbContext.SaveChanges();
tx.Commit();
Expand All @@ -122,19 +128,71 @@ public void ClearingTransactionOnRelationalConnectionViaReflectionWorks()


// see EfUnitOfWork.cs ClearTransactions()
RelationalConnection txman = (RelationalConnection) dbs.DbContext.Database.GetService<IDbContextTransactionManager>();
RelationalConnection txman = (RelationalConnection)dbs.DbContext.Database.GetService<IDbContextTransactionManager>();
var methodInfo = typeof(RelationalConnection).GetMethod("ClearTransactions", BindingFlags.Instance | BindingFlags.NonPublic);
methodInfo.Invoke(txman, new object[0]);

using (var tx = dbs.Connection.BeginTransaction())
{
dbs.DbContext.Database.UseTransaction((DbTransaction) tx);
dbs.DbContext.Database.UseTransaction((DbTransaction)tx);
dbs.DbContext.Bloggers.Add(new Blogger(2, "bbb", "fff"));
dbs.DbContext.SaveChanges();
tx.Commit();
}
}
}
}

public class AnEvent : IDomainEvent { }

public class AnEventHandler : IDomainEventHandler<AnEvent>
{
private readonly IRepository<Blog> _blogRepository;

public AnEventHandler(IRepository<Blog> blogRepository)
{
_blogRepository = blogRepository;
}

public void Handle(AnEvent domainEvent)
{
_blogRepository.Add(new Blog(99999, "Created via Event Handling"));
}
}

[Fact]
public void FlushesAfterDomainEventHandling()
{
IDomainEventHandlerProvider fakeEventHandlerProvider = A.Fake<IDomainEventHandlerProvider>();
var domainEventAggregator = new DomainEventAggregator(fakeEventHandlerProvider);


using (var dbs = _fixture.UseDbSession())
{
A.CallTo(
() => fakeEventHandlerProvider.GetAllEventHandlers<AnEvent>())
.ReturnsLazily(() =>
{
var repo = new EfRepository<Blog>(dbs.DbContext, new BlogMapping(),
CurrentTenantIdHolder.Create(_tenantId), new AllowAll<Blog>());
return new[] {new AnEventHandler(repo)};
});

using (var sut = new EfUnitOfWork(new FrozenClock(), CurrentIdentityHolder.CreateSystem(),
domainEventAggregator, A.Fake<IEventBusScope>(), dbs.DbContext, dbs.Connection))
{
sut.Begin();
domainEventAggregator.PublishDomainEvent(new AnEvent());
sut.Complete();
}
}

using (var dbs = _fixture.UseDbSession())
{
var createdViaEvent = dbs.DbContext.Blogs.Find(99999);
Assert.NotNull(createdViaEvent);
Assert.Equal("Created via Event Handling", createdViaEvent.Name);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void CommitsOnComplete()
sut.Complete();
sut.Dispose();
Assert.Equal(0, sut.RollbackCount);
Assert.Equal(1, sut.UpdateTrackingPropertiesCount);
Assert.Equal(2, sut.UpdateTrackingPropertiesCount);
Assert.Equal(1, sut.CommitCount);
}

Expand Down

0 comments on commit add8c10

Please sign in to comment.