Пример #1
0
        public void UnSubscribeNotSubscribed()
        {
            var subscriber = new SubscriberOne();

            MessageAggregator.Instance.UnSubscribe(subscriber);

            Assert.AreEqual(0, MessageAggregator.Instance.Subscriptions.Count);
        }
Пример #2
0
        public void DoubleSubscribeToMessage()
        {
            var subscriberOne = new SubscriberOne();

            MessageAggregator.Instance.Subscribe(subscriberOne);
            MessageAggregator.Instance.Subscribe(subscriberOne);
            Assert.AreEqual(1, MessageAggregator.Instance.Subscriptions.Count);
        }
Пример #3
0
        public void PublishMessage()
        {
            var subscriber = new SubscriberOne();

            MessageAggregator.Instance.Subscribe(subscriber);
            MessageAggregator.Instance.Publish(new MessageOne("hello"));

            Assert.AreEqual("hello", subscriber.Content);
            Assert.AreEqual(1, subscriber.CountOfEvents);
        }
Пример #4
0
        public void PublishTwoMessages()
        {
            var subscriberOne1 = new SubscriberOne();
            var subscriberOne2 = new SubscriberOne();

            var subscriberTwo1 = new SubscriberTwo();
            var subscriberTwo2 = new SubscriberTwo();

            MessageAggregator.Instance.Subscribe(subscriberOne1);
            MessageAggregator.Instance.Subscribe(subscriberOne2);

            MessageAggregator.Instance.Subscribe(subscriberTwo1);
            MessageAggregator.Instance.Subscribe(subscriberTwo2);

            Assert.AreEqual(0, subscriberOne1.CountOfEvents);
            Assert.AreEqual(0, subscriberOne2.CountOfEvents);
            Assert.AreEqual(0, subscriberTwo1.CountOfEvents);
            Assert.AreEqual(0, subscriberTwo2.CountOfEvents);

            MessageAggregator.Instance.Publish(new MessageOne("hello"));

            Assert.AreEqual(1, subscriberOne1.CountOfEvents);
            Assert.AreEqual(1, subscriberOne2.CountOfEvents);
            Assert.AreEqual(0, subscriberTwo1.CountOfEvents);
            Assert.AreEqual(0, subscriberTwo2.CountOfEvents);

            MessageAggregator.Instance.Publish(new MessageTwo(3));

            Assert.AreEqual("hello", subscriberOne1.Content);
            Assert.AreEqual("hello", subscriberOne2.Content);
            Assert.AreEqual(3, subscriberTwo1.Content);
            Assert.AreEqual(3, subscriberTwo2.Content);

            Assert.AreEqual(1, subscriberOne1.CountOfEvents);
            Assert.AreEqual(1, subscriberOne2.CountOfEvents);
            Assert.AreEqual(1, subscriberTwo1.CountOfEvents);
            Assert.AreEqual(1, subscriberTwo2.CountOfEvents);
        }