public async Task Should_Not_Call_Action_After_Unregister_2() { var totalData = 0; var action = new Func <MySimpleEventData, Task>( eventData => { totalData += eventData.Value; return(Task.CompletedTask); }); LocalEventBus.Subscribe(action); await LocalEventBus.PublishAsync(new MySimpleEventData(1)); await LocalEventBus.PublishAsync(new MySimpleEventData(2)); await LocalEventBus.PublishAsync(new MySimpleEventData(3)); LocalEventBus.Unsubscribe(action); await LocalEventBus.PublishAsync(new MySimpleEventData(4)); Assert.Equal(6, totalData); }
public async Task Should_Change_TenantId_If_EventData_Is_MultiTenant() { var tenantId = Guid.NewGuid(); var handler = new MyEventHandler(GetRequiredService <ICurrentTenant>()); LocalEventBus.Subscribe <EntityChangedEventData <MyEntity> >(handler); await LocalEventBus.PublishAsync(new EntityCreatedEventData <MyEntity>(new MyEntity(tenantId))); handler.TenantId.ShouldBe(tenantId); }
public async Task Should_Call_Created_And_Changed_Once() { var handler = new MyEventHandler(); LocalEventBus.Subscribe <EntityChangedEventData <MyEntity> >(handler); LocalEventBus.Subscribe <EntityCreatedEventData <MyEntity> >(handler); await LocalEventBus.PublishAsync(new EntityCreatedEventData <MyEntity>(new MyEntity())); handler.EntityCreatedEventCount.ShouldBe(1); handler.EntityChangedEventCount.ShouldBe(1); }
public async Task Should_Call_Handler_AndDispose() { LocalEventBus.Subscribe <MySimpleEventData, MySimpleTransientEventHandler>(); await LocalEventBus.PublishAsync(new MySimpleEventData(1)); await LocalEventBus.PublishAsync(new MySimpleEventData(2)); await LocalEventBus.PublishAsync(new MySimpleEventData(3)); Assert.Equal(3, MySimpleTransientEventHandler.HandleCount); Assert.Equal(3, MySimpleTransientEventHandler.DisposeCount); }
public async Task Should_Trigger_For_Inherited_Generic_2() { var triggeredEvent = false; LocalEventBus.Subscribe <EntityChangedEventData <Person> >( eventData => { eventData.Entity.Id.ShouldBe(42); triggeredEvent = true; return(Task.CompletedTask); }); await LocalEventBus.PublishAsync(new EntityChangedEventData <Student>(new Student(42))); triggeredEvent.ShouldBe(true); }
public async Task Should_Not_Handle_Exception() { var retryAttempt = 0; LocalEventBus.Subscribe <MySimpleEventData>(eventData => { retryAttempt++; throw new Exception("This exception is intentionally thrown!"); }); var appException = await Assert.ThrowsAsync <Exception>(async() => { await LocalEventBus.PublishAsync(new MySimpleEventData(1)); }); retryAttempt.ShouldBe(1); appException.Message.ShouldBe("This exception is intentionally thrown!"); }
public async Task Should_Handle_Exception() { var retryAttempt = 0; LocalEventBus.Subscribe <MyExceptionHandleEventData>(eventData => { eventData.Value.ShouldBe(0); retryAttempt++; if (retryAttempt < 2) { throw new Exception("This exception is intentionally thrown!"); } return(Task.CompletedTask); }); await LocalEventBus.PublishAsync(new MyExceptionHandleEventData(0)); retryAttempt.ShouldBe(2); }
public async Task Should_Call_Handler_With_Non_Generic_Trigger() { var totalData = 0; LocalEventBus.Subscribe <MySimpleEventData>( eventData => { totalData += eventData.Value; return(Task.CompletedTask); }); await LocalEventBus.PublishAsync(typeof(MySimpleEventData), new MySimpleEventData(1)); await LocalEventBus.PublishAsync(typeof(MySimpleEventData), new MySimpleEventData(2)); await LocalEventBus.PublishAsync(typeof(MySimpleEventData), new MySimpleEventData(3)); await LocalEventBus.PublishAsync(typeof(MySimpleEventData), new MySimpleEventData(4)); Assert.Equal(10, totalData); }
public async Task Should_Call_Action_On_Event_With_Correct_Source() { var totalData = 0; LocalEventBus.Subscribe <MySimpleEventData>( eventData => { totalData += eventData.Value; return(Task.CompletedTask); }); await LocalEventBus.PublishAsync(new MySimpleEventData(1)); await LocalEventBus.PublishAsync(new MySimpleEventData(2)); await LocalEventBus.PublishAsync(new MySimpleEventData(3)); await LocalEventBus.PublishAsync(new MySimpleEventData(4)); Assert.Equal(10, totalData); }
public async Task Should_Handle_Events_For_Derived_Classes() { var totalData = 0; LocalEventBus.Subscribe <MySimpleEventData>( eventData => { totalData += eventData.Value; return(Task.CompletedTask); }); await LocalEventBus.PublishAsync(new MySimpleEventData(1)).ConfigureAwait(false); //Should handle directly registered class await LocalEventBus.PublishAsync(new MySimpleEventData(2)).ConfigureAwait(false); //Should handle directly registered class await LocalEventBus.PublishAsync(new MyDerivedEventData(3)).ConfigureAwait(false); //Should handle derived class too await LocalEventBus.PublishAsync(new MyDerivedEventData(4)).ConfigureAwait(false); //Should handle derived class too Assert.Equal(10, totalData); }
public async Task Should_Not_Handle_Events_For_Base_Classes() { var totalData = 0; LocalEventBus.Subscribe <MyDerivedEventData>( eventData => { totalData += eventData.Value; return(Task.CompletedTask); }); await LocalEventBus.PublishAsync(new MySimpleEventData(1)).ConfigureAwait(false); //Should not handle await LocalEventBus.PublishAsync(new MySimpleEventData(2)).ConfigureAwait(false); //Should not handle await LocalEventBus.PublishAsync(new MyDerivedEventData(3)).ConfigureAwait(false); //Should handle await LocalEventBus.PublishAsync(new MyDerivedEventData(4)).ConfigureAwait(false); //Should handle Assert.Equal(7, totalData); }
public async Task Should_Throw_Exception_After_Error_Handle() { var retryAttempt = 0; LocalEventBus.Subscribe <MyExceptionHandleEventData>(eventData => { eventData.Value.ShouldBe(0); retryAttempt++; throw new Exception("This exception is intentionally thrown!"); }); var appException = await Assert.ThrowsAsync <Exception>(async() => { await LocalEventBus.PublishAsync(new MyExceptionHandleEventData(0)); }); retryAttempt.ShouldBe(4); appException.Message.ShouldBe("This exception is intentionally thrown!"); }
public async Task Should_Call_Action_On_Event_With_Correct_Source_Async() { int totalData = 0; LocalEventBus.Subscribe <MySimpleEventData>( async eventData => { await Task.Delay(20); Interlocked.Add(ref totalData, eventData.Value); await Task.Delay(20); }); await LocalEventBus.PublishAsync(new MySimpleEventData(1)); await LocalEventBus.PublishAsync(new MySimpleEventData(2)); await LocalEventBus.PublishAsync(new MySimpleEventData(3)); await LocalEventBus.PublishAsync(new MySimpleEventData(4)); Assert.Equal(10, totalData); }
public async Task Should_Not_Call_Action_After_Unregister_1() { var totalData = 0; var registerDisposer = LocalEventBus.Subscribe <MySimpleEventData>( eventData => { totalData += eventData.Value; return(Task.CompletedTask); }); await LocalEventBus.PublishAsync(new MySimpleEventData(1)); await LocalEventBus.PublishAsync(new MySimpleEventData(2)); await LocalEventBus.PublishAsync(new MySimpleEventData(3)); registerDisposer.Dispose(); await LocalEventBus.PublishAsync(new MySimpleEventData(4)); Assert.Equal(6, totalData); }