public void TestCompleteCurrentBatch()
        {
            const int batchSize             = 10;
            BlockingBatchingQueue <int> col = new BlockingBatchingQueue <int>(batchSize: batchSize);

            Assert.AreEqual(0, col.Count);
            Assert.AreEqual(0, col.CompletedBatchCount);

            Assert.IsFalse(col.CompleteCurrentBatch());
            Assert.AreEqual(0, col.Count);
            Assert.AreEqual(0, col.CompletedBatchCount);


            int[] dequeuedItems = null;

            Assert.IsFalse(col.TryTake(out dequeuedItems));
            col.Add(0);
            col.Add(1);
            Assert.AreEqual(2, col.Count);
            Assert.AreEqual(0, col.CompletedBatchCount);


            Assert.IsTrue(col.CompleteCurrentBatch());
            Assert.AreEqual(2, col.Count);
            Assert.AreEqual(1, col.CompletedBatchCount);


            Assert.IsTrue(col.TryTake(out dequeuedItems));
            Assert.AreEqual(2, dequeuedItems.Length);
            for (int i = 0; i < dequeuedItems.Length; i++)
            {
                Assert.AreEqual(i, dequeuedItems[i]);
            }

            Assert.AreEqual(0, col.Count);
            Assert.AreEqual(0, col.CompletedBatchCount);
        }
        private void RunComplexTest(BlockingBatchingQueue <int> q, int elemCount, int thCount)
        {
            int atomicRandom = 0;

            int trackElemCount = elemCount;
            int addFinished    = 0;

            Thread[] threadsTake         = new Thread[thCount];
            Thread[] threadsAdd          = new Thread[thCount];
            Thread   completeBatchThread = null;

            CancellationTokenSource tokSrc = new CancellationTokenSource();

            List <int> global = new List <int>(elemCount);

            Action addAction = () =>
            {
                Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * thCount * 2);

                while (true)
                {
                    int item = Interlocked.Decrement(ref trackElemCount);
                    if (item < 0)
                    {
                        break;
                    }

                    q.Add(item);

                    int sleepTime = rnd.Next(12);

                    if (sleepTime > 0)
                    {
                        SpinWaitHelper.SpinWait(sleepTime);
                    }
                }

                Interlocked.Increment(ref addFinished);
            };


            Action takeAction = () =>
            {
                Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * thCount * 2);

                List <int> data = new List <int>();

                try
                {
                    while (Volatile.Read(ref addFinished) < thCount)
                    {
                        int[] tmp;
                        if (q.TryTake(out tmp, -1, tokSrc.Token))
                        {
                            data.AddRange(tmp);
                        }

                        int sleepTime = rnd.Next(12);
                        if (sleepTime > 0)
                        {
                            SpinWaitHelper.SpinWait(sleepTime);
                        }
                    }
                }
                catch (OperationCanceledException) { }

                int[] tmp2;
                while (q.TryTake(out tmp2))
                {
                    data.AddRange(tmp2);
                }

                q.CompleteCurrentBatch();
                while (q.TryTake(out tmp2))
                {
                    data.AddRange(tmp2);
                }

                lock (global)
                    global.AddRange(data);
            };

            Action completeBatchAction = () =>
            {
                Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * thCount * 2);
                while (Volatile.Read(ref addFinished) < thCount && !tokSrc.IsCancellationRequested)
                {
                    q.CompleteCurrentBatch();
                    Thread.Sleep(rnd.Next(2));
                }
            };


            for (int i = 0; i < threadsTake.Length; i++)
            {
                threadsTake[i] = new Thread(new ThreadStart(takeAction));
            }
            for (int i = 0; i < threadsAdd.Length; i++)
            {
                threadsAdd[i] = new Thread(new ThreadStart(addAction));
            }
            completeBatchThread = new Thread(new ThreadStart(completeBatchAction));

            for (int i = 0; i < threadsTake.Length; i++)
            {
                threadsTake[i].Start();
            }
            for (int i = 0; i < threadsAdd.Length; i++)
            {
                threadsAdd[i].Start();
            }
            completeBatchThread.Start();


            for (int i = 0; i < threadsAdd.Length; i++)
            {
                threadsAdd[i].Join();
            }
            tokSrc.Cancel();
            for (int i = 0; i < threadsTake.Length; i++)
            {
                threadsTake[i].Join();
            }
            completeBatchThread.Join();


            Assert.AreEqual(elemCount, global.Count);
            global.Sort();

            for (int i = 0; i < elemCount; i++)
            {
                Assert.AreEqual(i, global[i]);
            }
        }
        private void SimpleConcurrentTestCore(int batchSize, int boundedCapacityInBatches)
        {
            BlockingBatchingQueue <long> col = new BlockingBatchingQueue <long>(batchSize: batchSize, boundedCapacityInBatches: boundedCapacityInBatches);
            Barrier bar = new Barrier(4);
            CancellationTokenSource cancelToken = new CancellationTokenSource();

            List <long> takenItems = new List <long>();

            Task addTask = Task.Run(() =>
            {
                Random rnd = new Random();
                bar.SignalAndWait();
                long data = 0;
                while (!cancelToken.IsCancellationRequested)
                {
                    if (col.TryAdd(data))
                    {
                        data++;
                    }
                    if (rnd.Next(100) == 1)
                    {
                        col.CompleteCurrentBatch();
                    }
                }
            });

            Task takeTask = Task.Run(() =>
            {
                bar.SignalAndWait();

                while (!cancelToken.IsCancellationRequested)
                {
                    if (col.TryTake(out long[] itemsT))
                    {
                        takenItems.AddRange(itemsT);
                    }

                    if (takenItems.Count > int.MaxValue / 2)
                    {
                        cancelToken.Cancel();
                    }
                }
            });

            Task enumerateTask = Task.Run(() =>
            {
                bar.SignalAndWait();

                while (!cancelToken.IsCancellationRequested)
                {
                    int count     = 0;
                    long prevItem = -1;
                    foreach (long item in col)
                    {
                        count++;
                        if (prevItem > 0)
                        {
                            Assert.AreEqual(prevItem + 1, item);
                        }

                        prevItem = item;
                    }
                    Thread.Sleep(count > 100 ? 0 : 1);
                }
            });

            bar.SignalAndWait();
            Thread.Sleep(300);
            cancelToken.Cancel();

            Task.WaitAll(addTask, takeTask, enumerateTask);

            while (col.TryTake(out long[] itemsF))
            {
                takenItems.AddRange(itemsF);
            }

            col.CompleteCurrentBatch();

            if (col.TryTake(out long[] itemsFF))
            {
                takenItems.AddRange(itemsFF);
            }

            for (int i = 0; i < takenItems.Count; i++)
            {
                Assert.AreEqual(i, takenItems[i]);
            }
        }
        private void ConcurrentPackageWithTimeoutTestCore(int addThreads, int itemCount, int batchSize, int boundedCapacityInBatches)
        {
            int atomicRandom = 0;

            BlockingBatchingQueue <int> col = new BlockingBatchingQueue <int>(batchSize: batchSize, boundedCapacityInBatches: boundedCapacityInBatches);
            Barrier bar = new Barrier(1 + 1 + addThreads);
            CancellationTokenSource cancelToken = new CancellationTokenSource();

            Thread[] threadsAdd  = new Thread[addThreads];
            Thread[] threadsTake = new Thread[1];

            List <int> takenItems   = new List <int>();
            int        itemsCounter = itemCount;

            Action addAction = () =>
            {
                Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * addThreads * 2);
                bar.SignalAndWait();

                while (true)
                {
                    int val = Interlocked.Decrement(ref itemsCounter);
                    if (val < 0)
                    {
                        break;
                    }

                    col.Add(val);

                    int delay = (int)(((double)val / itemCount) * 100);
                    if (delay > 0)
                    {
                        SpinWaitHelper.SpinWait(rnd.Next(delay));
                    }
                }
            };

            Action takeAction = () =>
            {
                var   token = cancelToken.Token;
                int[] items = null;
                bar.SignalAndWait();


                try
                {
                    while (!cancelToken.IsCancellationRequested)
                    {
                        if (col.TryTake(out items, 5, token))
                        {
                            takenItems.AddRange(items);
                        }
                        else
                        {
                            col.CompleteCurrentBatch();
                        }
                    }
                }
                catch (OperationCanceledException) { }

                while (col.TryTake(out items))
                {
                    takenItems.AddRange(items);
                }

                col.CompleteCurrentBatch();

                if (col.TryTake(out items))
                {
                    takenItems.AddRange(items);
                }
            };

            for (int i = 0; i < threadsTake.Length; i++)
            {
                threadsTake[i] = new Thread(new ThreadStart(takeAction));
            }
            for (int i = 0; i < threadsAdd.Length; i++)
            {
                threadsAdd[i] = new Thread(new ThreadStart(addAction));
            }

            for (int i = 0; i < threadsTake.Length; i++)
            {
                threadsTake[i].Start();
            }
            for (int i = 0; i < threadsAdd.Length; i++)
            {
                threadsAdd[i].Start();
            }

            bar.SignalAndWait();

            for (int i = 0; i < threadsAdd.Length; i++)
            {
                threadsAdd[i].Join();
            }

            cancelToken.Cancel();

            for (int i = 0; i < threadsTake.Length; i++)
            {
                threadsTake[i].Join();
            }


            takenItems.Sort();
            for (int i = 0; i < takenItems.Count; i++)
            {
                Assert.AreEqual(i, takenItems[i]);
            }
        }
        private void TestCancellationNotCorruptDataCore(int batchSize, int boundedCapacityInBatches)
        {
            BlockingBatchingQueue <long> col = new BlockingBatchingQueue <long>(batchSize: batchSize, boundedCapacityInBatches: boundedCapacityInBatches);
            Barrier bar = new Barrier(4);
            CancellationTokenSource cancelToken              = new CancellationTokenSource();
            CancellationTokenSource temporaryCancelTokenAdd  = new CancellationTokenSource();
            CancellationTokenSource temporaryCancelTokenTake = new CancellationTokenSource();

            List <long> takenItems = new List <long>();

            Task addTask = Task.Run(() =>
            {
                Random rnd = new Random();
                bar.SignalAndWait();
                long data = 0;
                CancellationToken token = temporaryCancelTokenAdd.Token;
                while (!cancelToken.IsCancellationRequested)
                {
                    try
                    {
                        col.Add(data, token);
                        data++;
                    }
                    catch (OperationCanceledException)
                    {
                        token = temporaryCancelTokenAdd.Token;
                    }
                }
            });

            Task takeTask = Task.Run(() =>
            {
                bar.SignalAndWait();

                CancellationToken token = temporaryCancelTokenTake.Token;
                while (!cancelToken.IsCancellationRequested)
                {
                    try
                    {
                        long[] itemsT = col.Take(token);
                        takenItems.AddRange(itemsT);
                    }
                    catch (OperationCanceledException)
                    {
                        token = temporaryCancelTokenTake.Token;
                    }

                    if (takenItems.Count > int.MaxValue / 2)
                    {
                        cancelToken.Cancel();
                    }
                }
            });


            Task cancelTask = Task.Run(() =>
            {
                Random rnd = new Random();
                bar.SignalAndWait();

                while (!cancelToken.IsCancellationRequested)
                {
                    if (rnd.Next(100) == 1)
                    {
                        temporaryCancelTokenAdd.Cancel();
                        temporaryCancelTokenAdd = new CancellationTokenSource();
                    }
                    if (rnd.Next(100) == 1)
                    {
                        temporaryCancelTokenTake.Cancel();
                        temporaryCancelTokenTake = new CancellationTokenSource();
                    }

                    SpinWaitHelper.SpinWait(rnd.Next(12));
                }
            });

            bar.SignalAndWait();
            Thread.Sleep(500);
            cancelToken.Cancel();
            temporaryCancelTokenAdd.Cancel();
            temporaryCancelTokenTake.Cancel();

            Task.WaitAll(addTask, takeTask, cancelTask);

            while (col.TryTake(out long[] itemsF))
            {
                takenItems.AddRange(itemsF);
            }

            col.CompleteCurrentBatch();

            if (col.TryTake(out long[] itemsFF))
            {
                takenItems.AddRange(itemsFF);
            }

            for (int i = 0; i < takenItems.Count; i++)
            {
                Assert.AreEqual(i, takenItems[i]);
            }
        }