Пример #1
0
        public void Test_PerformanceMoudle_测试HTTP请求执行超时场景()
        {
            // 测试 HTTP 请求执行超时场景
            using (WebContext context = HttpInfoTest.CreateWebContext()) {
                PerformanceModule module = new PerformanceModule();


                System.Threading.Thread.Sleep(WriterFactory.Config.TimerPeriod + 1000);
                // 暂停异步写入
                SetAsyncWriteEnabled(false);

                // 这个调用仅仅为了覆盖代码,没什么具体意义
                module.Init(context.Application.Instance);

                // 代替 HttpApplicaton 触发 PreRequestHandlerExecute事件
                MethodInfo method = module.GetType().GetMethod("app_PreRequestHandlerExecute", BindingFlags.Instance | BindingFlags.NonPublic);
                method.Invoke(module, new object[] { context.Application.Instance, null });

                // 获取结果,检查上面的调用是否成功
                object value = context.HttpContext.Items["289e8920-b291-4167-80b0-793cd46cad22"];
                Assert.IsTrue(value.GetType() == typeof(DateTime));


                // 模拟超时
                System.Threading.Thread.Sleep(WriterFactory.Config.Performance.HttpExecuteTimeout + 1000);


                // 代替 HttpApplicaton 触发 PostRequestHandlerExecute事件
                method = module.GetType().GetMethod("app_PostRequestHandlerExecute", BindingFlags.Instance | BindingFlags.NonPublic);
                method.Invoke(module, new object[] { context.Application.Instance, null });

                // 获取调用 LogHelper.Write(info); 的数据

                List <PerformanceInfo> list = GetQueueData();

                Assert.AreEqual(1, list.Count);

                PerformanceInfo performanceInfo = list[0];
                Assert.AreEqual("http://www.bing.com/sfdjosfdj/slfjsfj/sdjfosf.aspx", performanceInfo.HttpInfo.Url);

                // 启用异步写入
                SetAsyncWriteEnabled(true);

                module.Dispose();
            }
        }
Пример #2
0
        public void Test2()
        {
            // 测试 HTTP 请求执行超时场景
            using (WebContext context = HttpInfoTest.CreateWebContext()) {
                context.SetUserName("fish li");
                // 测试 SQL 执行超时场景

                PerformanceModule module = new PerformanceModule();

                System.Threading.Thread.Sleep(WriterFactory.Config.TimerPeriod + 1000);
                // 暂停异步写入
                SetAsyncWriteEnabled(false);


                TimeSpan  timeSpan = TimeSpan.FromSeconds(2d);
                DbCommand command  = SqlInfoTest.CreateDbCommand();

                // 第一次调用,第一个参数command = null
                PerformanceModule.CheckDbExecuteTime(null, timeSpan);

                // 第二次调用
                PerformanceModule.CheckDbExecuteTime(command, timeSpan);



                // 获取调用 LogHelper.Write(info); 的数据
                List <PerformanceInfo> list = GetQueueData();

                Assert.AreEqual(1, list.Count);

                PerformanceInfo performanceInfo = list[0];
                Assert.AreEqual(command.CommandText, performanceInfo.SqlInfo.SqlText.ToString());

                Assert.IsNotNull(performanceInfo.HttpInfo);
                Assert.AreEqual(context.HttpContext.Request.Url.OriginalString, performanceInfo.HttpInfo.Url);
                Console.WriteLine(performanceInfo.HttpInfo.Url);
                Assert.AreEqual("fish li", performanceInfo.HttpInfo.UserName);

                // 启用异步写入
                SetAsyncWriteEnabled(true);
            }
        }
Пример #3
0
        public void Test_LogHelper_Write_DbCommand()
        {
            using (WebContext context = HttpInfoTest.CreateWebContext()) {
                context.SetUserName("Fish Li");
                context.AddSession("session-1", "aaaaaaaaaaaa");
                context.AddSession("session-2", DateTime.Now);
                context.AddSession("session-3", null);
                context.Request.SetInputStream("a=1&b=2");

                DbCommand command = SqlInfoTest.CreateDbCommand();


                Exception     ex   = CreateException("Test: HttpInfo.Create");
                ExceptionInfo info = ExceptionInfo.Create(ex, context.HttpContext, command);
                info.Addition = Guid.NewGuid().ToString();

                LogHelper.SyncWrite(info);

                LogHelperTest.AssertWriteOK(info.Addition);

                AssertWriteOK(ex.Message);
            }
        }