Пример #1
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);
        }
Пример #2
0
        async void TestBoundedBlockingQueueClearDispose()
        {
            BoundedBlockingQueue <TestQueueItem> bbq = new BoundedBlockingQueue <TestQueueItem>(3);

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

            Assert.Equal(2, bbq.Count);

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

            Task.Run(async() =>
            {
                await Task.Delay(1000); //have to wait as new item will be enqueued immediately after clearing
                bbq.Enqueue(new TestQueueItem {
                    Value = 400
                });
            });

            bbq.Clear();

            Assert.Equal(0, bbq.Count);

            var result = bbq.TryDequeue(out var qItem);

            Assert.False(result);
            Assert.Null(qItem);

            await Task.Delay(1500);

            Assert.Equal(1, bbq.Count);
            var item400 = bbq.Dequeue();

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

            bool threwObjectDisposedEx = false;

            Task.Run(() =>
            {
                try
                {
                    bbq.Dequeue();
                }
                catch (ObjectDisposedException ex)
                {
                    threwObjectDisposedEx = true;
                }
            });

            await Task.Delay(1500);

            bbq.Dispose();

            await Task.Delay(1500);

            Assert.True(threwObjectDisposedEx);

            Assert.True(bbq.IsDisposed);

            Assert.Throws <ObjectDisposedException>(() => bbq.Count);

            Assert.Throws <ObjectDisposedException>(() => bbq.Size);

            Assert.Throws <ObjectDisposedException>(() => bbq.QueueSyncRoot);

            Assert.Throws <ObjectDisposedException>(() => bbq.Values);

            Assert.Throws <ObjectDisposedException>(() => bbq.IsReadOnly);

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

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

            Assert.Throws <ObjectDisposedException>(() => bbq.Peek());

            var resTryPeek = bbq.TryPeek(out var itemPeek);

            Assert.False(resTryPeek);
            Assert.Null(itemPeek);

            Assert.Throws <ObjectDisposedException>(() => bbq.Dequeue());

            var resTryDeq = bbq.TryDequeue(out var itemDequeue);

            Assert.False(resTryDeq);
            Assert.Null(itemDequeue);

            Assert.Throws <ObjectDisposedException>(() => bbq.Contains(new TestQueueItem {
                Value = 1000
            }));
            Assert.Throws <ObjectDisposedException>(() => bbq.CopyTo(new TestQueueItem[3], 0));

            Assert.Throws <ObjectDisposedException>(() => bbq.GetEnumerator());
            Assert.Throws <ObjectDisposedException>(() => (bbq as IEnumerable).GetEnumerator());
        }