public void TestDisposeInterruptWaitersOnAdd()
        {
            BlockingBatchingQueue <int> queue = new BlockingBatchingQueue <int>(batchSize: 2, boundedCapacityInBatches: 1);

            queue.Add(1);
            queue.Add(2);
            Barrier bar = new Barrier(2);

            Task disposeTask = Task.Run(() =>
            {
                bar.SignalAndWait();
                Thread.Sleep(10);
                queue.Dispose();
            });

            try
            {
                bar.SignalAndWait();
                bool added = queue.TryAdd(3, 10000);
                Assert.Fail();
            }
            catch (OperationInterruptedException)
            {
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception)
            {
                Assert.Fail("Unexpected exception");
            }


            disposeTask.Wait();
        }
        public void TestQueueEnumeration()
        {
            const int batchSize             = 17;
            BlockingBatchingQueue <int> col = new BlockingBatchingQueue <int>(batchSize: batchSize);

            for (int i = 0; i < 113; i++)
            {
                col.Add(i);

                int j = 0;
                foreach (var item in col)
                {
                    Assert.AreEqual(j, item);
                    j++;
                }
                Assert.AreEqual(j - 1, i);
            }

            int offset       = 0;
            int initialCount = col.Count;

            while (col.TryTake(out int[] items))
            {
                offset += items.Length;

                int j = offset;
                foreach (var item in col)
                {
                    Assert.AreEqual(j, item);
                    j++;
                }
                Assert.AreEqual(j - 1, initialCount - 1);
            }
        }
        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);
        }
        public void TestSimpleEnqueueDequeue()
        {
            const int batchSize             = 20;
            BlockingBatchingQueue <int> col = new BlockingBatchingQueue <int>(batchSize: batchSize);

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, col.Count);
                Assert.AreEqual(i / batchSize, col.CompletedBatchCount);
                col.Add(i);
            }

            Assert.AreEqual(100, col.Count);
            int expectedCount = 100;

            while (true)
            {
                bool dequeueRes = col.TryTake(out int[] batch);
                if (!dequeueRes)
                {
                    break;
                }

                Assert.IsNotNull(batch);
                Assert.AreEqual(batchSize, batch.Length);
                for (int i = 0; i < batch.Length; i++)
                {
                    Assert.AreEqual(i + (100 - expectedCount), batch[i]);
                }

                expectedCount -= batch.Length;
                Assert.AreEqual(expectedCount, col.Count);
                Assert.AreEqual(expectedCount / batchSize, col.CompletedBatchCount);
            }

            Assert.AreEqual(0, col.Count);
        }
        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 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]);
            }
        }