예제 #1
0
        public async Task Should_Receive_Two_Commands_From_Client()
        {
            // arrange
            AutoResetEvent          responseWaiter    = new AutoResetEvent(false);
            CancellationTokenSource cancellationToken = new CancellationTokenSource(TestTimeout);

            TextualContent expectedContent1 = new TextualContent("Hello my world!");
            TextualContent expectedContent2 = new TextualContent("My second message for you");

            TextualContent actualContent1 = null;
            TextualContent actualContent2 = null;

            SocketCommunicator.OnClientSendCommand += (connectionUid, command) =>
            {
                var actualCommand = command as SendMessageCommand;

                if (actualContent1 == null)
                {
                    actualContent1 = actualCommand.Content as TextualContent;
                }
                else
                {
                    actualContent2 = actualCommand.Content as TextualContent;
                    responseWaiter.Set();
                }

                return(Task.CompletedTask);
            };

            // act when server is ready
            WhenSocketIsReady(cancellationToken, () =>
            {
                ClientConnection client = new ClientConnection("localhost", 33000);

                client.Send(CommandSerializer.Serializer(new SendMessageCommand {
                    Content = expectedContent1
                }));
                client.Send(CommandSerializer.Serializer(new SendMessageCommand {
                    Content = expectedContent2
                }));

                responseWaiter.WaitOne(ReceiveMessageTimeout);
            });

            await SocketCommunicator.ListenAsync(cancellationToken.Token);

            // assert
            Assert.AreEqual(expectedContent1.Text, actualContent1.Text);
            Assert.AreEqual(expectedContent2.Text, actualContent2.Text);
        }
예제 #2
0
        public async Task Should_Send_Command_To_Client()
        {
            // arrange
            PropagateMessageCommand expectedCommand = new PropagateMessageCommand
            {
                Content = new TextualContent("Welcome to my world!")
            };

            PropagateMessageCommand actualCommand     = null;
            ManualResetEvent        connectionWaiter  = new ManualResetEvent(false);
            CancellationTokenSource cancellationToken = new CancellationTokenSource(TestTimeout);

            TextualContent expectedContent     = expectedCommand.Content as TextualContent;
            TextualContent actualContent       = null;
            Guid           actualConnectionUid = default;

            SocketCommunicator.OnClientConnected += (connectionUid) =>
            {
                actualConnectionUid = connectionUid;
                connectionWaiter.Set();
                return(Task.CompletedTask);
            };

            // act when server is ready
            WhenSocketIsReady(cancellationToken, () =>
            {
                ClientConnection client = new ClientConnection("localhost", 33000);

                connectionWaiter.WaitOne(ReceiveMessageTimeout);

                SocketCommunicator.PublishAsync(actualConnectionUid, expectedCommand);

                var receivedText = client.Receive();

                actualCommand = CommandSerializer.Deserialize(receivedText) as PropagateMessageCommand;
                actualContent = actualCommand.Content as TextualContent;
            });

            await SocketCommunicator.ListenAsync(cancellationToken.Token);

            // assert
            Assert.AreEqual(expectedContent.Text, actualContent.Text);
        }
예제 #3
0
        public async Task Should_Receive_Command_From_Client()
        {
            // arrange
            SendMessageCommand expectedCommand = new SendMessageCommand
            {
                Content = new TextualContent("Hello my world!")
            };

            SendMessageCommand      actualCommand     = null;
            ManualResetEvent        responseWaiter    = new ManualResetEvent(false);
            CancellationTokenSource cancellationToken = new CancellationTokenSource(TestTimeout);

            TextualContent expectedContent = expectedCommand.Content as TextualContent;
            TextualContent actualContent   = null;

            SocketCommunicator.OnClientSendCommand += (connectionUid, command) =>
            {
                actualCommand = command as SendMessageCommand;
                actualContent = actualCommand.Content as TextualContent;
                responseWaiter.Set();

                return(Task.CompletedTask);
            };

            // act when server is ready
            WhenSocketIsReady(cancellationToken, () =>
            {
                ClientConnection client = new ClientConnection("localhost", 33000);

                client.Send(CommandSerializer.Serializer(expectedCommand));

                responseWaiter.WaitOne(ReceiveMessageTimeout);
            });

            await SocketCommunicator.ListenAsync(cancellationToken.Token);

            // assert
            Assert.AreEqual(expectedContent.Text, actualContent.Text);
        }