示例#1
0
文件: UnitTest1.cs 项目: iarly/chat
        public void Should_Serialize()
        {
            var expectedText = "{\"$type\":\"Chat.Server.Domain.Commands.SendMessageCommand, Chat.Server.Domain\",\"TargetClientNickname\":null,\"Content\":null,\"Private\":false,\"IsTargeted\":false,\"ConnectionUid\":\"00000000-0000-0000-0000-000000000000\"}";
            var text         = CommandSerializer.Serializer(new SendMessageCommand());

            Assert.AreEqual(expectedText, text);
        }
示例#2
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);
        }