Ejemplo n.º 1
0
        public void TransferQueueOneTakeTimeout()
        {
            TransferQueue <int> tranferQueue = new TransferQueue <int>();
            int    msg1 = 0, msg2 = 0;
            bool   transfered = false, taken1 = false, taken2 = true;
            Thread t1 = new Thread(() =>
            {
                transfered = tranferQueue.Transfer(22, 50);
            });
            Thread t2 = new Thread(() =>
            {
                taken1 = tranferQueue.Take(50, out msg1);
            });
            Thread t3 = new Thread(() =>
            {
                taken2 = tranferQueue.Take(50, out msg2);
            });

            t1.Start();
            t2.Start();
            Thread.Sleep(10);
            t3.Start();
            t1.Join();
            t2.Join();
            t3.Join();
            Assert.IsTrue(taken1);
            Assert.IsFalse(taken2);
            Assert.IsTrue(transfered);
            Assert.AreEqual(22, msg1);
            Assert.AreEqual(default(int), msg2);
        }
Ejemplo n.º 2
0
        public void SuccessfulTransferAndTake()
        {
            TransferQueue <int> transferQueue = new TransferQueue <int>();
            int numberOfThreads = 4;
            int timeout         = 50;
            int message         = 35;

            Thread[] threads = new Thread[numberOfThreads];

            for (int i = 0; i < numberOfThreads / 2; i++)
            {
                threads[i] = new Thread(() => {
                    try {
                        transferQueue.Transfer(message, timeout);
                    }
                    catch (ThreadInterruptedException e) {
                        Console.WriteLine("ERROR : transfer exception");
                        Console.WriteLine(e.StackTrace);
                    }
                });
            }

            int msg = default;

            for (int i = 2; i < numberOfThreads; i++)
            {
                threads[i] = new Thread(() => {
                    try {
                        Thread.Sleep(10);
                        msg = transferQueue.Take(timeout);
                    }
                    catch (ThreadInterruptedException e) {
                        Console.WriteLine("ERROR : take exception");
                        Console.WriteLine(e.StackTrace);
                    }
                });
            }

            for (int i = 0; i < numberOfThreads; i++)
            {
                threads[i].Start();
            }

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

            Assert.AreEqual(message, msg);
        }
Ejemplo n.º 3
0
        public void TransferQueueTranferAndTakeTest()
        {
            TransferQueue <int> tranferQueue = new TransferQueue <int>();
            int    msg = 0;
            bool   transfered = false, taken = false;
            Thread t1 = new Thread(() =>
            {
                transfered = tranferQueue.Transfer(22, Timeout.Infinite);
            });
            Thread t2 = new Thread(() =>
            {
                taken = tranferQueue.Take(Timeout.Infinite, out msg);
            });

            t1.Start();
            Thread.Sleep(10);
            t2.Start();
            t2.Join();
            t1.Join();
            Assert.IsTrue(taken);
            Assert.IsTrue(transfered);
            Assert.AreEqual(msg, 22);
        }