Ejemplo n.º 1
0
        void TestBoundedBlockingQueueCopy()
        {
            BoundedBlockingQueue <TestQueueItem> bbq = new BoundedBlockingQueue <TestQueueItem>(3);

            bbq.Enqueue(new TestQueueItem {
                Value = 100
            });
            bbq.Enqueue(new TestQueueItem {
                Value = 200
            });
            bbq.Enqueue(new TestQueueItem {
                Value = 300
            });

            var values = bbq.Values;

            TestQueueItem[] copyArray = new TestQueueItem[bbq.Size];
            bbq.CopyTo(copyArray, 0);

            foreach (var item in copyArray)
            {
                Assert.Contains(item, bbq);
                Assert.Contains(item, values);
            }
        }
Ejemplo n.º 2
0
 private void Verify(TestQueueItem item)
 {
     Assert.NotNull(item);
     _id = _counter / _consumersPerProducer;
     Assert.Equal(_id, item.Id);
     Assert.Equal($"Item{_id}", item.Name);
     ++_counter;
 }
 public static bool IsLessThan(TestQueueItem a, TestQueueItem b)
 {
     return(a.Value < b.Value);
 }
Ejemplo n.º 4
0
        async void TestBoundedBlockingQueueEnqueueDequeue()
        {
            BoundedBlockingQueue <TestQueueItem> bbq = new BoundedBlockingQueue <TestQueueItem>(2);

            Assert.Equal(0, bbq.Count);
            Assert.Equal(2, bbq.Size);

            Assert.Throws <ArgumentNullException>(() =>
            {
                bbq.Enqueue(null);
            });

            Assert.Equal(0, bbq.Count);

            Assert.False(bbq.TryEnqueue(null));

            Assert.Equal(0, bbq.Count);

            bbq.Enqueue(new TestQueueItem {
                Value = 100
            });
            bbq.Enqueue(new TestQueueItem {
                Value = 200
            });

            Assert.Equal(2, bbq.Count);

            Assert.Throws <TimeoutException>(() => bbq.Enqueue(new TestQueueItem {
                Value = 300
            }, 1000));

            Assert.Equal(2, bbq.Count);

            Assert.False(bbq.TryEnqueue(new TestQueueItem {
                Value = 300
            }, 1000));

            Assert.Equal(2, bbq.Count);

            var item100 = bbq.Dequeue();

            Assert.Equal(2, bbq.Size);
            Assert.Equal(1, bbq.Count);
            Assert.Equal(100, item100.Value);

            bbq.Enqueue(new TestQueueItem {
                Value = 500
            }, 1000);
            Assert.Equal(2, bbq.Count);

            var item200 = bbq.Dequeue();

            Assert.Equal(200, item200.Value);
            Assert.Equal(1, bbq.Count);

            bbq.Enqueue(new TestQueueItem {
                Value = 400
            });

            Task.Run(() =>
            {
                bbq.Enqueue(new TestQueueItem {
                    Value = 600
                });
            });

            await Task.Delay(1000); //allow enough time for task to set up

            var item500 = bbq.Dequeue();

            await Task.Delay(1000); //allow enough time for task to enqueue 600

            Assert.Equal(500, item500.Value);
            Assert.Equal(2, bbq.Count); //400 and 600 should be in here

            var item400 = bbq.Dequeue();

            Assert.Equal(1, bbq.Count);
            Assert.Equal(400, item400.Value);

            bool tryDeqRes = bbq.TryDequeue(out var item600);

            Assert.True(tryDeqRes);
            Assert.Equal(0, bbq.Count);
            Assert.Equal(600, item600.Value);

            TestQueueItem itemNull = null;

            Assert.Throws <TimeoutException>(() => itemNull = bbq.Dequeue(500));
            Assert.Null(itemNull);

            TestQueueItem item700 = null;

            Task.Run(() =>
            {
                item700 = bbq.Dequeue();
            });

            await Task.Delay(1000); //allow enough time for task to set up

            bbq.Enqueue(new TestQueueItem {
                Value = 700
            });

            await Task.Delay(1000); //allow enough time for task to dequeue 700

            Assert.Equal(700, item700.Value);
            Assert.Equal(0, bbq.Count);
        }
Ejemplo n.º 5
0
        async void TestBoundedBlockingQueuePeek()
        {
            BoundedBlockingQueue <TestQueueItem> bbq = new BoundedBlockingQueue <TestQueueItem>(1);

            Assert.Equal(0, bbq.Count);

            bool tryPeekEmpty = bbq.TryPeek(out var testQueueItem);

            Assert.False(tryPeekEmpty);
            Assert.Null(testQueueItem);

            TestQueueItem emptyItem = null;

            Assert.Throws <InvalidOperationException>(() => emptyItem = bbq.Peek());

            Assert.Null(emptyItem);

            Assert.Throws <TimeoutException>(() => emptyItem = bbq.Peek(500, true));

            Assert.Null(emptyItem);
            Assert.Equal(0, bbq.Count);

            bbq.Enqueue(new TestQueueItem {
                Value = 100
            });

            Assert.Equal(1, bbq.Count);

            var item100Peek = bbq.Peek();

            Assert.Equal(1, bbq.Count);
            Assert.Equal(100, item100Peek.Value);

            var item100 = bbq.Dequeue();

            Assert.Equal(0, bbq.Count);
            Assert.Equal(100, item100.Value);

            bool          exceptionThrown = false;
            TestQueueItem item200         = null;

            Task.Run(() =>
            {
                try
                {
                    item200 = bbq.Peek(5000, true);
                }
                catch
                {
                    exceptionThrown = true;
                }
            });

            bbq.Enqueue(new TestQueueItem {
                Value = 200
            });

            await Task.Delay(2000);

            Assert.Equal(1, bbq.Count);
            Assert.Equal(200, item200.Value);

            Assert.False(exceptionThrown);

            bool tryPeekRes = bbq.TryPeek(out var item200TryPeek);

            Assert.Equal(1, bbq.Count);
            Assert.True(tryPeekRes);
            Assert.Equal(200, item200.Value);
        }
Ejemplo n.º 6
0
        async void TestBoundedBlockingQueueClearMultipleWaitingThreads()
        {
            BoundedBlockingQueue <TestQueueItem> bbq = new BoundedBlockingQueue <TestQueueItem>(3);

            var item100 = new TestQueueItem {
                Value = 100
            };
            var item200 = new TestQueueItem {
                Value = 200
            };
            var item300 = new TestQueueItem {
                Value = 300
            };
            var item400 = new TestQueueItem {
                Value = 400
            };
            var item500 = new TestQueueItem {
                Value = 500
            };
            var item600 = new TestQueueItem {
                Value = 600
            };

            bbq.Enqueue(item100);
            bbq.Enqueue(item200);
            bbq.Enqueue(item300);

            Assert.Equal(3, bbq.Count);

            var values = bbq.Values;

            Assert.Equal(3, values.Length);
            Assert.Contains(item100, values);
            Assert.Contains(item200, values);
            Assert.Contains(item300, values);
            Assert.Contains(item100, bbq);
            Assert.Contains(item200, bbq);
            Assert.Contains(item300, bbq);

            bool exceptionThrown = false;

            Task.Run(() =>
            {
                try
                {
                    bbq.Enqueue(item400);
                }
                catch
                {
                    exceptionThrown = true;
                }
            });

            Task.Run(() =>
            {
                try
                {
                    bbq.Enqueue(item500);
                }
                catch
                {
                    exceptionThrown = true;
                }
            });

            Task.Run(() =>
            {
                try
                {
                    bbq.Enqueue(item600);
                }
                catch
                {
                    exceptionThrown = true;
                }
            });

            await Task.Delay(1000);

            Assert.False(exceptionThrown);

            bbq.Clear();

            await Task.Delay(1000);

            values = bbq.Values;

            Assert.Equal(3, values.Length);
            Assert.Equal(3, bbq.Count);
            Assert.Contains(item400, values);
            Assert.Contains(item500, values);
            Assert.Contains(item600, values);
            Assert.Contains(item400, bbq);
            Assert.Contains(item500, bbq);
            Assert.Contains(item500, bbq);
        }