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); }
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()); }