Ejemplo n.º 1
0
        public void TestChannelPackerErrorDecode()
        {
            var fromClient = new byte[] { };

            using (var server = new TcpServer((data) =>
            {
                fromClient = Arr.Merge(fromClient, data);
            }))
            {
                var socket  = MakeEnv().Make <ISocketManager>().Make("tcp://127.0.0.1", "TestChannelPackerErrorDecode");
                var channel = new NetworkChannel(socket, new PackerError(PackerError.ErrorType.Decode), "TestChannelPackerErrorDecode");

                var wait  = channel.Connect("127.0.0.1", 7777);
                var count = 0;
                while (!wait.IsDone && count++ < 3000)
                {
                    Thread.Sleep(1);
                }
                try
                {
                    string err = string.Empty;
                    channel.OnError += (c, ex) =>
                    {
                        if (err == string.Empty)
                        {
                            err = ex.ToString();
                        }
                    };

                    channel.Send(Encoding.Default.GetBytes("helloworld"));
                    count = 0;
                    while (channel.Socket.Connected && count++ < 3000)
                    {
                        Thread.Sleep(1);
                    }

                    Assert.AreEqual(false, channel.Socket.Connected);
                    Assert.AreEqual("CatLib.RuntimeException: exception decode", err);
                }
                finally
                {
                    channel.Disconnect();
                }
            }
        }
Ejemplo n.º 2
0
        public void TestChannelPackerErrorEncode()
        {
            var fromClient = new byte[] { };

            using (var server = new TcpServer((data) =>
            {
                fromClient = Arr.Merge(fromClient, data);
            }))
            {
                var socket  = MakeEnv().Make <ISocketManager>().Make("tcp://127.0.0.1", "TestChannelPackerErrorEncode");
                var channel = new NetworkChannel(socket, new PackerError(PackerError.ErrorType.Encode), "TestChannelPackerErrorEncode");

                var wait  = channel.Connect("127.0.0.1", 7777);
                var count = 0;
                while (!wait.IsDone && count++ < 3000)
                {
                    Thread.Sleep(1);
                }
                try
                {
                    var isThrow = false;
                    try
                    {
                        channel.Send(Encoding.Default.GetBytes("helloworld"));
                    }
                    catch (RuntimeException)
                    {
                        isThrow = true;
                    }
                    Assert.AreEqual(true, isThrow);
                }
                finally
                {
                    channel.Disconnect();
                }
            }
        }