示例#1
0
文件: UnitTest1.cs 项目: iarly/chat
        public void Should_Deserialize()
        {
            var expectedConnectionUid = Guid.NewGuid();
            var text = "{\"$type\":\"Chat.Server.Domain.Commands.SendMessageCommand, Chat.Server.Domain\",\"TargetClientNickname\":null,\"Content\":null,\"Private\":false,\"IsTargeted\":false,\"ConnectionUid\":\"" + expectedConnectionUid.ToString() + "\"}";

            var command = CommandSerializer.Deserialize(text);

            Assert.IsAssignableFrom <SendMessageCommand>(command);
            Assert.AreEqual(expectedConnectionUid, command.ConnectionUid);
        }
示例#2
0
        static void HandleCommand(byte[] buf, int size)
        {
            Commands          k       = new Commands();
            CommandHandler    handler = new CommandHandler();
            CommandSerializer sr      = new CommandSerializer();
            var compressedbuf         = Compression.Decompress(buf);

            k = sr.Deserialize(compressedbuf);
            Console.WriteLine("Got new data, length {0} bytes", size);
            handler.Handle(k);
        }
示例#3
0
        public void UpdatePlayersCommand_SerializationRoundTrip()
        {
            var cmd = new UpdatePlayersCommand();

            cmd.Players = new[] { "Test1", "Test2" };

            string json = Serializer.Serialize(cmd);

            Assert.IsFalse(string.IsNullOrWhiteSpace(json));
            Assert.IsTrue(json.Contains("Test1"));
            Assert.IsTrue(json.Contains("Test2"));

            if (Serializer.Deserialize(json) is UpdatePlayersCommand cmd2)
            {
                Assert.IsTrue(cmd.Players.Length == 2);
                Assert.AreEqual("Test1", cmd.Players[0]);
                Assert.AreEqual("Test2", cmd.Players[1]);
            }
            else
            {
                Assert.Fail();
            }
        }
示例#4
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);
        }