Esempio n. 1
0
 private void SwitchToLive()
 {
     while (_liveBuffer.Count > 0)
     {
         _buffer.Enqueue(_liveBuffer.Dequeue());
     }
     Live = true;
 }
Esempio n. 2
0
        public void TwoInTwoOut()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            queue.Enqueue(1);
            queue.Enqueue(2);
            Assert.AreEqual(queue.Dequeue(), 1);
            Assert.AreEqual(queue.Dequeue(), 2);
        }
Esempio n. 3
0
        public void IfEnqueueIsEqualDequeueMultiple()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Dequeue();

            Assert.That(queue.Dequeue(), Is.EqualTo(3), "Some useful error message");
        }
Esempio n. 4
0
        public async Task TryEnqueueShouldNQ()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(1);
            var ok = queue.TryEnqueue(3, 500);

            Assert.That(ok, Is.True);
            Assert.That(queue.Dequeue(), Is.EqualTo(1));
            Assert.That(queue.Dequeue(), Is.EqualTo(3));
        }
Esempio n. 5
0
        public void OneProdOneCons()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            ParallelTasks ptasks = new ParallelTasks();

            // PRODUCER thread inserts 4 elements
            ptasks.Add("Producer", () =>
            {
                for (int i = 0; i < 4; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // CONSUMER thread removes 4 elements
            ptasks.Add("Consumer", () =>
            {
                for (int i = 0; i < 4; i++)
                {
                    Assert.AreEqual(i, queue.Dequeue());
                }
            });

            ptasks.Execute();

            // check size: should be zero
            Assert.AreEqual(queue.Size(), 0);
        }
Esempio n. 6
0
        public void BoundedQueueTest()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            ParallelTasks ptasks = new ParallelTasks();

            // PRODUCER thread 1 inserts 3 elements
            ptasks.Add("Producer1", () => {
                for (int i = 0; i < 3; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // PRODUCER thread 2 inserts 3 elements
            ptasks.Add("Producer2", () => {
                for (int i = 0; i < 3; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // CONSUMER thread removes 4 elements
            ptasks.Add("Consumer", () => {
                for (int i = 0; i < 4; i++)
                {
                    System.Diagnostics.Debug.Assert(i >= queue.Dequeue());
                }
            });

            ptasks.Execute();

            // check size: should be two
            Assert.AreEqual(2, queue.Size(), "The final size of the queue.");
        }
Esempio n. 7
0
        //[TestProperty("ChessExpectedResult", "Deadlock")]
        public void NoProducer()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(10);

            while (true)
            {
                queue.Dequeue();
            }
        }
Esempio n. 8
0
        public void TestBoundedQueueAbsorbWithMaxsizeOne()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(1, true);

            queue.Enqueue(0);
            queue.Enqueue(10);
            Assert.True(queue.Count == 1);
            Assert.True(queue.Dequeue() == 10);
            Assert.True(queue.Count == 0);
        }
Esempio n. 9
0
        /// <summary>
        /// Consumes the work in the task queue.
        /// </summary>
        private void Consume()
        {
            while (run)
            {
                Action task;

                if (_taskQueue.Dequeue(out task) && run)
                {
                    task();
                }
                else
                {
                    Thread.Sleep(0);
                }
            }
        }
Esempio n. 10
0
        public void TestBoundedQueueNoAbsorb()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(10);

            for (int i = 0; i < 10; i++)
            {
                Assert.False(queue.IsFull);
                queue.Enqueue(i);
            }
            Assert.Equal(queue.Count, 10);
            Assert.True(queue.Contains(5));
            Assert.True(queue.IsFull);
            Assert.Equal(queue.Peek(), 0);
            Assert.Equal(queue.Dequeue(), 0);
            Assert.Equal(queue.Count, 9);
            Assert.False(queue.IsFull);
        }
Esempio n. 11
0
        public async Task EnqueueAndDequeueMultiThreaded()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(2);
            queue.Enqueue(3);
            var   t       = Task.Run(() => queue.Enqueue(4));
            var   timeout = Task.WhenAny(t, Task.Delay(100));
            await timeout;

            Assert.That(t.IsCompleted, Is.False, "Some useful error message");

            queue.Dequeue();
            await t;

            Assert.That(t.IsCompleted, Is.True, "Some useful error message");
            Assert.That(queue.Count, Is.EqualTo(2), "Some useful error message");
        }
Esempio n. 12
0
        public async Task BigTest()
        {
            var list1 = new List <int> {
                1, 2, 3, 4, 5
            };
            var list2 = new List <int> {
                6, 7, 9, 10, 8
            };
            var expected = new List <int>(list1.Union(list2));

            expected.Sort();
            var queue = new BoundedQueue <int>(2);

            var results = new ConcurrentBag <int>();

            var t1 = new Task(() =>
            {
                for (int i = 0; i < 5; i++)
                {
                    queue.Enqueue(list1[i]);
                }
            });

            var t2 = new Task(() =>
            {
                for (int i = 0; i < 5; i++)
                {
                    queue.Enqueue(list2[i]);
                }
            });

            var t3 = new Task(() =>
            {
                while (results.Count < 10)
                {
                    results.Add(queue.Dequeue());
                }
            });

            var t4 = new Task(() =>
            {
                while (results.Count < 10)
                {
                    results.Add(queue.Dequeue());
                }
            });

            t1.Start();
            t2.Start();

            t3.Start();
            t4.Start();

            var t5 = Task.Delay(5000);
            await Task.WhenAny(t3, t4, t5);

            Assert.That(t5.IsCompleted, Is.EqualTo(false), "TimeOut");
            Assert.That(results.Count, Is.EqualTo(10), "Count is not 10");

            var sortedList = results.ToList();

            sortedList.Sort();

            Assert.That(expected.SequenceEqual(sortedList), "Sequence not equal");
        }