Example #1
0
        public void TestBlockingQueueStress()
        {
            var queue = new BlockingQueue<int>(1);

            var rnd = new ThreadLocal<Random>(() => new Random());
            var generatedTotal = 0;
            var consummedTotal = 0;

            var producers = TestMonitorSimple.RunSimultanously(5, () =>
            {
                for (var i = 0; i < 1e6; i++)
                {
                    var value = rnd.Value.Next(100);
                    Interlocked.Add(ref generatedTotal, value);
                    queue.AddIfNotCompleted(value);
                }
            }, false);

            var consumers = TestMonitorSimple.RunSimultanously(5, () =>
            {
                foreach (var value in queue.GetConsumingEnumerable())
                {
                    Interlocked.Add(ref consummedTotal, value);
                }
            }, false);

            producers.ForEach(t => t.Join());
            queue.CompleteAdding();

            consumers.ForEach(t => t.Join());

            Assert.IsTrue(consummedTotal == generatedTotal);
        }
        public void Should_Add_Complete()
        {
            _queue.CompleteAdding();

            _queue.TryDequeue(out _, Timeout.InfiniteTimeSpan).Should().BeFalse();
        }
 /// <summary>
 /// Marks the queue as complete for adding, allowing the enumerator returned from <see cref="GetConsumingEnumerable"/> to eventually complete. This method may be called several times.
 /// </summary>
 public void CompleteAdding()
 {
     _queue.CompleteAdding();
 }