public void MessageContextRecordLimtTest() { //测试MessageContext的数量限制 var openId = "olPjZjsXuQPJoV0HlruZkNzKc91E"; //创建 GlobalMessageContext 对象 var globalMessageContext = new GlobalMessageContext <DefaultMpMessageContext, IRequestMessageBase, IResponseMessageBase>(); globalMessageContext.Restore();//清空所有上下文 for (int i = 0; i < 100; i++) { var xml = string.Format(string.Format(xmlTextFormat, "测试限制"), DateTimeHelper.GetUnixDateTime(SystemTime.Now.AddSeconds(i)), SystemTime.NowTicks + i); Init(xml);//初始化 var timestamp = "itsafaketimestamp"; var nonce = "whateveryouwant"; var signature = CheckSignature.GetSignature(timestamp, nonce, WeixinController.Token); var postModel = new PostModel() { Signature = signature, Timestamp = timestamp, Nonce = nonce }; var actual = target.MiniPost(postModel) as FixWeixinBugWeixinResult; Assert.IsNotNull(actual); Console.WriteLine(actual.ToJson()); } //获取指定 openId 的上下文信息 var myMessageContext = globalMessageContext.GetMessageContext(openId); Assert.IsNotNull(myMessageContext); var recordCount = myMessageContext.MaxRecordCount; Console.WriteLine(myMessageContext.RequestMessages[0].ToJson()); Assert.AreEqual(recordCount, myMessageContext.RequestMessages.Count); Assert.AreEqual(recordCount, myMessageContext.ResponseMessages.Count); }
public void DistributedCacheTest(Func <IBaseObjectCacheStrategy> cacheStrategy) { //强制使用本地缓存 CacheStrategyFactory.RegisterObjectCacheStrategy(cacheStrategy); Console.WriteLine($"当前使用缓存:{CacheStrategyFactory.GetObjectCacheStrategyInstance().GetType().FullName}"); //清空缓存 var globalMessageContext = new GlobalMessageContext <CustomMessageContext, RequestMessageBase, ResponseMessageBase>(); globalMessageContext.Restore(); //第一次请求 var dt1 = SystemTime.Now; var doc = XDocument.Parse(textRequestXml.FormatWith("TNT2", CO2NET.Helpers.DateTimeHelper.GetUnixDateTime(SystemTime.Now.UtcDateTime), SystemTime.Now.Ticks)); var messageHandler = new CustomMessageHandler(doc, postModel); var currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(1, currentMessageContext.RequestMessages.Count);//初始化之后,RequestMessage 已经被记录到上下文中 Assert.AreEqual(0, currentMessageContext.ResponseMessages.Count); messageHandler.Execute(); Console.WriteLine($"第 1 次请求耗时:{SystemTime.NowDiff(dt1).TotalMilliseconds} ms"); currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(1, currentMessageContext.RequestMessages.Count); Assert.AreEqual(0, currentMessageContext.ResponseMessages.Count);//默认使用异步ResponseMessage写入,有延迟(有时队列速度太快,也会执行到此处时已经写入,结果为1) //回复消息记录可以使用队列,对时间不敏感,因此需要等待队列完成记录 Thread.Sleep(500); currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(1, currentMessageContext.ResponseMessages.Count);//默认使用异步ResponseMessage写入,有延迟 Console.WriteLine(currentMessageContext.ResponseMessages.Last().GetType()); Console.WriteLine(currentMessageContext.ResponseMessages.Last().ToJson()); //测试 StorageData Assert.AreEqual(1, currentMessageContext.StorageData); var lastResponseMessage = currentMessageContext.ResponseMessages.Last() as ResponseMessageText; Assert.IsNotNull(lastResponseMessage); Assert.AreEqual("来自单元测试:TNT2", lastResponseMessage.Content); //第二次请求 var dt2 = SystemTime.Now; doc = XDocument.Parse(textRequestXml.FormatWith("TNT3", CO2NET.Helpers.DateTimeHelper.GetUnixDateTime(SystemTime.Now.UtcDateTime), SystemTime.Now.Ticks)); messageHandler = new CustomMessageHandler(doc, postModel) { RecordResponseMessageSync = true //设置同步写入 ResponseMessage }; currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(2, currentMessageContext.RequestMessages.Count); Assert.AreEqual(1, currentMessageContext.ResponseMessages.Count); messageHandler.Execute(); Console.WriteLine($"第 2 次请求耗时:{SystemTime.NowDiff(dt2).TotalMilliseconds} ms"); currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(2, currentMessageContext.RequestMessages.Count); Assert.AreEqual(2, currentMessageContext.ResponseMessages.Count);//同步写入 ResponseMessage,没有延迟 lastResponseMessage = currentMessageContext.ResponseMessages.Last() as ResponseMessageText; Assert.IsNotNull(lastResponseMessage); Assert.AreEqual("来自单元测试:TNT3", lastResponseMessage.Content); //测试去重 var dt3 = SystemTime.Now; messageHandler = new CustomMessageHandler(doc, postModel) { RecordResponseMessageSync = true //设置同步写入 ResponseMessage }; //使用和上次同样的请求 currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(2, currentMessageContext.RequestMessages.Count); Assert.AreEqual(2, currentMessageContext.ResponseMessages.Count); messageHandler.Execute(); Console.WriteLine($"第 3 次请求耗时:{SystemTime.NowDiff(dt3).TotalMilliseconds} ms"); //没有变化 currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(2, currentMessageContext.RequestMessages.Count); Assert.AreEqual(2, currentMessageContext.ResponseMessages.Count); lastResponseMessage = currentMessageContext.ResponseMessages.Last() as ResponseMessageText; Assert.IsNotNull(lastResponseMessage); Assert.AreEqual("来自单元测试:TNT3", lastResponseMessage.Content); //测试最大纪录储存 Console.WriteLine("==== 循环测试开始 ===="); for (int i = 0; i < 15; i++) { var dt4 = SystemTime.Now; doc = XDocument.Parse(textRequestXml.FormatWith($"循环测试-{i}", CO2NET.Helpers.DateTimeHelper.GetUnixDateTime(SystemTime.Now.UtcDateTime) + i, SystemTime.Now.Ticks)); var maxRecordCount = 10; messageHandler = new CustomMessageHandler(doc, postModel, maxRecordCount) { RecordResponseMessageSync = true //设置同步写入 ResponseMessage }; //使用和上次同样的请求 //messageHandler.GlobalMessageContext.MaxRecordCount = 10;//在这里设置的话,Request已经插入了,无法及时触发删除多余消息的过程 messageHandler.Execute(); currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(i < 7 ? i + 3 : 10, currentMessageContext.RequestMessages.Count); Assert.AreEqual(i < 7 ? i + 3 : 10, currentMessageContext.ResponseMessages.Count); Console.WriteLine($"第 {i + 1} 次循环测试请求耗时:{SystemTime.NowDiff(dt4).TotalMilliseconds} ms"); } Console.WriteLine("==== 循环测试结束 ===="); //清空 messageHandler.GlobalMessageContext.Restore(); currentMessageContext = messageHandler.GetCurrentMessageContext().GetAwaiter().GetResult(); Assert.AreEqual(0, currentMessageContext.RequestMessages.Count); //回复消息记录可以使用队列,对时间不敏感,因此需要等待队列完成记录 Thread.Sleep(300); Assert.AreEqual(0, currentMessageContext.ResponseMessages.Count); Console.WriteLine(); }