Exemplo n.º 1
0
            public Task Should_Throw_If_Cancellation_Token_Source_Is_Cancelled()
            {
                return(Assert.ThrowsAnyAsync <OperationCanceledException>(async() =>
                {
                    var asyncHandler1 = new TestEventAsyncHandler1(_testOutputHelper);

                    var registration = new EventHandlerRegistration();
                    registration.Register <TriggerLongRunningEvent>(() => asyncHandler1);

                    var publisher = new EventPublisher(registration);

                    publisher.OnError += (@event, ex) =>
                    {
                        _testOutputHelper.WriteLine(ex.Message);
                    };

                    var cts = new CancellationTokenSource();

                    Task publishTask = publisher.PublishAsync(new TriggerLongRunningEvent(1000), cts.Token);

                    cts.Cancel();

                    try
                    {
                        await publishTask;
                    }
                    catch (Exception ex)
                    {
                        _testOutputHelper.WriteLine(ex.Message);
                        throw;
                    }
                }));
            }
Exemplo n.º 2
0
        /// <summary>
        /// Manages counting down dividers and invoking handlers.
        /// </summary>
        /// <param name="registration">The event handler callback registration. Must not be null.</param>
        private void InvokeEventHandler(EventHandlerRegistration <CallbackRegistration> registration)
        {
            Check.IfArgumentNull(registration, "registration");

            if (registration.UserData != null)
            {
                if (registration.UserData.DividerCount > 1)
                {
                    registration.UserData.RunningDivider--;

                    if (registration.UserData.RunningDivider != 0)
                    {
                        // exit, divider not counted down.
                        return;
                    }

                    registration.UserData.RunningDivider = registration.UserData.DividerCount;
                }

                if (registration.UserData.Dispatcher != null)
                {
                    registration.UserData.Dispatcher.BeginInvoke(
                        new Action(() => { InvokeHandlerDirect(registration); }));
                }
                else
                {
                    this.InvokeHandlerDirect(registration);
                }
            }
            else
            {
                this.InvokeHandlerDirect(registration);
            }
        }
Exemplo n.º 3
0
            public Task Should_Check_For_Correct_Command_Type()
            {
                return(Assert.ThrowsAnyAsync <ArgumentException>(async() =>
                {
                    var eventHandler = new TestEventHandler1(_testOutputHelper);

                    var registration = new EventHandlerRegistration();
                    registration.Register(() => (IEventAsyncHandler <TestEvent1>)eventHandler);

                    IEnumerable <EventHandlerDelegate> eventHandlerDelegates = registration.ResolveEventHandlers <TestEvent1>();

                    Assert.NotNull(eventHandlerDelegates);
                    Assert.NotEmpty(eventHandlerDelegates);

                    try
                    {
                        // This delegate handles TestEvent1, but was passed in a TestEvent2.
                        await eventHandlerDelegates.First().Invoke(new TestEvent2());
                    }
                    catch (Exception ex)
                    {
                        _testOutputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
Exemplo n.º 4
0
            public async Task Should_Invoke_All_Registered_Event_Handlers_In_Registration()
            {
                var asyncHandler1 = new TestEventAsyncHandler1(_testOutputHelper);
                var asyncHandler2 = new TestEventAsyncHandler2(_testOutputHelper);
                var asyncHandler3 = new TestEventAsyncHandler3(_testOutputHelper);
                var handler1      = new TestEventHandler1(_testOutputHelper);
                var handler2      = new TestEventHandler2(_testOutputHelper);
                var handler3      = new TestEventHandler3(_testOutputHelper);

                var registration = new EventHandlerRegistration();

                registration.Register <TestEvent1>(() => handler1);
                registration.Register <TestEvent1>(() => handler2);
                registration.Register <TestEvent1>(() => handler3);
                registration.Register <TestEvent1>(() => asyncHandler1);
                registration.Register <TestEvent1>(() => asyncHandler2);
                registration.Register <TestEvent1>(() => asyncHandler3);

                var publisher = new EventPublisher(registration);

                publisher.OnError += (@event, ex) =>
                {
                    _testOutputHelper.WriteLine(ex.Message);
                };

                await publisher.PublishAsync(new TestEvent1());

                // Event may not have yet been handled in background.
                await Task.Delay(500);

                // AsyncHandler1 should have 1 event.
                // 1. TestEvent1
                Assert.Equal(1, asyncHandler1.HandledEvents.Count);
                Assert.Contains(asyncHandler1.HandledEvents, (e) => e is TestEvent1);

                // AsyncHandler2 should have 1 event.
                // 1. TestEvent1
                Assert.Equal(1, asyncHandler2.HandledEvents.Count);
                Assert.Contains(asyncHandler2.HandledEvents, (e) => e is TestEvent1);

                // AsyncHandler3 should have 1 event.
                // 1. TestEvent1
                Assert.Equal(1, asyncHandler3.HandledEvents.Count);
                Assert.Contains(asyncHandler3.HandledEvents, (e) => e is TestEvent1);

                // Handler1 should have 1 event.
                // 1. TestEvent1
                Assert.Equal(1, handler1.HandledEvents.Count);
                Assert.Contains(handler1.HandledEvents, (e) => e is TestEvent1);

                // Handler2 should have 1 event.
                // 1. TestEvent1
                Assert.Equal(1, handler2.HandledEvents.Count);
                Assert.Contains(handler2.HandledEvents, (e) => e is TestEvent1);

                // Handler3 should have 1 event.
                // 1. TestEvent1
                Assert.Equal(1, handler3.HandledEvents.Count);
                Assert.Contains(handler3.HandledEvents, (e) => e is TestEvent1);
            }
Exemplo n.º 5
0
        public static IEventPublisher CreatePublisher(Action <IEventHandlerRegistration> registrationMutator = null)
        {
            var registration = new EventHandlerRegistration();

            if (registrationMutator != null)
            {
                registrationMutator.Invoke(registration);
            }
            return(new EventPublisher(registration));
        }
        /// <summary>
        /// Register event handler for the event interface.
        /// </summary>
        /// <typeparam name="TEventInterface">Event interface of the type <see cref="IEvent"/>.</typeparam>
        /// <typeparam name="TImplementation">Handler for the event interface.</typeparam>
        /// <param name="builder">Instance of <see cref="IEventBusBuilder"/>.</param>
        /// <returns>Instance of <see cref="IEventBusBuilder"/>.</returns>
        public static IEventBusBuilder AddHandler <TEventInterface, TImplementation>(this IEventBusBuilder builder)
            where TEventInterface : IEvent
            where TImplementation : class, IEventHandler <TEventInterface>
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.Add(EventHandlerRegistration.Register <TEventInterface, TImplementation>()));
        }
Exemplo n.º 7
0
 /// <summary>
 /// Directly invokes the handler (no synchronization).
 /// </summary>
 /// <param name="registration">Must not be null.</param>
 private void InvokeHandlerDirect(EventHandlerRegistration <CallbackRegistration> registration)
 {
     try
     {
         registration.Handler?.DynamicInvoke(this, EventArgs.Empty);
     }
     catch (Exception e)
     {
         registration.UserData.LastError = e;
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Add event hander registration to event bus.
        /// </summary>
        /// <param name="registration"><see cref="EventHandlerRegistration"/>.</param>
        /// <returns><see cref="IEventBusBuilder"/>.</returns>
        public IEventBusBuilder Add(EventHandlerRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            Services.Configure <EventBusServiceOptions>(options =>
            {
                options.Registrations.Add(registration);
            });

            return(this);
        }
Exemplo n.º 9
0
 public Task Should_Throw_If_Null_Is_Being_Published()
 {
     return(Assert.ThrowsAsync <ArgumentNullException>(async() =>
     {
         var registration = new EventHandlerRegistration();
         var publisher = new EventPublisher(registration);
         try
         {
             await publisher.PublishAsync((IEvent)null);
         }
         catch (Exception ex)
         {
             _testOutputHelper.WriteLine(ex.Message);
             throw;
         }
     }));
 }
Exemplo n.º 10
0
            public async Task Should_Trigger_OnError_If_EventHandlerRegistration_Produces_Null_Instance()
            {
                var registration = new EventHandlerRegistration();

                // Produces null.
                registration.Register(() => (IEventAsyncHandler <TestEvent1>)null);

                var publisher = new EventPublisher(registration);

                publisher.OnError += (@event, ex) =>
                {
                    _testOutputHelper.WriteLine(ex.Message);

                    Assert.IsType <InvalidOperationException>(ex);
                };

                await publisher.PublishAsync(new TestEvent1());
            }
Exemplo n.º 11
0
            public async Task Should_Invoke_The_Actual_Registered_Command_Handler()
            {
                var eventHandler = new TestEventHandler1(_testOutputHelper);

                var registration = new EventHandlerRegistration();

                registration.Register(() => (IEventHandler <TestEvent1>)eventHandler);

                IEnumerable <EventHandlerDelegate> eventHandlerDelegates = registration.ResolveEventHandlers <TestEvent1>();

                Assert.NotNull(eventHandlerDelegates);
                Assert.NotEmpty(eventHandlerDelegates);

                // Invoke.
                await eventHandlerDelegates.First().Invoke(new TestEvent1());

                // Check if actual command handler instance was invoked.
                Assert.Equal(1, eventHandler.HandledEvents.Count);
                Assert.Contains(eventHandler.HandledEvents, e => e is TestEvent1);
            }
Exemplo n.º 12
0
            public void Should_Store_All_Event_Handlers()
            {
                var asyncHandler1 = new TestEventAsyncHandler1(_testOutputHelper);
                var asyncHandler2 = new TestEventAsyncHandler2(_testOutputHelper);
                var asyncHandler3 = new TestEventAsyncHandler3(_testOutputHelper);
                var handler1      = new TestEventHandler1(_testOutputHelper);
                var handler2      = new TestEventHandler2(_testOutputHelper);
                var handler3      = new TestEventHandler3(_testOutputHelper);

                var registration = new EventHandlerRegistration();

                registration.Register <TestEvent1>(() => asyncHandler1);
                registration.Register <TestEvent1>(() => asyncHandler2);
                registration.Register <TestEvent1>(() => asyncHandler3);
                registration.Register <TestEvent1>(() => handler1);
                registration.Register <TestEvent1>(() => handler2);
                registration.Register <TestEvent1>(() => handler3);

                IEnumerable <EventHandlerDelegate> eventHandlerDelegates = registration.ResolveEventHandlers <TestEvent1>();

                Assert.Equal(6, eventHandlerDelegates.Count());
            }