示例#1
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);
        }