mirror of https://github.com/abpframework/abp.git
9 changed files with 250 additions and 13 deletions
@ -1,9 +1,6 @@ |
|||||
using JetBrains.Annotations; |
namespace Volo.Abp.Uow |
||||
|
|
||||
namespace Volo.Abp.Uow |
|
||||
{ |
{ |
||||
public interface IAmbientUnitOfWork : IUnitOfWorkAccessor |
public interface IAmbientUnitOfWork : IUnitOfWorkAccessor |
||||
{ |
{ |
||||
void SetUnitOfWork([CanBeNull] IUnitOfWork unitOfWork); |
|
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using System; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.Uow |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Used as event arguments on <see cref="IUnitOfWork.Failed"/> event.
|
||||
|
/// </summary>
|
||||
|
public class UnitOfWorkFailedEventArgs : EventArgs |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Exception that caused failure. This is set only if an error occured during <see cref="IBasicUnitOfWork.Complete"/>.
|
||||
|
/// Can be null if there is no exception, but <see cref="IBasicUnitOfWork.Complete"/> is not called.
|
||||
|
/// Can be null if another exception occurred during the UOW.
|
||||
|
/// </summary>
|
||||
|
[CanBeNull] |
||||
|
public Exception Exception { get; private set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a new <see cref="UnitOfWorkFailedEventArgs"/> object.
|
||||
|
/// </summary>
|
||||
|
/// <param name="exception">Exception that caused failure</param>
|
||||
|
public UnitOfWorkFailedEventArgs([CanBeNull] Exception exception) |
||||
|
{ |
||||
|
Exception = exception; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,113 @@ |
|||||
|
using System; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.TestBase; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Uow |
||||
|
{ |
||||
|
public class UnitOfWork_Events_Tests : AbpIntegratedTest<IndependentEmptyModule> |
||||
|
{ |
||||
|
private readonly IUnitOfWorkManager _unitOfWorkManager; |
||||
|
|
||||
|
public UnitOfWork_Events_Tests() |
||||
|
{ |
||||
|
_unitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Trigger_Complete_On_Success() |
||||
|
{ |
||||
|
var completed = false; |
||||
|
var disposed = false; |
||||
|
|
||||
|
using (var uow = _unitOfWorkManager.Begin()) |
||||
|
{ |
||||
|
uow.Completed += (sender, args) => completed = true; |
||||
|
uow.Disposed += (sender, args) => disposed = true; |
||||
|
|
||||
|
uow.Complete(); |
||||
|
|
||||
|
completed.ShouldBeTrue(); |
||||
|
} |
||||
|
|
||||
|
disposed.ShouldBeTrue(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Trigger_Complete_On_Success_In_Child_Uow() |
||||
|
{ |
||||
|
var completed = false; |
||||
|
var disposed = false; |
||||
|
|
||||
|
using (var uow = _unitOfWorkManager.Begin()) |
||||
|
{ |
||||
|
using (var childUow = _unitOfWorkManager.Begin()) |
||||
|
{ |
||||
|
childUow.Completed += (sender, args) => completed = true; |
||||
|
uow.Disposed += (sender, args) => disposed = true; |
||||
|
|
||||
|
childUow.Complete(); |
||||
|
|
||||
|
completed.ShouldBeFalse(); //Parent has not been completed yet!
|
||||
|
disposed.ShouldBeFalse(); |
||||
|
} |
||||
|
|
||||
|
completed.ShouldBeFalse(); //Parent has not been completed yet!
|
||||
|
disposed.ShouldBeFalse(); |
||||
|
|
||||
|
uow.Complete(); |
||||
|
|
||||
|
completed.ShouldBeTrue(); //It's completed now!
|
||||
|
disposed.ShouldBeFalse(); //But not disposed yet!
|
||||
|
} |
||||
|
|
||||
|
disposed.ShouldBeTrue(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Not_Trigger_Complete_If_Uow_Is_Not_Completed() |
||||
|
{ |
||||
|
var completed = false; |
||||
|
var failed = false; |
||||
|
var disposed = false; |
||||
|
|
||||
|
using (var uow = _unitOfWorkManager.Begin()) |
||||
|
{ |
||||
|
uow.Completed += (sender, args) => completed = true; |
||||
|
uow.Failed += (sender, args) => failed = true; |
||||
|
uow.Disposed += (sender, args) => disposed = true; |
||||
|
} |
||||
|
|
||||
|
completed.ShouldBeFalse(); |
||||
|
failed.ShouldBeTrue(); |
||||
|
disposed.ShouldBeTrue(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Trigger_Failed_If_Uow_Throws_Exception() |
||||
|
{ |
||||
|
var completed = false; |
||||
|
var failed = false; |
||||
|
var disposed = false; |
||||
|
|
||||
|
Assert.Throws<Exception>(new Action(() => |
||||
|
{ |
||||
|
using (var uow = _unitOfWorkManager.Begin()) |
||||
|
{ |
||||
|
uow.Completed += (sender, args) => completed = true; |
||||
|
uow.Failed += (sender, args) => failed = true; |
||||
|
uow.Disposed += (sender, args) => disposed = true; |
||||
|
|
||||
|
throw new Exception("test exception"); |
||||
|
} |
||||
|
})).Message.ShouldBe("test exception"); |
||||
|
|
||||
|
completed.ShouldBeFalse(); |
||||
|
failed.ShouldBeTrue(); |
||||
|
disposed.ShouldBeTrue(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue