public void MultiplePublisherMultipleSubscriber()
        {
            EventBroker eb = new EventBroker();
            Subscriber.Count = 0;

            Publisher p1 = new Publisher();
            Publisher p2 = new Publisher();

            Subscriber s1 = new Subscriber();
            Subscriber s2 = new Subscriber();
            Subscriber s3 = new Subscriber();

            eb.Register(p1);
            eb.Register(p2);
            eb.Register(s1);
            eb.Register(s2);
            eb.Register(s3);

            p1.CallCount();
            p2.CallCount();

            eb.Unregister(p1);
            eb.Unregister(p2);
            eb.Unregister(s1);
            eb.Unregister(s2);
            eb.Unregister(s3);

            Assert.AreEqual(6, Subscriber.Count);
        }
        public void RegisterUnknownEvent()
        {
            Publisher p = new Publisher();

            Assert.Throws<Exceptions.PublisherEventNotFoundException>(
                () => this.testee.RegisterEvent(EventTopics.SimpleEvent, p, "UnknownEvent", HandlerRestriction.None));
        }
Esempio n. 3
0
        public void SetUp()
        {
            Subscriber.Count = 0;

            this.testee = new EventBroker();
            this.p = new Publisher();
            this.s = new Subscriber();

            this.testee.Register(this.p);
            this.testee.Register(this.s);
        }
        public void RegisterEvent()
        {
            Publisher p = new Publisher();
            Subscriber s = new Subscriber();

            this.testee.RegisterEvent(EventTopics.SimpleEvent, p, "Event", HandlerRestriction.None);
            this.testee.Register(s);

            p.CallEvent();

            Assert.IsTrue(s.SimpleEventCalled, "event was not handled.");
        }
Esempio n. 5
0
        private void DotNetEvent(int runNumber)
        {
            var p = new Publisher();
            var s = new Subscriber();

            p.Event += s.HandleEvent;

            this.Run(
                p.FireEvent,
                runNumber + " .Net event",
                this.numberOfEvents * 9);
        }
        public void DisposeUnregistersPublicationByProperty()
        {
            Publisher p = new Publisher();
            Subscriber s = new Subscriber();

            this.testee.Register(p);
            this.testee.Register(s);

            this.testee.Dispose();

            Assert.Throws<NullReferenceException>(
                () => p.CallSimpleEvent(),
                "Should result in a null reference exception because the event should not be registered by the event topic anymore.");
        }
        public void PublisherWithoutSubscriber()
        {
            EventBroker eb = new EventBroker();

            Publisher p = new Publisher();
            Subscriber.Count = 0;

            eb.Register(p);

            p.CallCount();

            eb.Unregister(p);

            Assert.AreEqual(0, Subscriber.Count);
        }
Esempio n. 8
0
        private void SimpleEvent(int runNumber)
        {
            EventBroker eventBroker = new EventBroker();
            
            var p = new Publisher();
            var s = new Subscriber();

            eventBroker.Register(p);
            eventBroker.Register(s);

            this.Run(
                p.FireEvent,
                runNumber + " simple event",
                0);
        }
        public void GlobalPublicationMatcher()
        {
            var publicationMatcher = new TestPublicationMatcher();

            var publisher = new Publisher();
            var subscriber = new Subscriber();

            this.testee.Register(publisher);
            this.testee.Register(subscriber);

            this.testee.AddGlobalMatcher(publicationMatcher);

            publisher.CallSimpleEvent();

            subscriber.SimpleEventCalled.Should().BeFalse();
        }
Esempio n. 10
0
        private void TrueMatcher(int runNumber)
        {
            EventBroker eventBroker = new EventBroker();

            eventBroker.AddGlobalMatcher(new Matcher(true));

            var p = new Publisher();
            var s = new Subscriber();

            eventBroker.Register(p);
            eventBroker.Register(s);

            this.Run(
                p.FireEvent,
                runNumber + " true matcher",
                0);
        }
        public void Restrict()
        {
            EventBroker eventBroker = new EventBroker();

            Publisher p = new Publisher();
            Subscriber s = new Subscriber();

            eventBroker.Register(p);
            eventBroker.Register(s);

            CancelEventArgs e = new CancelEventArgs(false);
            p.Fire(e);

            Assert.AreEqual(1, s.NumberOfHandledEvents, "should be handled.");

            p.Fire(e);
            Assert.AreEqual(1, s.NumberOfHandledEvents, "should not be handled.");
        }
        public void SeveralHandlers()
        {
            EventBroker eventBroker = new EventBroker();

            Publisher p = new Publisher();
            Subscriber s1 = new Subscriber();
            Subscriber s2 = new Subscriber();

            eventBroker.Register(p);
            eventBroker.Register(s1);
            eventBroker.Register(s2);

            CancelEventArgs e = new CancelEventArgs(false);
            p.Fire(e);

            Assert.AreEqual(1, s1.NumberOfHandledEvents, "s1 was not called");
            Assert.AreEqual(0, s2.NumberOfHandledEvents, "s2 was called");
        }
        public void RegisterHandlerMethod()
        {
            Publisher p = new Publisher();
            Subscriber s = new Subscriber();

            this.testee.RegisterHandlerMethod(EventTopics.SimpleEvent, s, s.Handle, new Handlers.Publisher());
            this.testee.Register(p);

            p.CallSimpleEvent();

            Assert.IsTrue(s.Called, "event was not handled.");

            s.Reset();
            this.testee.RemoveSubscription(EventTopics.SimpleEvent, s, s.Handle);

            p.CallSimpleEvent();

            Assert.IsFalse(s.Called, "event should not have been handled.");
        }
        public void RegisterCustomEventHandlerMethod()
        {
            Publisher p = new Publisher();
            Subscriber s = new Subscriber();

            this.testee.RegisterHandlerMethod<CustomEventArguments>(EventTopics.CustomEventArgs, s, s.HandleCustomEvent, new Handlers.Publisher());
            this.testee.Register(p);

            p.CallCustomEventArgs("test");

            Assert.IsTrue(s.Called, "event was not handled.");

            s.Reset();
            this.testee.RemoveSubscription<CustomEventArguments>(EventTopics.CustomEventArgs, s, s.HandleCustomEvent);

            p.CallCustomEventArgs("test");

            Assert.IsFalse(s.Called, "event should not have been handled.");
        }
        public void FreedSubscriber()
        {
            Publisher p = new Publisher();
            Subscriber s1 = new Subscriber();
            Subscriber s2 = new Subscriber();

            this.testee.Register(p);
            this.testee.Register(s1);
            this.testee.Register(s2);

// ReSharper disable RedundantAssignment
            s1 = null; // kill reference to s1
// ReSharper restore RedundantAssignment
            GC.Collect();  // breaks up the weak reference to the subscriber

            p.CallCount();

            Assert.AreEqual(1, Subscriber.Count);

            Assert.Greater(s2.ThreadId, -10); // just some stupid code to prevent s2 from being collected
        }
Esempio n. 16
0
        private void Subscribers(int runNumber, int numberOfSubscribersInThisRun)
        {
            EventBroker eventBroker = new EventBroker();

            var p = new Publisher();
            var subscribers = new Subscriber[numberOfSubscribersInThisRun];

            for (int i = 0; i < numberOfSubscribersInThisRun; i++)
            {
                subscribers[i] = new Subscriber();
                eventBroker.Register(subscribers[i]);
            }

            eventBroker.Register(p);
           
            this.Run(
                p.FireEvent,
                runNumber + " number of subscribers " + numberOfSubscribersInThisRun,
                -9 * this.numberOfEvents / 10);
        }
        public void ExceptionHandlingMultipleException()
        {
            Publisher p = new Publisher();
            SubscriberThrowingException s1 = new SubscriberThrowingException();
            SubscriberThrowingException s2 = new SubscriberThrowingException();

            this.testee.Register(p);
            this.testee.Register(s1);
            this.testee.Register(s2);

            Assert.Throws<SubscriberThrowingException.TestException>(
                () => p.CallSimpleEvent());
        }
        public void ExceptionHandlingWithExtensionHandlingException()
        {
            Publisher p = new Publisher();
            SubscriberThrowingException s = new SubscriberThrowingException();

            var exceptionHandlingExtension = new ExceptionHandlingExtension();
            this.testee.AddExtension(exceptionHandlingExtension);

            this.testee.Register(p);
            this.testee.Register(s);

            p.CallSimpleEvent();

            exceptionHandlingExtension.HandledException
                .Should().BeOfType<SubscriberThrowingException.TestException>();
        }
        public void ExceptionHandling()
        {
            Publisher p = new Publisher();
            SubscriberThrowingException s = new SubscriberThrowingException();

            this.testee.Register(p);
            this.testee.Register(s);

            p.Invoking(x => x.CallSimpleEvent()).ShouldThrow<SubscriberThrowingException.TestException>();
        }
        public void ExceptionHandling()
        {
            try
            {
                Publisher p = new Publisher();
                SubscriberThrowingException s = new SubscriberThrowingException();

                this.testee.Register(p);
                this.testee.Register(s);

                p.CallSimpleEvent();

                Assert.Fail("must not be reached.");
            }
            catch (EventTopicException e)
            {
                Assert.IsTrue(e.InnerException is SubscriberThrowingException.TestException);
            }
        }
        public void SubscriptionsInCodeCanBeRemoved()
        {
            Publisher p = new Publisher();
            Subscriber s = new Subscriber();

            this.testee.Register(p);
            this.testee.AddSubscription(EventTopics.SimpleEvent, s, s.SimpleEvent, new Handlers.Publisher());

            this.testee.RemoveSubscription(EventTopics.SimpleEvent, s, s.SimpleEvent);

            p.CallSimpleEvent();

            Assert.IsFalse(s.SimpleEventCalled);
        }