Beispiel #1
0
 private PublishedEvent GetEvent(string eventName)
 {
     if(!eventPublishers.ContainsKey(eventName))
     {
         eventPublishers[eventName] = new PublishedEvent();
     }
     return eventPublishers[eventName];
 }
Beispiel #2
0
        public void ShouldAddDelegateToPublishingEventOnAddingPublisher()
        {
            PublishedEvent pe = new PublishedEvent();
            EventSource1 publisher = new EventSource1();

            pe.AddPublisher(publisher, "Event1");

            Assert.AreEqual(1, publisher.NumberOfEvent1Delegates);
        }
Beispiel #3
0
        public void ShouldRemoveDelegateWhenRemovingPublisher()
        {
            PublishedEvent pe = new PublishedEvent();
            EventSource1 publisher = new EventSource1();
            pe.AddPublisher(publisher, "Event1");

            pe.RemovePublisher(publisher, "Event1");

            Assert.AreEqual(0, publisher.NumberOfEvent1Delegates);
        }
Beispiel #4
0
        public void ShouldCallSubscriberWhenPublisherFiresEvent()
        {
            PublishedEvent pe = new PublishedEvent();
            EventSource1 publisher = new EventSource1();
            pe.AddPublisher(publisher, "Event1");
            bool subscriberCalled = false;
            EventHandler subscriber = delegate { subscriberCalled = true; };
            pe.AddSubscriber(subscriber);

            publisher.FireEvent1();

            Assert.IsTrue(subscriberCalled);
        }
Beispiel #5
0
        public void ShouldNotCallSubscriberAfterRemoval()
        {
            PublishedEvent pe = new PublishedEvent();
            EventSource1 publisher = new EventSource1();
            pe.AddPublisher(publisher, "Event1");
            int numberOfSubscriberCalls = 0;
            EventHandler subscriber = delegate { ++numberOfSubscriberCalls; };
            pe.AddSubscriber(subscriber);

            publisher.FireEvent1();

            pe.RemoveSubscriber(subscriber);
            publisher.FireEvent1();

            Assert.AreEqual(1, numberOfSubscriberCalls);
        }
Beispiel #6
0
        public void ShouldMulticastEventsToMultipleSubscribers()
        {
            PublishedEvent pe = new PublishedEvent();
            EventSource1 publisher = new EventSource1();
            pe.AddPublisher(publisher, "Event1");
            bool subscriber1Called = false;
            EventHandler subscriber1 = delegate { subscriber1Called = true; };
            bool subscriber2Called = false;
            EventHandler subscriber2 = delegate { subscriber2Called = true; };
            pe.AddSubscriber(subscriber1);
            pe.AddSubscriber(subscriber2);

            publisher.FireEvent1();

            Assert.IsTrue(subscriber1Called);
            Assert.IsTrue(subscriber2Called);
        }
Beispiel #7
0
        public void RegisterSubscriber(string publishedEventName, EventHandler subscriber)
        {
            PublishedEvent publishedEvent = GetEvent(publishedEventName);

            publishedEvent.AddSubscriber(subscriber);
        }
Beispiel #8
0
        public void RegisterPublisher(string publishedEventName, object publisher, string eventName)
        {
            PublishedEvent @event = GetEvent(publishedEventName);

            @event.AddPublisher(publisher, eventName);
        }