예제 #1
0
        public void When_subscribing_same_handler_multiple_times()
        {
            var hub = new MessageHub();

            var totalMsgCount = 0;

            hub.RegisterGlobalHandler((type, msg) =>
            {
                type.ShouldBe(typeof(string));
                msg.ShouldBeOfType <string>();
                Interlocked.Increment(ref totalMsgCount);
            });

            var             queue      = new ConcurrentQueue <string>();
            Action <string> subscriber = msg => queue.Enqueue(msg);

            var tokenOne = hub.Subscribe(subscriber);
            var tokenTwo = hub.Subscribe(subscriber);

            hub.IsSubscribed(tokenOne);
            hub.IsSubscribed(tokenTwo);

            hub.Publish("A");

            queue.Count.ShouldBe(2);
            totalMsgCount.ShouldBe(1);
        }
예제 #2
0
        public void When_operating_on_a_disposed_hub()
        {
            var totalMessages = 0;
            var hub           = new MessageHub();

            hub.RegisterGlobalHandler((type, msg) =>
            {
                type.ShouldBe(typeof(string));
                msg.ShouldBeOfType <string>();
                Interlocked.Increment(ref totalMessages);
            });

            var queue = new ConcurrentQueue <string>();

            Action <string> handler = msg => queue.Enqueue(msg);

            var token = hub.Subscribe(handler);

            hub.Dispose();

            Should.NotThrow(() => hub.Subscribe(handler));
            Should.NotThrow(() => hub.Unsubscribe(token));
            Should.NotThrow(() => hub.IsSubscribed(token));
            Should.NotThrow(() => hub.ClearSubscriptions());

            totalMessages.ShouldBe(0);
        }
예제 #3
0
        public void When_testing_handler_exists()
        {
            var hub = new MessageHub();

            hub.ClearSubscriptions();

            Action <string> subscriberOne = msg => { };
            var             tokenOne      = hub.Subscribe(subscriberOne);

            hub.IsSubscribed(tokenOne).ShouldBeTrue();

            Action <string> subscriberTwo = msg => { };
            var             tokenTwo      = hub.Subscribe(subscriberTwo);

            hub.IsSubscribed(tokenTwo).ShouldBeTrue();

            Action <string> subscriberThree = msg => { };
            var             tokenThree      = hub.Subscribe(subscriberThree);

            hub.IsSubscribed(tokenThree).ShouldBeTrue();

            Action <string> subscriberFour = msg => { };
            var             tokenFour      = hub.Subscribe(subscriberFour);

            hub.IsSubscribed(tokenFour).ShouldBeTrue();

            hub.Unsubscribe(tokenThree);
            hub.IsSubscribed(tokenThree).ShouldBeFalse();

            hub.Unsubscribe(tokenFour);
            hub.IsSubscribed(tokenFour).ShouldBeFalse();

            hub.IsSubscribed(tokenTwo).ShouldBeTrue();
            hub.IsSubscribed(tokenOne).ShouldBeTrue();

            hub.ClearSubscriptions();

            hub.IsSubscribed(tokenOne).ShouldBeFalse();
            hub.IsSubscribed(tokenTwo).ShouldBeFalse();
            hub.IsSubscribed(tokenThree).ShouldBeFalse();
            hub.IsSubscribed(tokenFour).ShouldBeFalse();

            // now let's add back one subscription
            tokenFour = hub.Subscribe(subscriberFour);
            hub.IsSubscribed(tokenFour).ShouldBeTrue();
        }