public void ConcurrentEnqueue()
        {
            ConcurrentQueue <string> result = new ConcurrentQueue <string>();

            QueueWorkerBus.CreateDequeuers("d:\\_Queue\\ENQUEUE",
                                           d => Thread.Sleep(1000),
                                           loopInterval: 1000,
                                           idleLoopCountBeforeStopping: 0,
                                           workersCountForEachQueue: 10);
            Parallel.For(0, 100, _ =>
            {
                //检查并发会不会造成一个队列的Workers创建了多次
                QueueWorkerBus.Enqueue(queryRoot, $"A", Guid.NewGuid().ToString());
            });
            Thread.Sleep(5000);

            QueueWorkerBus.StopAllDequeuers();
        }
        public void EnqueueAndDequeueByWorkBus()
        {
            int times = 10;

            string[] data = new[] { "000", "111", "222", "333", "444", "555" };
            using (var queueC = new PersistentQueue(queryRoot.PathCombine("A")))
                using (var session = queueC.OpenSession())
                {
                    Parallel.For(0, times, _ =>
                                 session.Enqueue(Encoding.UTF8.GetBytes(data[0])));
                    session.Flush();
                }

            ConcurrentBag <string> result = new ConcurrentBag <string>();

            QueueWorkerBus.CreateDequeuers(queryRoot, result.Add,
                                           loopInterval: 1000,
                                           idleLoopCountBeforeStopping: 3,
                                           workersCountForEachQueue: 4);
            Parallel.For(0, times, _ =>
            {
                QueueWorkerBus.Enqueue(queryRoot, "A", data[1]);
                QueueWorkerBus.Enqueue(queryRoot, "A", data[2]);
                QueueWorkerBus.Enqueue(queryRoot, "B", data[3]);
            });

            // 3*1000=3秒后,转入休眠
            Thread.Sleep(10000);

            // 监测任务每10秒工作一次,发现有任务唤醒工作任务
            Parallel.For(0, times, _ =>
            {
                QueueWorkerBus.Enqueue(queryRoot, "B", data[4]);
                QueueWorkerBus.Enqueue(queryRoot, "C", data[5]);
            });

            Thread.Sleep(15000);

            QueueWorkerBus.StopAllDequeuers();

            Assert.AreEqual(data.Length * times, result.Count);
            Assert.AreEqual(3, Directory.GetDirectories(queryRoot).Length);
        }
        public void ConcurrentEnqueueUsingDB()
        {
            using (var db = new TestModel())
            {
                db.Database.ExecuteSqlCommand("delete from TestTables");
            }
            ConcurrentQueue <string> result = new ConcurrentQueue <string>();

            QueueWorkerBus.CreateDequeuers("d:\\_Queue\\ENQUEUE", d =>
            {
                using (var db = new TestModel())
                {
                    //模拟一些读写开销
                    var data = db.TestTables.FirstOrDefault(row => row.TestColumn1 == d);
                    if (data == null)
                    {
                        db.TestTables.Add(new TestTable {
                            TestColumn1 = d
                        });
                    }
                    db.SaveChanges();
                }
            }, 1000, 0, 10);
            Parallel.For(0, 100, _ =>
            {
                //检查并发会不会造成一个队列的Workers创建了多次
                QueueWorkerBus.Enqueue(queryRoot, $"A", Guid.NewGuid().ToString());
            });
            Thread.Sleep(5000);

            QueueWorkerBus.StopAllDequeuers();

            using (var db = new TestModel())
            {
                Assert.AreEqual(100, db.TestTables.Count());
            }
        }
        private void DoQueueWorkFactoryQueueCountStressTest(int queueCount)
        {
            //this create 1000 queue files
            Parallel.For(0, queueCount, i =>
            {
                using (var queueC = new PersistentQueue($"d:\\_Queue\\STRESS\\{i}"))
                    using (var session = queueC.OpenSession())
                    {
                        if (i % 2 == 0)
                        {
                            //仅有一半的队列初始化有数据,这样剩下一半会Lazy加载
                            session.Enqueue(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
                            session.Flush();
                        }
                    }
            });

            //Load 1000 queues and start 1000 workers

            ConcurrentQueue <string> result = new ConcurrentQueue <string>();

            //修改这几个参数测试性能与线程数量
            QueueWorkerBus.CreateDequeuers("d:\\_Queue\\STRESS", result.Enqueue, 1000, 0, 2);

            Parallel.For(0, queueCount, i =>
            {
                QueueWorkerBus.Enqueue(queryRoot, $"{i}", Guid.NewGuid().ToString());
            });

            Thread.Sleep(15000);

            QueueWorkerBus.StopAllDequeuers();

            Assert.AreEqual(queueCount * 1.5, result.Count);
            Assert.AreEqual(queueCount, Directory.GetDirectories("d:\\_Queue\\STRESS").Length);
        }