public static void Subscribe_ThrowsArgumentNullException_ForNullSubscriber()
        {
            // Arrange
            var coordinator = new AspNetMessageCoordinator();

            // Act a Assert
            Assert.Throws <ArgumentNullException>(() => coordinator.Subscribe((Action <object>)null));
        }
        public static void Publish_AcceptsMessageWithoutMatchingSubscribers()
        {
            // Arrange
            var coordinator = new AspNetMessageCoordinator();

            // Act & Assert
            ////Assert.DoesNotThrow(() => coordinator.Publish("message"));
            coordinator.Publish("message");
            Assert.True(true);
        }
        public static void Subscribe_ThrowsInvalidOperationException_WhenCalledAfterClose()
        {
            // Arrange
            var coordinator = new AspNetMessageCoordinator();

            coordinator.Close();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => coordinator.Subscribe <string>(_ => { }));
        }
        public static void Publish_ThrowsInvalidOperationException_WhenCalledAfterClose()
        {
            // Arrange
            var coordinator = new AspNetMessageCoordinator();

            coordinator.Close();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => coordinator.Publish("message"));
        }
        public static void Close_MayBeCalledMultipleTimes()
        {
            // Arrange
            var coordinator = new AspNetMessageCoordinator();

            coordinator.Close();

            // Act & Assert
            ////Assert.DoesNotThrow(() => coordinator.Close());
            coordinator.Close();
            Assert.True(true);
        }
        public static void Publish_DoesNotPushMessageToSubscribersOfInheritedTypes()
        {
            // Arrange
            var coordinator = new AspNetMessageCoordinator();

            coordinator.Subscribe <MyInheritedMessage>(
                _ => Assert.False(true, "Callback should never have been called."));

            // Act
            coordinator.Publish(new MyMessage());

            // Assert
        }
        public static void Publish_AcceptsNullMessage()
        {
            // Arrange
            var    coordinator     = new AspNetMessageCoordinator();
            string receivedMessage = "whatever";

            coordinator.Subscribe <string>(_ => receivedMessage = _);

            // Act
            coordinator.Publish((string)null);

            // Assert
            Assert.Null(receivedMessage);
        }
        public static void PushesPreviouslyPublishedMessages_WhenNestedInSubscriber()
        {
            // Arrange
            var coordinator = new AspNetMessageCoordinator();
            var received    = false;

            coordinator.Publish("message");

            // Act
            coordinator.Subscribe <int>(_ => { received = _ == 123; });
            coordinator.Subscribe <string>(_ => coordinator.Publish(123));

            // Assert
            Assert.True(received);
        }
        public static void Publish_PushesMessageToSubscribersOfBaseType()
        {
            // Arrange
            var       coordinator     = new AspNetMessageCoordinator();
            MyMessage receivedMessage = null;

            coordinator.Subscribe <MyMessage>(_ => receivedMessage = _);

            // Act
            var publishedMessage = new MyInheritedMessage();

            coordinator.Publish(publishedMessage);

            // Assert
            Assert.Equal(publishedMessage, receivedMessage);
        }
        public static void Publish_PushesMessageToSubscribers()
        {
            // Arrange
            var    coordinator     = new AspNetMessageCoordinator();
            string receivedMessage = null;

            coordinator.Subscribe <string>(_ => receivedMessage = _);

            // Act
            var publishedMessage = "hello";

            coordinator.Publish(publishedMessage);

            // Assert
            Assert.Equal(publishedMessage, receivedMessage);
        }
        public static void Subscribe_PushesPreviouslyPublishedMessagesToNewSubscribers()
        {
            // Arrange
            var coordinator      = new AspNetMessageCoordinator();
            var publishedMessage = "hello";

            coordinator.Publish(publishedMessage);

            // Act
            string receivedMessage = null;

            coordinator.Subscribe <string>(_ => receivedMessage = _);

            // Assert
            Assert.Equal(publishedMessage, receivedMessage);
        }
        public static void Publish_PushesToSubscribersInOrderOfSubscription()
        {
            // Arrange
            var coordinator      = new AspNetMessageCoordinator();
            var subscriberCount  = 20;
            var firedSubscribers = new List <int>();

            for (var i = 0; i < subscriberCount; i++)
            {
                var subscriberIndex = i;
                coordinator.Subscribe <string>(_ => firedSubscribers.Add(subscriberIndex));
            }

            // Act
            coordinator.Publish("message");

            // Assert
            Assert.Equal(Enumerable.Range(0, subscriberCount).ToArray(), firedSubscribers);
        }
        public static void Subscribe_PushesPreviouslyPublishedMessagesToNewSubscribersInOrderOfPublication()
        {
            // Arrange
            var coordinator       = new AspNetMessageCoordinator();
            var messageCount      = 20;
            var publishedMessages = Enumerable.Range(0, messageCount).ToList();

            foreach (var message in publishedMessages)
            {
                coordinator.Publish(message);
            }

            // Act
            var receivedMessages = new List <int>();

            coordinator.Subscribe <int>(_ => receivedMessages.Add(_));

            // Assert
            Assert.Equal(publishedMessages, receivedMessages);
        }
        public static void Publish_StopsPushingMessageAfterFirstFailingSubscriber()
        {
            // Arrange
            var coordinator = new AspNetMessageCoordinator();

            coordinator.Subscribe <string>(
                _ => { throw new ApplicationException("Test exception"); });
            coordinator.Subscribe <string>(
                _ => Assert.False(true, "This subscriber should not have been called."));

            // Act
            try
            {
                coordinator.Publish("message");
            }
            catch (ApplicationException)
            {
            }

            // Assert
        }