public void CheckPutBlockingOnAFullBoundedQueue()
        {
            var  bq          = new BoundedQueue <int>(1);
            bool interrupted = false;

            Thread t = new Thread(() =>
            {
                try {
                    bq.Put(1);
                    // will block
                    bq.Put(2);
                }
                catch (ThreadInterruptedException)
                {
                    interrupted = true;
                }
            });

            t.Start();
            // Give a security margin to thread t1 execution.
            // Not really rocket science, but...
            Thread.Sleep(3000);

            t.Interrupt();
            t.Join();
            // success if the previous interruption
            // removes thread from blocking in second Put operation
            Assert.IsTrue(interrupted);
        }
        public void TestTransfer1To10ViaBoundedQueue()
        {
            int[] sendingNumbers  = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[] receivedNumbers = new int[10];
            using (var bb = new BoundedQueue <int>(1))
            {
                Thread producer = new Thread(() =>
                {
                    foreach (int i in sendingNumbers)
                    {
                        bb.Put(i);
                    }
                });

                Thread consumer = new Thread(() =>
                {
                    for (int i = 0; i < 10; ++i)
                    {
                        int val            = bb.Get();
                        receivedNumbers[i] = val;
                    }
                });

                consumer.Start();
                producer.Start();
                producer.Join();
                consumer.Join();
                Assert.AreEqual(sendingNumbers, receivedNumbers);
            }
        }
        public void OneProducerOneConsumerBlockedOnGet()
        {
            const int CAPACITY          = 100;
            const int MAX_VAL           = 10000;
            const int CONCLUDED_TIMEOUT = 5000;

            ManualResetEvent   concluded = new ManualResetEvent(false);
            BoundedQueue <int> queue     = new BoundedQueue <int>(CAPACITY);
            bool result = true;

            ThreadStart producer = () => {
                for (int i = 1; i <= MAX_VAL; ++i)
                {
                    queue.Put(i);
                }
            };

            ThreadStart consumer = () => {
                int pred = 0;
                try {
                    // read one more than is produced
                    for (int i = 1; i <= MAX_VAL + 1; ++i)
                    {
                        int val = queue.Get();
                        if (val != pred + 1)
                        {
                            result = false;
                            return;
                        }
                        pred = val;
                    }
                }
                catch (Exception) {
                    result = false;
                }
                finally {
                    concluded.Set();
                }
            };

            Thread tprod = new Thread(producer);

            tprod.Start();

            Thread tcons = new Thread(consumer);

            tcons.Start();

            bool waitRes = concluded.WaitOne(CONCLUDED_TIMEOUT);

            concluded.Close();

            Assert.AreEqual(true, result, "Error on Get!");
            Assert.AreEqual(false, waitRes, "Timeout must occurr!");
        }