Example #1
0
        public void Should_distribute_message_to_all_subscribed_clients()
        {
            // Given
            Logger logger        = LogSetup.CreateLogger();
            var    msgDispatcher = new MessageDispatcher(logger);

            var source  = new Mock <Node>();
            var client1 = new Mock <Node>();
            var client2 = new Mock <Node>();
            var client3 = new Mock <Node>();

            msgDispatcher.AddClient(source.Object);
            msgDispatcher.AddClient(client1.Object);
            msgDispatcher.AddClient(client2.Object);
            msgDispatcher.AddClient(client3.Object);

            msgDispatcher.OnMessageReceived(client1.Object, new SubscribeEvent("CowEvent").Serialize());
            msgDispatcher.OnMessageReceived(client2.Object, new SubscribeEvent("CowEvent").Serialize());

            // When
            msgDispatcher.OnMessageReceived(source.Object, "[CowEvent] Moo!");

            // Then
            client1.Verify(c => c.SendMessage("Moo!"));
            client2.Verify(c => c.SendMessage("Moo!"));
            client3.Verify(c => c.SendMessage(It.IsAny <string>()), Times.Never);
        }
Example #2
0
        public void Should_support_multiple_tags()
        {
            // Given
            Logger logger        = LogSetup.CreateLogger(typeof(MessageDispatcher).Name + "_");
            var    msgDispatcher = new MessageDispatcher(logger);

            var client1 = new Mock <Node>();
            var client2 = new Mock <Node>();
            var client3 = new Mock <Node>();
            var client4 = new Mock <Node>();

            client1.Setup(c => c.ToString()).Returns("client1");
            client2.Setup(c => c.ToString()).Returns("client2");
            client3.Setup(c => c.ToString()).Returns("client3");
            client4.Setup(c => c.ToString()).Returns("client4");

            msgDispatcher.AddClient(client1.Object);
            msgDispatcher.AddClient(client2.Object);
            msgDispatcher.AddClient(client3.Object);
            msgDispatcher.AddClient(client4.Object);

            msgDispatcher.OnMessageReceived(client1.Object, new SubscribeEvent("CowEvent").Serialize());
            msgDispatcher.OnMessageReceived(client2.Object, new SubscribeEvent("CowEvent").Serialize());
            msgDispatcher.OnMessageReceived(client3.Object, new SubscribeEvent("SheepEvent").Serialize());
            msgDispatcher.OnMessageReceived(client4.Object, new SubscribeEvent("SheepEvent").Serialize());

            client3.ResetCalls();
            client4.ResetCalls();

            // When
            msgDispatcher.OnMessageReceived(client1.Object, "[CowEvent] Moo!");

            // Then
            client2.Verify(c => c.SendMessage("Moo!"));
            client3.Verify(c => c.SendMessage(It.IsAny <string>()), Times.Never);
            client4.Verify(c => c.SendMessage(It.IsAny <string>()), Times.Never);

            client1.ResetCalls();
            client2.ResetCalls();

            // When
            msgDispatcher.OnMessageReceived(client3.Object, "[SheepEvent] Baa!");

            // Then
            client1.Verify(c => c.SendMessage(It.IsAny <string>()), Times.Never);
            client2.Verify(c => c.SendMessage(It.IsAny <string>()), Times.Never);
            client4.Verify(c => c.SendMessage("Baa!"));
        }
Example #3
0
        public void Should_distribute_message_to_all_clients_that_are_not_subscribed_to_anything()
        {
            // Given
            Logger logger        = LogSetup.CreateLogger();
            var    msgDispatcher = new MessageDispatcher(logger);

            var source  = new Mock <Node>();
            var client1 = new Mock <Node>();

            msgDispatcher.AddClient(source.Object);
            msgDispatcher.AddClient(client1.Object);

            // When
            msgDispatcher.OnMessageReceived(source.Object, "Random message");

            // Then
            client1.Verify(c => c.SendMessage("Random message"));
        }
Example #4
0
        public void Should_distribute_message_to_all_clients()
        {
            // Given
            Logger logger        = LogSetup.CreateLogger();
            var    msgDispatcher = new MessageDispatcher(logger);

            var source  = new Mock <Node>();
            var client1 = new Mock <Node>();
            var client2 = new Mock <Node>();

            msgDispatcher.AddClient(source.Object);
            msgDispatcher.AddClient(client1.Object);
            msgDispatcher.AddClient(client2.Object);

            // When
            msgDispatcher.OnMessageReceived(source.Object, "Hey!");

            // Then
            client1.Verify(c => c.SendMessage("Hey!"));
            client2.Verify(c => c.SendMessage("Hey!"));
            source.Verify(c => c.SendMessage(It.IsAny <string>()), Times.Never);
        }
Example #5
0
        public void should_close_connection_to_client_when_client_sends_stop_message()
        {
            // Given
            Logger logger = LogSetup.CreateLogger();

            var messageListenerFactory = new Mock <IMessageListenerFactory>();
            var messageListener        = new Mock <IMessageListener>();

            messageListenerFactory.Setup(m => m.Create(It.IsAny <Logger>())).Returns(messageListener.Object);

            var broadcaster = new Mock <IMessageDispatcher>();
            var taskRunner  = new Mock <TaskRunner>();
            var communicationInitializer = new ClientCommunicationInitializer(logger, messageListenerFactory.Object, broadcaster.Object, taskRunner.Object);

            var client = new Mock <Node>();

            communicationInitializer.SetUpCommunicationWith(client.Object);

            // When
            messageListener.Raise(m => m.DoneListeningForMessages += null, client.Object);

            // Then
            client.Verify(c => c.Disconnect());
        }
 public MessageLengthReaderTest()
 {
     _logger = LogSetup.CreateLogger();
 }
Example #7
0
        static internal Server CreateServer()
        {
            Logger logger = LogSetup.CreateLogger();

            return(ServerFactory.CreateServer(logger));
        }
Example #8
0
 public IntegrationTest()
 {
     logger = LogSetup.CreateLogger();
 }
Example #9
0
        public void Should_distribute_message_only_to_clients_that_have_subscribed_to_same_channel()
        {
            // Given
            int    port         = new PortSetup(logger).GetNextPort();
            Logger serverLogger = LogSetup.CreateLogger("Server_");

            serverLogger.MinimumInfoLevelBeforeWrite = InfoLevel.Trace;
            logger.MinimumInfoLevelBeforeWrite       = InfoLevel.Trace;
            Server server = Bootstrapper.CreateServer(serverLogger);

            server.ListenForConnectionsInANewThread(port);

            logger.Write <IntegrationTest>("Connecting client 1");
            var client1 = Bootstrapper.CreateClient(logger);

            client1.Connect(Localhost, port);
            var client1SubscribedEvent = new AutoResetEvent(false);

            client1.OnSubscribed += (theEvent) =>
            {
                logger.Write <IntegrationTest>("Client 1 received subscribeevent: " + theEvent);
                client1SubscribedEvent.Set();
            };
            client1.SubscribeTo("SkeletonEvent");

            logger.Write <IntegrationTest>("Connecting client 2");
            var client2 = Bootstrapper.CreateClient(logger);

            client2.Connect(Localhost, port);
            var client2SubscribedEvent = new AutoResetEvent(false);

            client2.OnSubscribed += (theEvent) =>
            {
                logger.Write <IntegrationTest>("Client 2 received subscribeevent: " + theEvent);
                client2SubscribedEvent.Set();
            };
            client2.SubscribeTo("SkeletonEvent");

            logger.Write <IntegrationTest>("Connecting client 3");
            var client3 = Bootstrapper.CreateClient(logger);

            client3.Connect(Localhost, port);
            var client3SubscribedEvent = new AutoResetEvent(false);

            client3.OnSubscribed += (theEvent) =>
            {
                logger.Write <IntegrationTest>("Client 3 received subscribeevent: " + theEvent);
                client3SubscribedEvent.Set();
            };
            client3.SubscribeTo("SkeletonEvent");

            logger.Write <IntegrationTest>("Connecting client 4");
            var client4 = Bootstrapper.CreateClient(logger);

            client4.Connect(Localhost, port);
            var client4SubscribedEvent = new AutoResetEvent(false);

            client4.OnSubscribed += (theEvent) =>
            {
                logger.Write <IntegrationTest>("Client 4 received subscribeevent: " + theEvent);
                client4SubscribedEvent.Set();
            };
            client4.SubscribeTo("CowEvent");

            logger.Write <IntegrationTest>("Connecting client 5");
            var client5 = Bootstrapper.CreateClient(logger);

            client5.Connect(Localhost, port);
            var client5SubscribedEvent = new AutoResetEvent(false);

            client5.OnSubscribed += (theEvent) =>
            {
                logger.Write <IntegrationTest>("Client 5 received subscribeevent: " + theEvent);
                client5SubscribedEvent.Set();
            };
            client5.SubscribeTo("CowEvent");

            var client2MessageReceived = new AutoResetEvent(false);

            client2.OnMessageReceived += (msg) =>
            {
                logger.Write <IntegrationTest>("Client 2 received message: " + msg);
                client2MessageReceived.Set();
                Assert.Equal("Boo!", msg);
            };

            var client3MessageReceived = new AutoResetEvent(false);

            client3.OnMessageReceived += (msg) =>
            {
                logger.Write <IntegrationTest>("Client 3 received message: " + msg);
                client3MessageReceived.Set();
                Assert.Equal("Boo!", msg);
            };

            var client5MessageReceived = new AutoResetEvent(false);

            client5.OnMessageReceived += (msg) =>
            {
                logger.Write <IntegrationTest>("Client 5 received message: " + msg);
                client5MessageReceived.Set();
                Assert.Equal("Moo!", msg);
            };

            try
            {
                client1SubscribedEvent.WaitAndThrowErrorIfNoSignalIsSet(DefaultWaitTIme, "Client 1 never received SubscribedEvent");
                client2SubscribedEvent.WaitAndThrowErrorIfNoSignalIsSet(DefaultWaitTIme, "Client 2 never received SubscribedEvent");
                client3SubscribedEvent.WaitAndThrowErrorIfNoSignalIsSet(DefaultWaitTIme, "Client 3 never received SubscribedEvent");
                client4SubscribedEvent.WaitAndThrowErrorIfNoSignalIsSet(DefaultWaitTIme, "Client 4 never received SubscribedEvent");
                client5SubscribedEvent.WaitAndThrowErrorIfNoSignalIsSet(DefaultWaitTIme, "Client 5 never received SubscribedEvent");
            }
            catch (ApplicationException e)
            {
                logger.Dispose();
                serverLogger.Dispose();
                throw e;
            }


            // When
            logger.Write <IntegrationTest>("Sending message from client 1.");
            client1.SendMessage("[SkeletonEvent] Boo!");
            logger.Write <IntegrationTest>("Sending message from client 4.");
            client4.SendMessage("[CowEvent] Moo!");

            // Then
            try
            {
                client2MessageReceived.WaitAndThrowErrorIfNoSignalIsSet(DefaultWaitTIme, "Never received message from client 1");
                client3MessageReceived.WaitAndThrowErrorIfNoSignalIsSet(DefaultWaitTIme, "Never received message from client 1");
                client5MessageReceived.WaitAndThrowErrorIfNoSignalIsSet(DefaultWaitTIme, "Never received message from client 4");
            }
            catch (ApplicationException e)
            {
                logger.Dispose();
                serverLogger.Dispose();
                throw e;
            }

            // Finally
            server.Shutdown();
            logger.Write <IntegrationTest>("Integration test done.");
            serverLogger.Dispose();
        }
 public MessageStreamReaderTest()
 {
     _logger = LogSetup.CreateLogger();
 }