public void Should_Store_All_Event_Attributed_Handlers() { var attributedHandler1 = new TestAttributedEventHandler1(_testOutputHelper); var attributedHandler2 = new TestAttributedEventHandler2(_testOutputHelper); var attributedHandler3 = new TestAttributedEventHandler3(_testOutputHelper); var registration = new EventHandlerAttributeRegistration(); registration.Register(() => attributedHandler1); registration.Register(() => attributedHandler2); registration.Register(() => attributedHandler3); IEnumerable <EventHandlerDelegate> eventHandlerDelegates = registration.ResolveEventHandlers <TestEvent1>(); // Get all methods marked with [EventHandler] and receiving TestEvent as parameter. int eventHandler1MethodCount = attributedHandler1.GetType().GetMethods().Count(m => m.CustomAttributes.Any(a => a.AttributeType == typeof(EventHandlerAttribute)) && m.GetParameters().Any(p => p.ParameterType == typeof(TestEvent1))); int eventHandler2MethodCount = attributedHandler2.GetType().GetMethods().Count(m => m.CustomAttributes.Any(a => a.AttributeType == typeof(EventHandlerAttribute)) && m.GetParameters().Any(p => p.ParameterType == typeof(TestEvent1))); int eventHandler3MethodCount = attributedHandler3.GetType().GetMethods().Count(m => m.CustomAttributes.Any(a => a.AttributeType == typeof(EventHandlerAttribute)) && m.GetParameters().Any(p => p.ParameterType == typeof(TestEvent1))); int totalEventHandlerMethodCount = eventHandler1MethodCount + eventHandler2MethodCount + eventHandler3MethodCount; Assert.Equal(totalEventHandlerMethodCount, eventHandlerDelegates.Count()); }
public async Task Should_Invoke_All_Registered_Event_Handlers_In_Attribute_Registration() { var attributedHandler1 = new TestAttributedEventHandler1(_testOutputHelper); var attributedHandler2 = new TestAttributedEventHandler2(_testOutputHelper); var attributedHandler3 = new TestAttributedEventHandler3(_testOutputHelper); var registration = new EventHandlerAttributeRegistration(); registration.Register(() => attributedHandler1); registration.Register(() => attributedHandler2); registration.Register(() => attributedHandler3); var publisher = new EventPublisher(registration); publisher.OnError += (eventHandler, ex) => { _testOutputHelper.WriteLine(ex.Message); }; await publisher.PublishAsync(new TestEvent1()); // Just to make sure that events have been processed. await Task.Delay(200); // Handler1 should have 2 events. // It contains sync and async handlers for: // 1. TestEvent Assert.Equal(2, attributedHandler1.HandledEvents.Count); Assert.Contains(attributedHandler1.HandledEvents, e => e is TestEvent1); // Handler2 should have 2 events. // It contains sync and async handlers for: // 1. TestEvent Assert.Equal(2, attributedHandler2.HandledEvents.Count); Assert.Contains(attributedHandler2.HandledEvents, e => e is TestEvent1); // Handler3 should have 2 events. // It contains sync and async handlers for: // 1. TestEvent Assert.Equal(2, attributedHandler3.HandledEvents.Count); Assert.Contains(attributedHandler3.HandledEvents, e => e is TestEvent1); }
public async Task Should_Invoke_All_Registered_Event_Handlers_In_Attribute_Registration_For_Each_Events() { var attributedHandler1 = new TestAttributedEventHandler1(_testOutputHelper); var attributedHandler2 = new TestAttributedEventHandler2(_testOutputHelper); var attributedHandler3 = new TestAttributedEventHandler3(_testOutputHelper); var registration = new EventHandlerAttributeRegistration(); registration.Register(() => attributedHandler1); registration.Register(() => attributedHandler2); registration.Register(() => attributedHandler3); var publisher = new EventPublisher(registration); publisher.OnError += (@event, ex) => { _testOutputHelper.WriteLine(ex.Message); }; await publisher.PublishAsync(new List <IEvent> { new TestEvent1(), new TestEvent2(), new TestEvent3() }); }