Ejemplo n.º 1
0
        public void Should_notify_changes_in_subscribers()
        {
            //arrange
            var subscriberCount = 0;
            var changes         = 0;
            var subscriber1     = new SampleEventBrokerSubscriber();

            //act
            eventBroker.SubscriptionAdded += (sender, args) =>
            {
                subscriberCount = args.SubscriberCount;
                changes++;
            };

            eventBroker.SubscriptionRemoved += (sender, args) =>
            {
                subscriberCount = args.SubscriberCount;
                changes++;
            };

            eventBroker.Subscribe(0, subscriber1.GetHashCode(), new NormalTopicSubscription((o, e) => { }));
            eventBroker.Unsubscribe(0, subscriber1.GetHashCode());

            //assert
            Assert.AreEqual(0, subscriberCount);
            Assert.AreEqual(2, changes);
        }
Ejemplo n.º 2
0
        public void Should_notify_changes_in_subscribers()
        {
            //arrange
            var subscriberCount = 0;
            var changes = 0;
            var subscriber1 = new SampleEventBrokerSubscriber();

            //act
            eventBroker.SubscriptionAdded += (sender, args) =>
            {
                subscriberCount = args.SubscriberCount;
                changes++;
            };

            eventBroker.SubscriptionRemoved += (sender, args) =>
            {
                subscriberCount = args.SubscriberCount;
                changes++;
            };

            eventBroker.Subscribe(0, subscriber1.GetHashCode(), new NormalTopicSubscription((o, e) => { }));
            eventBroker.Unsubscribe(0, subscriber1.GetHashCode());

            //assert
            Assert.Equal(0, subscriberCount);
            Assert.Equal(2, changes);
        }
Ejemplo n.º 3
0
        public void Should_notify_subscribers()
        {
            //arrange
            var subscriber1 = new SampleEventBrokerSubscriber();
            var subscriber2 = new SampleEventBrokerSubscriber();

            //act
            eventBroker.Subscribe(0, subscriber1.GetHashCode(),
                                  new NormalTopicSubscription((o, e) => { subscriber1.ReceivedEvent = true; }));
            eventBroker.Subscribe(0, subscriber2.GetHashCode(),
                                  new NormalTopicSubscription((o, e) => { subscriber2.ReceivedEvent = true; }));
            eventBroker.InvokeEvent(0, this, new EventArgs());

            //assert
            Assert.True(subscriber1.ReceivedEvent);
            Assert.True(subscriber2.ReceivedEvent);
        }
Ejemplo n.º 4
0
        public void Should_notify_subscribers()
        {
            //arrange
            var subscriber1 = new SampleEventBrokerSubscriber();
            var subscriber2 = new SampleEventBrokerSubscriber();

            //act
            eventBroker.Subscribe(0, subscriber1.GetHashCode(),
                new NormalTopicSubscription((o, e) => { subscriber1.ReceivedEvent = true; }));
            eventBroker.Subscribe(0, subscriber2.GetHashCode(),
                new NormalTopicSubscription((o, e) => { subscriber2.ReceivedEvent = true; }));
            eventBroker.InvokeEvent(0, this, new EventArgs());

            //assert
            Assert.True(subscriber1.ReceivedEvent);
            Assert.True(subscriber2.ReceivedEvent);
        }
Ejemplo n.º 5
0
        public void Should_only_notify_subscribers_on_relevant_topic()
        {
            //arrange
            var invoked     = 0;
            var subscriber1 = new SampleEventBrokerSubscriber();
            var subscriber2 = new SampleEventBrokerSubscriber();
            var subscriber3 = new SampleEventBrokerSubscriber();
            var subscriber4 = new SampleEventBrokerSubscriber();


            eventBroker.Subscribe(0, subscriber1.GetHashCode(), new NormalTopicSubscription((o, e) =>
            {
                subscriber1.ReceivedEvent = true;
                invoked++;
            }));
            eventBroker.Subscribe(0, subscriber2.GetHashCode(), new NormalTopicSubscription((o, e) =>
            {
                subscriber2.ReceivedEvent = true;
                invoked++;
            }));

            eventBroker.Subscribe(2, subscriber3.GetHashCode(), new NormalTopicSubscription((o, e) =>
            {
                subscriber3.ReceivedEvent = true;
                invoked++;
            }));
            eventBroker.Subscribe(1, subscriber4.GetHashCode(), new NormalTopicSubscription((o, e) =>
            {
                subscriber4.ReceivedEvent = true;
                invoked++;
            }));

            //act
            eventBroker.InvokeEvent(1, this, new EventArgs());
            eventBroker.InvokeEvent(0, this, new EventArgs());

            //assert
            Assert.AreEqual(3, invoked);
            Assert.IsTrue(subscriber1.ReceivedEvent);
            Assert.IsTrue(subscriber2.ReceivedEvent);
            Assert.IsFalse(subscriber3.ReceivedEvent);
            Assert.IsTrue(subscriber4.ReceivedEvent);
        }
Ejemplo n.º 6
0
        public void Should_only_notify_subscribers_on_relevant_topic()
        {
            //arrange
            var invoked = 0;
            var subscriber1 = new SampleEventBrokerSubscriber();
            var subscriber2 = new SampleEventBrokerSubscriber();
            var subscriber3 = new SampleEventBrokerSubscriber();
            var subscriber4 = new SampleEventBrokerSubscriber();


            eventBroker.Subscribe(0, subscriber1.GetHashCode(), new NormalTopicSubscription((o, e) =>
            {
                subscriber1.ReceivedEvent = true;
                invoked++;
            }));
            eventBroker.Subscribe(0, subscriber2.GetHashCode(), new NormalTopicSubscription((o, e) =>
            {
                subscriber2.ReceivedEvent = true;
                invoked++;
            }));

            eventBroker.Subscribe(2, subscriber3.GetHashCode(), new NormalTopicSubscription((o, e) =>
            {
                subscriber3.ReceivedEvent = true;
                invoked++;
            }));
            eventBroker.Subscribe(1, subscriber4.GetHashCode(), new NormalTopicSubscription((o, e) =>
            {
                subscriber4.ReceivedEvent = true;
                invoked++;
            }));

            //act
            eventBroker.InvokeEvent(1, this, new EventArgs());
            eventBroker.InvokeEvent(0, this, new EventArgs());

            //assert
            Assert.Equal(3, invoked);
            Assert.True(subscriber1.ReceivedEvent);
            Assert.True(subscriber2.ReceivedEvent);
            Assert.False(subscriber3.ReceivedEvent);
            Assert.True(subscriber4.ReceivedEvent);
        }