Exemplo n.º 1
0
        /// <summary>
        /// TCP 服务测试
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        protected static bool testCase(IServer client)
        {
            using (client as IDisposable)
                using (System.Threading.AutoResetEvent wait = new System.Threading.AutoResetEvent(false))
                {
                    IntValue = 0;
                    client.Inc();
                    if (IntValue != 1)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue - 2;
                    client.IncLong();
                    if (LongValue != long.MaxValue - 1)
                    {
                        return(false);
                    }

                    client.Set(3);
                    if (IntValue != 3)
                    {
                        return(false);
                    }

                    client.Set(long.MaxValue - 3);
                    if (LongValue != long.MaxValue - 3)
                    {
                        return(false);
                    }

                    client.Set("5");
                    if (IntValue != 5)
                    {
                        return(false);
                    }

                    client.Add(4, 7);
                    if (IntValue != 11)
                    {
                        return(false);
                    }

                    client.Add(long.MaxValue >> 1, (long.MaxValue >> 1) - 1);
                    if (LongValue != ((long.MaxValue >> 1) << 1) - 1)
                    {
                        return(false);
                    }

                    client.Add("6", "11");
                    if (IntValue != 17)
                    {
                        return(false);
                    }

                    IntValue = 9;
                    if (client.GetInc() != 10)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue - 9;
                    if (client.GetIncLong() != long.MaxValue - 8)
                    {
                        return(false);
                    }

                    IntValue = 12;
                    if (client.GetIncString() != "13")
                    {
                        return(false);
                    }

                    IntValue = 4;
                    if (client.GetAdd(3) != 7)
                    {
                        return(false);
                    }
                    LongValue = (long.MaxValue >> 2) - 1;
                    if (client.GetAdd((long.MaxValue >> 2) - 2) != ((long.MaxValue >> 2) << 1) - 3)
                    {
                        return(false);
                    }
                    IntValue = 3;
                    if (client.GetAdd("11") != "14")
                    {
                        return(false);
                    }

                    if (client.GetAdd(8, 14) != 22)
                    {
                        return(false);
                    }
                    if (client.GetAdd(long.MaxValue >> 2, (long.MaxValue >> 2) - 1) != ((long.MaxValue >> 2) << 1) - 1)
                    {
                        return(false);
                    }
                    if (client.GetAdd("3", "15") != "18")
                    {
                        return(false);
                    }

                    IntValue = 4;
                    int outInt;
                    if (client.GetAdd(5, ref IntValue, out outInt) != 14)
                    {
                        return(false);
                    }
                    if (outInt != 14)
                    {
                        return(false);
                    }
                    if (IntValue != 9)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue >> 3;
                    long outLong;
                    if (client.GetAdd((long.MaxValue >> 3) - 5, ref LongValue, out outLong) != (long.MaxValue >> 3) * 3 - 10)
                    {
                        return(false);
                    }
                    if (outLong != (long.MaxValue >> 3) * 3 - 10)
                    {
                        return(false);
                    }
                    if (LongValue != ((long.MaxValue >> 3) << 1) - 5)
                    {
                        return(false);
                    }

                    string refString = "6", outString;
                    if (client.GetAdd("10", ref refString, out outString) != "26")
                    {
                        return(false);
                    }
                    if (outString != "26")
                    {
                        return(false);
                    }
                    if (refString != "16")
                    {
                        return(false);
                    }

                    IntValue = 0;
                    client.IncSynchronous();
                    if (IntValue != 1)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue - 2;
                    client.IncLongSynchronous();
                    if (LongValue != long.MaxValue - 1)
                    {
                        return(false);
                    }

                    client.AddSynchronous(4, 7);
                    if (IntValue != 11)
                    {
                        return(false);
                    }

                    client.AddSynchronous(long.MaxValue >> 1, (long.MaxValue >> 1) - 1);
                    if (LongValue != ((long.MaxValue >> 1) << 1) - 1)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue >> 3;
                    long outLongSynchronous;
                    if (client.GetAddSynchronous((long.MaxValue >> 3) - 5, ref LongValue, out outLongSynchronous) != (long.MaxValue >> 3) * 3 - 10)
                    {
                        return(false);
                    }
                    if (outLongSynchronous != (long.MaxValue >> 3) * 3 - 10)
                    {
                        return(false);
                    }
                    if (LongValue != ((long.MaxValue >> 3) << 1) - 5)
                    {
                        return(false);
                    }

                    string refStringSynchronous = "6", outStringSynchronous;
                    if (client.GetAddSynchronous("10", ref refStringSynchronous, out outStringSynchronous) != "26")
                    {
                        return(false);
                    }
                    if (outStringSynchronous != "26")
                    {
                        return(false);
                    }
                    if (refStringSynchronous != "16")
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue - 2;
                    bool isAsynchronous = false;
                    client.IncLong(value =>
                    {
                        isAsynchronous = value.Type == AutoCSer.Net.TcpServer.ReturnType.Success;
                        wait.Set();
                        return(true);
                    });
                    wait.WaitOne();
                    if (!isAsynchronous || LongValue != long.MaxValue - 1)
                    {
                        return(false);
                    }

                    isAsynchronous = false;
                    client.Add(long.MaxValue >> 1, (long.MaxValue >> 1) - 1, value =>
                    {
                        isAsynchronous = value.Type == AutoCSer.Net.TcpServer.ReturnType.Success;
                        wait.Set();
                        return(true);
                    });
                    wait.WaitOne();
                    if (!isAsynchronous || LongValue != ((long.MaxValue >> 1) << 1) - 1)
                    {
                        return(false);
                    }

                    isAsynchronous = false;
                    client.GetAdd(8, 14, value =>
                    {
                        isAsynchronous = value.Type == AutoCSer.Net.TcpServer.ReturnType.Success && value.Value.Left == 8 && value.Value.Right == 14 && value.Value.Result == 22;
                        wait.Set();
                        return(true);
                    });
                    wait.WaitOne();
                    if (!isAsynchronous)
                    {
                        return(false);
                    }

                    isAsynchronous = false;
                    client.GetAdd(long.MaxValue >> 2, (long.MaxValue >> 2) - 1, value =>
                    {
                        isAsynchronous = value.Type == AutoCSer.Net.TcpServer.ReturnType.Success && value.Value == ((long.MaxValue >> 2) << 1) - 1;
                        wait.Set();
                        return(true);
                    });
                    wait.WaitOne();
                    if (!isAsynchronous)
                    {
                        return(false);
                    }

                    isAsynchronous = false;
                    client.GetAdd("3", "15", value =>
                    {
                        isAsynchronous = value.Type == AutoCSer.Net.TcpServer.ReturnType.Success && value.Value == "18";
                        wait.Set();
                        return(true);
                    });
                    wait.WaitOne();
                    if (!isAsynchronous)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue - 4;
                    int keepCallbackCount = 2, successCount = 2;
                    using (AutoCSer.Net.TcpServer.KeepCallback keepCallback = client.IncLongKeepCallback(value =>
                    {
                        if (value.Type == AutoCSer.Net.TcpServer.ReturnType.Success)
                        {
                            --successCount;
                        }
                        if (--keepCallbackCount == 0)
                        {
                            wait.Set();
                        }
                        return(true);
                    }))
                    {
                        wait.WaitOne();
                        if (successCount != 0 || LongValue != long.MaxValue - 2)
                        {
                            return(false);
                        }
                    }

                    keepCallbackCount = successCount = 2;
                    using (AutoCSer.Net.TcpServer.KeepCallback keepCallback = client.GetAddKeepCallback(8, 14, value =>
                    {
                        if (value.Type == AutoCSer.Net.TcpServer.ReturnType.Success)
                        {
                            switch (value.Value)
                            {
                            case 22:
                            case 25: --successCount; break;
                            }
                        }
                        if (--keepCallbackCount == 0)
                        {
                            wait.Set();
                        }
                        return(true);
                    }))
                    {
                        wait.WaitOne();
                        if (successCount != 0)
                        {
                            return(false);
                        }
                    }

                    if (client.Expired().Type != AutoCSer.Net.TcpServer.ReturnType.VersionExpired)
                    {
                        return(false);
                    }
                    if (client.ServerDeSerializeError().Type != AutoCSer.Net.TcpServer.ReturnType.ServerDeSerializeError)
                    {
                        return(false);
                    }

                    //try
                    //{
                    //    client.ThrowException();
                    //    return false;
                    //}
                    //catch { }

                    //AutoCSer.Net.TcpServer.ReturnValue value = client.ReturnThrowException();
                    //if (value.Type != AutoCSer.Net.TcpServer.ReturnType.ServerException) return false;

                    return(true);
                }
        }
Exemplo n.º 2
0
        /// <summary>
        /// TCP 服务测试
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        protected static bool testCase(IServer client)
        {
            using (client as IDisposable)
                using (System.Threading.AutoResetEvent wait = new System.Threading.AutoResetEvent(false))
                {
                    IntValue = 0;
                    client.Inc();
                    if (IntValue != 1)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue - 2;
                    client.IncLong();
                    if (LongValue != long.MaxValue - 1)
                    {
                        return(false);
                    }

                    client.Set(3);
                    if (IntValue != 3)
                    {
                        return(false);
                    }

                    client.Set(long.MaxValue - 3);
                    if (LongValue != long.MaxValue - 3)
                    {
                        return(false);
                    }

                    client.Set("5");
                    if (IntValue != 5)
                    {
                        return(false);
                    }

                    client.Add(4, 7);
                    if (IntValue != 11)
                    {
                        return(false);
                    }

                    client.Add(long.MaxValue >> 1, (long.MaxValue >> 1) - 1);
                    if (LongValue != ((long.MaxValue >> 1) << 1) - 1)
                    {
                        return(false);
                    }

                    client.Add("6", "11");
                    if (IntValue != 17)
                    {
                        return(false);
                    }

                    IntValue = 9;
                    if (client.GetInc() != 10)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue - 9;
                    if (client.GetIncLong() != long.MaxValue - 8)
                    {
                        return(false);
                    }

                    IntValue = 12;
                    if (client.GetIncString() != "13")
                    {
                        return(false);
                    }

                    IntValue = 4;
                    if (client.GetAdd(3) != 7)
                    {
                        return(false);
                    }
                    LongValue = (long.MaxValue >> 2) - 1;
                    if (client.GetAdd((long.MaxValue >> 2) - 2) != ((long.MaxValue >> 2) << 1) - 3)
                    {
                        return(false);
                    }
                    IntValue = 3;
                    if (client.GetAdd("11") != "14")
                    {
                        return(false);
                    }

                    if (client.GetAdd(8, 14) != 22)
                    {
                        return(false);
                    }
                    if (client.GetAdd(long.MaxValue >> 2, (long.MaxValue >> 2) - 1) != ((long.MaxValue >> 2) << 1) - 1)
                    {
                        return(false);
                    }
                    if (client.GetAdd("3", "15") != "18")
                    {
                        return(false);
                    }

                    IntValue = 4;
                    int outInt;
                    if (client.GetAdd(5, ref IntValue, out outInt) != 14)
                    {
                        return(false);
                    }
                    if (outInt != 14)
                    {
                        return(false);
                    }
                    if (IntValue != 9)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue >> 3;
                    long outLong;
                    if (client.GetAdd((long.MaxValue >> 3) - 5, ref LongValue, out outLong) != (long.MaxValue >> 3) * 3 - 10)
                    {
                        return(false);
                    }
                    if (outLong != (long.MaxValue >> 3) * 3 - 10)
                    {
                        return(false);
                    }
                    if (LongValue != ((long.MaxValue >> 3) << 1) - 5)
                    {
                        return(false);
                    }

                    string refString = "6", outString;
                    if (client.GetAdd("10", ref refString, out outString) != "26")
                    {
                        return(false);
                    }
                    if (outString != "26")
                    {
                        return(false);
                    }
                    if (refString != "16")
                    {
                        return(false);
                    }

                    IntValue = 0;
                    client.IncSynchronous();
                    if (IntValue != 1)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue - 2;
                    client.IncLongSynchronous();
                    if (LongValue != long.MaxValue - 1)
                    {
                        return(false);
                    }

                    client.AddSynchronous(4, 7);
                    if (IntValue != 11)
                    {
                        return(false);
                    }

                    client.AddSynchronous(long.MaxValue >> 1, (long.MaxValue >> 1) - 1);
                    if (LongValue != ((long.MaxValue >> 1) << 1) - 1)
                    {
                        return(false);
                    }

                    LongValue = long.MaxValue >> 3;
                    long outLongSynchronous;
                    if (client.GetAddSynchronous((long.MaxValue >> 3) - 5, ref LongValue, out outLongSynchronous) != (long.MaxValue >> 3) * 3 - 10)
                    {
                        return(false);
                    }
                    if (outLongSynchronous != (long.MaxValue >> 3) * 3 - 10)
                    {
                        return(false);
                    }
                    if (LongValue != ((long.MaxValue >> 3) << 1) - 5)
                    {
                        return(false);
                    }

                    string refStringSynchronous = "6", outStringSynchronous;
                    if (client.GetAddSynchronous("10", ref refStringSynchronous, out outStringSynchronous) != "26")
                    {
                        return(false);
                    }
                    if (outStringSynchronous != "26")
                    {
                        return(false);
                    }
                    if (refStringSynchronous != "16")
                    {
                        return(false);
                    }

                    if (client.Expired().Type != AutoCSer.Net.TcpServer.ReturnType.VersionExpired)
                    {
                        return(false);
                    }
                    if (client.ServerDeSerializeError().Type != AutoCSer.Net.TcpServer.ReturnType.ServerDeSerializeError)
                    {
                        return(false);
                    }

                    //try
                    //{
                    //    client.ThrowException();
                    //    return false;
                    //}
                    //catch { }

                    //AutoCSer.Net.TcpServer.ReturnValue value = client.ReturnThrowException();
                    //if (value.Type != AutoCSer.Net.TcpServer.ReturnType.ServerException) return false;

                    return(true);
                }
        }