mirror of https://github.com/abpframework/abp.git
10 changed files with 133 additions and 34 deletions
@ -0,0 +1,72 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EventBus.Local |
|||
{ |
|||
public class EventBus_Exception_Handler_Tests : EventBusTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Not_Handle_Exception() |
|||
{ |
|||
MySimpleEventData data = null; |
|||
LocalEventBus.Subscribe<MySimpleEventData>(eventData => |
|||
{ |
|||
++eventData.Value; |
|||
data = eventData; |
|||
throw new Exception("This exception is intentionally thrown!"); |
|||
}); |
|||
|
|||
var appException = await Assert.ThrowsAsync<Exception>(async () => |
|||
{ |
|||
await LocalEventBus.PublishAsync(new MySimpleEventData(1)); |
|||
}); |
|||
|
|||
data.Value.ShouldBe(2); |
|||
appException.Message.ShouldBe("This exception is intentionally thrown!"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Handle_Exception() |
|||
{ |
|||
MyExceptionHandleEventData data = null; |
|||
LocalEventBus.Subscribe<MyExceptionHandleEventData>(eventData => |
|||
{ |
|||
++eventData.RetryAttempts; |
|||
data = eventData; |
|||
|
|||
if (eventData.RetryAttempts < 2) |
|||
{ |
|||
throw new Exception("This exception is intentionally thrown!"); |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
|
|||
}); |
|||
|
|||
await LocalEventBus.PublishAsync(new MyExceptionHandleEventData(0)); |
|||
data.RetryAttempts.ShouldBe(2); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Throw_Exception_After_Error_Handle() |
|||
{ |
|||
MyExceptionHandleEventData data = null; |
|||
LocalEventBus.Subscribe<MyExceptionHandleEventData>(eventData => |
|||
{ |
|||
++eventData.RetryAttempts; |
|||
data = eventData; |
|||
throw new Exception("This exception is intentionally thrown!"); |
|||
}); |
|||
|
|||
var appException = await Assert.ThrowsAsync<Exception>(async () => |
|||
{ |
|||
await LocalEventBus.PublishAsync(new MyExceptionHandleEventData(0)); |
|||
}); |
|||
|
|||
data.RetryAttempts.ShouldBe(4); |
|||
appException.Message.ShouldBe("This exception is intentionally thrown!"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
public class MyExceptionHandleEventData |
|||
{ |
|||
public int RetryAttempts { get; set; } |
|||
|
|||
public MyExceptionHandleEventData(int retryAttempts) |
|||
{ |
|||
RetryAttempts = retryAttempts; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue