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 SuccessfulPutAndTake()
        {
            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(() => transferQueue.Put(message));
            }

            int[] msgs = new int[2];
            int   idx  = default;

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

            threads[0].Start(); //put message
            Thread.Sleep(1);
            threads[2].Start(); // take message
            Thread.Sleep(1);
            threads[3].Start(); // take message - blocking
            Thread.Sleep(1);
            threads[1].Start(); // put message
            Thread.Sleep(1);

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

            for (int i = 2; i < numberOfThreads; i++)
            {
                Assert.AreEqual(message, msgs[i - 2]);
            }
        }
Ejemplo n.º 3
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.º 4
0
        public void ShouldTimeoutAndReturnNullForTakeAndFalseForTransfer()
        {
            TransferQueue <int?> transferQueue = new TransferQueue <int?>();
            int numberOfThreads = 3;
            int timeout         = 10;
            int message         = 35;

            Thread[] threads = new Thread[numberOfThreads];

            threads[0] = new Thread(() => {
                try {
                    Thread.Sleep(100);
                    transferQueue.Put(message);
                }
                catch (ThreadInterruptedException e) {
                    Console.WriteLine(e.StackTrace);
                }
            });

            int?takeIntegerResult = default;

            threads[1] = new Thread(() => {
                try {
                    takeIntegerResult = transferQueue.Take(timeout);
                }
                catch (ThreadInterruptedException e) {
                    Console.WriteLine("ERROR : take exception");
                    Console.WriteLine(e.StackTrace);
                }
            });

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

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

            Assert.IsNull(takeIntegerResult);
        }
Ejemplo n.º 5
0
        public void TranferQueuePutAndTakeTest()
        {
            TransferQueue <int> tranferQueue = new TransferQueue <int>();
            int    msg      = 0;
            bool   received = false;
            Thread t1       = new Thread(() =>
            {
                tranferQueue.Put(22);
            });
            Thread t2 = new Thread(() =>
            {
                received = tranferQueue.Take(Timeout.Infinite, out msg);
            });

            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            Assert.IsTrue(received);
            Assert.IsTrue(msg == 22);
        }
Ejemplo n.º 6
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);
        }