mirror of https://github.com/abpframework/abp.git
13 changed files with 470 additions and 0 deletions
@ -0,0 +1,25 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp2.0</TargetFramework> |
||||
|
<LangVersion>latest</LangVersion> |
||||
|
<AssemblyName>Volo.Abp.EventBus.Tests</AssemblyName> |
||||
|
<PackageId>Volo.Abp.EventBus.Tests</PackageId> |
||||
|
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Ddd\Volo.Abp.Ddd.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.EventBus\Volo.Abp.EventBus.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,115 @@ |
|||||
|
using System; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class ActionBasedEventHandlerTest : EventBusTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Call_Action_On_Event_With_Correct_Source() |
||||
|
{ |
||||
|
var totalData = 0; |
||||
|
|
||||
|
EventBus.Register<MySimpleEventData>( |
||||
|
eventData => |
||||
|
{ |
||||
|
totalData += eventData.Value; |
||||
|
}); |
||||
|
|
||||
|
EventBus.Trigger(new MySimpleEventData(1)); |
||||
|
EventBus.Trigger(new MySimpleEventData(2)); |
||||
|
EventBus.Trigger(new MySimpleEventData(3)); |
||||
|
EventBus.Trigger(new MySimpleEventData(4)); |
||||
|
|
||||
|
Assert.Equal(10, totalData); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Call_Handler_With_Non_Generic_Trigger() |
||||
|
{ |
||||
|
var totalData = 0; |
||||
|
|
||||
|
EventBus.Register<MySimpleEventData>( |
||||
|
eventData => |
||||
|
{ |
||||
|
totalData += eventData.Value; |
||||
|
}); |
||||
|
|
||||
|
EventBus.Trigger(typeof(MySimpleEventData), new MySimpleEventData(1)); |
||||
|
EventBus.Trigger(typeof(MySimpleEventData), new MySimpleEventData(2)); |
||||
|
EventBus.Trigger(typeof(MySimpleEventData), new MySimpleEventData(3)); |
||||
|
EventBus.Trigger(typeof(MySimpleEventData), new MySimpleEventData(4)); |
||||
|
|
||||
|
Assert.Equal(10, totalData); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Not_Call_Action_After_Unregister_1() |
||||
|
{ |
||||
|
var totalData = 0; |
||||
|
|
||||
|
var registerDisposer = EventBus.Register<MySimpleEventData>( |
||||
|
eventData => |
||||
|
{ |
||||
|
totalData += eventData.Value; |
||||
|
}); |
||||
|
|
||||
|
EventBus.Trigger(new MySimpleEventData(1)); |
||||
|
EventBus.Trigger(new MySimpleEventData(2)); |
||||
|
EventBus.Trigger(new MySimpleEventData(3)); |
||||
|
|
||||
|
registerDisposer.Dispose(); |
||||
|
|
||||
|
EventBus.Trigger(new MySimpleEventData(4)); |
||||
|
|
||||
|
Assert.Equal(6, totalData); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Not_Call_Action_After_Unregister_2() |
||||
|
{ |
||||
|
var totalData = 0; |
||||
|
|
||||
|
var action = new Action<MySimpleEventData>( |
||||
|
eventData => |
||||
|
{ |
||||
|
totalData += eventData.Value; |
||||
|
}); |
||||
|
|
||||
|
EventBus.Register(action); |
||||
|
|
||||
|
EventBus.Trigger(new MySimpleEventData(1)); |
||||
|
EventBus.Trigger(new MySimpleEventData(2)); |
||||
|
EventBus.Trigger(new MySimpleEventData(3)); |
||||
|
|
||||
|
EventBus.Unregister(action); |
||||
|
|
||||
|
EventBus.Trigger(new MySimpleEventData(4)); |
||||
|
|
||||
|
Assert.Equal(6, totalData); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Call_Action_On_Event_With_Correct_Source_Async() |
||||
|
{ |
||||
|
int totalData = 0; |
||||
|
|
||||
|
EventBus.AsyncRegister<MySimpleEventData>( |
||||
|
async eventData => |
||||
|
{ |
||||
|
await Task.Delay(20); |
||||
|
Interlocked.Add(ref totalData, eventData.Value); |
||||
|
await Task.Delay(20); |
||||
|
}); |
||||
|
|
||||
|
await EventBus.TriggerAsync(new MySimpleEventData(1)); |
||||
|
await EventBus.TriggerAsync(new MySimpleEventData(2)); |
||||
|
await EventBus.TriggerAsync(new MySimpleEventData(3)); |
||||
|
await EventBus.TriggerAsync(new MySimpleEventData(4)); |
||||
|
|
||||
|
Assert.Equal(10, totalData); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public abstract class EventBusTestBase |
||||
|
{ |
||||
|
protected IEventBus EventBus; |
||||
|
|
||||
|
protected EventBusTestBase() |
||||
|
{ |
||||
|
EventBus = new EventBus(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
using System; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class EventBus_Exception_Test : EventBusTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Throw_Single_Exception_If_Only_One_Of_Handlers_Fails() |
||||
|
{ |
||||
|
EventBus.Register<MySimpleEventData>( |
||||
|
eventData => throw new Exception("This exception is intentionally thrown!")); |
||||
|
|
||||
|
var appException = Assert.Throws<Exception>(() => |
||||
|
{ |
||||
|
EventBus.Trigger(new MySimpleEventData(1)); |
||||
|
}); |
||||
|
|
||||
|
appException.Message.ShouldBe("This exception is intentionally thrown!"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Throw_Aggregate_Exception_If_More_Than_One_Of_Handlers_Fail() |
||||
|
{ |
||||
|
EventBus.Register<MySimpleEventData>( |
||||
|
eventData => throw new Exception("This exception is intentionally thrown #1!")); |
||||
|
|
||||
|
EventBus.Register<MySimpleEventData>( |
||||
|
eventData => throw new Exception("This exception is intentionally thrown #2!")); |
||||
|
|
||||
|
var aggrException = Assert.Throws<AggregateException>(() => |
||||
|
{ |
||||
|
EventBus.Trigger(new MySimpleEventData(1)); |
||||
|
}); |
||||
|
|
||||
|
aggrException.InnerExceptions.Count.ShouldBe(2); |
||||
|
aggrException.InnerExceptions[0].Message.ShouldBe("This exception is intentionally thrown #1!"); |
||||
|
aggrException.InnerExceptions[1].Message.ShouldBe("This exception is intentionally thrown #2!"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Domain.Entities; |
||||
|
using Volo.Abp.Domain.Entities.Events; |
||||
|
using Volo.Abp.EventBus.Handlers; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class EventBus_EntityEvents_Test : EventBusTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Call_Created_And_Changed_Once() |
||||
|
{ |
||||
|
var handler = new MyEventHandler(); |
||||
|
|
||||
|
EventBus.Register<EntityChangedEventData<MyEntity>>(handler); |
||||
|
EventBus.Register<EntityCreatedEventData<MyEntity>>(handler); |
||||
|
|
||||
|
var asyncHandler = new MyAsyncEventHandler(); |
||||
|
|
||||
|
EventBus.AsyncRegister<EntityChangedEventData<MyEntity>>(asyncHandler); |
||||
|
EventBus.AsyncRegister<EntityCreatedEventData<MyEntity>>(asyncHandler); |
||||
|
|
||||
|
EventBus.Trigger(new EntityCreatedEventData<MyEntity>(new MyEntity())); |
||||
|
|
||||
|
handler.EntityCreatedEventCount.ShouldBe(1); |
||||
|
handler.EntityChangedEventCount.ShouldBe(1); |
||||
|
|
||||
|
asyncHandler.EntityCreatedEventCount.ShouldBe(1); |
||||
|
asyncHandler.EntityChangedEventCount.ShouldBe(1); |
||||
|
} |
||||
|
|
||||
|
public class MyEntity : Entity |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public class MyEventHandler : |
||||
|
IEventHandler<EntityChangedEventData<MyEntity>>, |
||||
|
IEventHandler<EntityCreatedEventData<MyEntity>> |
||||
|
{ |
||||
|
public int EntityChangedEventCount { get; set; } |
||||
|
public int EntityCreatedEventCount { get; set; } |
||||
|
|
||||
|
public void HandleEvent(EntityChangedEventData<MyEntity> eventData) |
||||
|
{ |
||||
|
EntityChangedEventCount++; |
||||
|
} |
||||
|
|
||||
|
public void HandleEvent(EntityCreatedEventData<MyEntity> eventData) |
||||
|
{ |
||||
|
EntityCreatedEventCount++; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MyAsyncEventHandler : |
||||
|
IAsyncEventHandler<EntityChangedEventData<MyEntity>>, |
||||
|
IAsyncEventHandler<EntityCreatedEventData<MyEntity>> |
||||
|
{ |
||||
|
public int EntityChangedEventCount { get; set; } |
||||
|
public int EntityCreatedEventCount { get; set; } |
||||
|
|
||||
|
public Task HandleEventAsync(EntityChangedEventData<MyEntity> eventData) |
||||
|
{ |
||||
|
EntityChangedEventCount++; |
||||
|
return Task.FromResult(0); |
||||
|
} |
||||
|
|
||||
|
public Task HandleEventAsync(EntityCreatedEventData<MyEntity> eventData) |
||||
|
{ |
||||
|
EntityCreatedEventCount++; |
||||
|
return Task.FromResult(0); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,55 @@ |
|||||
|
using Shouldly; |
||||
|
using Volo.Abp.Domain.Entities; |
||||
|
using Volo.Abp.Domain.Entities.Events; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class GenericInheritanceTest : EventBusTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Trigger_For_Inherited_Generic_1() |
||||
|
{ |
||||
|
var triggeredEvent = false; |
||||
|
|
||||
|
EventBus.Register<EntityChangedEventData<Person>>( |
||||
|
eventData => |
||||
|
{ |
||||
|
eventData.Entity.Id.ShouldBe(42); |
||||
|
triggeredEvent = true; |
||||
|
}); |
||||
|
|
||||
|
EventBus.Trigger(new EntityUpdatedEventData<Person>(new Person { Id = 42 })); |
||||
|
|
||||
|
triggeredEvent.ShouldBe(true); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Trigger_For_Inherited_Generic_2() |
||||
|
{ |
||||
|
var triggeredEvent = false; |
||||
|
|
||||
|
EventBus.Register<EntityChangedEventData<Person>>( |
||||
|
eventData => |
||||
|
{ |
||||
|
eventData.Entity.Id.ShouldBe(42); |
||||
|
triggeredEvent = true; |
||||
|
}); |
||||
|
|
||||
|
EventBus.Trigger(new EntityChangedEventData<Student>(new Student { Id = 42 })); |
||||
|
|
||||
|
triggeredEvent.ShouldBe(true); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public class Person : Entity<int> |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public class Student : Person |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,45 @@ |
|||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class InheritanceTest : EventBusTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Handle_Events_For_Derived_Classes() |
||||
|
{ |
||||
|
var totalData = 0; |
||||
|
|
||||
|
EventBus.Register<MySimpleEventData>( |
||||
|
eventData => |
||||
|
{ |
||||
|
totalData += eventData.Value; |
||||
|
}); |
||||
|
|
||||
|
EventBus.Trigger(new MySimpleEventData(1)); //Should handle directly registered class
|
||||
|
EventBus.Trigger(new MySimpleEventData(2)); //Should handle directly registered class
|
||||
|
EventBus.Trigger(new MyDerivedEventData(3)); //Should handle derived class too
|
||||
|
EventBus.Trigger(new MyDerivedEventData(4)); //Should handle derived class too
|
||||
|
|
||||
|
Assert.Equal(10, totalData); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Not_Handle_Events_For_Base_Classes() |
||||
|
{ |
||||
|
var totalData = 0; |
||||
|
|
||||
|
EventBus.Register<MyDerivedEventData>( |
||||
|
eventData => |
||||
|
{ |
||||
|
totalData += eventData.Value; |
||||
|
}); |
||||
|
|
||||
|
EventBus.Trigger(new MySimpleEventData(1)); //Should not handle
|
||||
|
EventBus.Trigger(new MySimpleEventData(2)); //Should not handle
|
||||
|
EventBus.Trigger(new MyDerivedEventData(3)); //Should handle
|
||||
|
EventBus.Trigger(new MyDerivedEventData(4)); //Should handle
|
||||
|
|
||||
|
Assert.Equal(7, totalData); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class MyDerivedEventData : MySimpleEventData |
||||
|
{ |
||||
|
public MyDerivedEventData(int value) |
||||
|
: base(value) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class MySimpleEventData |
||||
|
{ |
||||
|
public int Value { get; set; } |
||||
|
|
||||
|
public MySimpleEventData(int value) |
||||
|
{ |
||||
|
Value = value; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.EventBus.Handlers; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class MySimpleTransientAsyncEventHandler : IAsyncEventHandler<MySimpleEventData>, IDisposable |
||||
|
{ |
||||
|
public static int HandleCount { get; set; } |
||||
|
|
||||
|
public static int DisposeCount { get; set; } |
||||
|
|
||||
|
public Task HandleEventAsync(MySimpleEventData eventData) |
||||
|
{ |
||||
|
++HandleCount; |
||||
|
return Task.FromResult(0); |
||||
|
} |
||||
|
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
++DisposeCount; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using System; |
||||
|
using Volo.Abp.EventBus.Handlers; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class MySimpleTransientEventHandler : IEventHandler<MySimpleEventData>, IDisposable |
||||
|
{ |
||||
|
public static int HandleCount { get; set; } |
||||
|
|
||||
|
public static int DisposeCount { get; set; } |
||||
|
|
||||
|
public void HandleEvent(MySimpleEventData eventData) |
||||
|
{ |
||||
|
++HandleCount; |
||||
|
} |
||||
|
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
++DisposeCount; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus |
||||
|
{ |
||||
|
public class TransientDisposableEventHandlerTest : EventBusTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Call_Handler_AndDispose() |
||||
|
{ |
||||
|
EventBus.Register<MySimpleEventData, MySimpleTransientEventHandler>(); |
||||
|
EventBus.Register<MySimpleEventData, MySimpleTransientAsyncEventHandler>(); |
||||
|
|
||||
|
EventBus.Trigger(new MySimpleEventData(1)); |
||||
|
EventBus.Trigger(new MySimpleEventData(2)); |
||||
|
EventBus.Trigger(new MySimpleEventData(3)); |
||||
|
|
||||
|
Assert.Equal(3, MySimpleTransientEventHandler.HandleCount); |
||||
|
Assert.Equal(3, MySimpleTransientEventHandler.DisposeCount); |
||||
|
|
||||
|
Assert.Equal(3, MySimpleTransientAsyncEventHandler.HandleCount); |
||||
|
Assert.Equal(3, MySimpleTransientAsyncEventHandler.DisposeCount); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue