/// <summary> /// 消息分发测试 /// </summary> /// <param name="client"></param> /// <returns></returns> internal static bool TestCase(AutoCSer.CacheServer.Client client) { string name = "MesssageDistributor"; #region 除名称为 MesssageDistributor 的缓存数据,防止未处理数据对测试验证产生副作用 client.RemoveDataStructure(name); #endregion #region 创建名称为 MesssageDistributor 的 JSON 消息分发 Distributor <Json <int> > distributor = client.GetOrCreateDataStructure <Distributor <Json <int> > >(name).Value; if (distributor == null) { return(false); } #endregion #region 添加消息 2 ReturnValue <bool> isEnqueue = distributor.Enqueue(2); if (!isEnqueue.Value) { return(false); } #endregion isEnqueue = distributor.Enqueue(6); if (!isEnqueue.Value) { return(false); } isEnqueue = distributor.Enqueue(4); if (!isEnqueue.Value) { return(false); } #region 初始化消息消费状态(仅用于当前测试验证) checkValue = 0; wait.Reset(); #endregion #region 创建消息分发的消费者 using (AutoCSer.CacheServer.MessageQueue.DistributionConsumer <Json <int>, int> consumer = distributor.CreateConsumer(onMessage, new AutoCSer.CacheServer.MessageQueue.DistributionConsumerConfig { MemoryCacheNodeCount = 0 })) #endregion { #region 等待消息处理完毕 wait.WaitOne(); #endregion } return(checkValue == successValue); }
/// <summary> /// 消息队列测试 /// </summary> /// <param name="queue"></param> /// <returns></returns> private static bool start(Distributor <Json <int> > queue) { ReturnValue <bool> isEnqueue = queue.Enqueue(2); if (!isEnqueue.Value) { return(false); } isEnqueue = queue.Enqueue(4); if (!isEnqueue.Value) { return(false); } isEnqueue = queue.Enqueue(6); if (!isEnqueue.Value) { return(false); } return(true); }
/// <summary> /// 消息分发测试 /// </summary> /// <returns></returns> internal static bool TestCase(Client masterClient, bool isStart) { string name = "messsageDistributor"; Distributor <Json <int> > distributor = masterClient.GetOrCreateDataStructure <Distributor <Json <int> > >(name).Value; if (distributor == null) { return(false); } if (isStart) { if (!start(distributor)) { return(false); } } else { using (MesssageDistributor consumer = new MesssageDistributor(distributor)) { ReturnValue <bool> isEnqueue = distributor.Enqueue(5); if (!isEnqueue.Value) { return(false); } isEnqueue = distributor.Enqueue(3); if (!isEnqueue.Value) { return(false); } isEnqueue = distributor.Enqueue(1); if (!isEnqueue.Value) { return(false); } consumer.wait.WaitOne(); return(consumer.checkValue == successValue); } } return(true); }