예제 #1
0
        public void Should_Enumerate_All_Enqueued_Items()
        {
            _queue.TryEnqueue(Value, _timeout);
            _queue.TryEnqueue(Value + 1, _timeout);

            using (var enumerator = _queue.GetConsumingEnumerable().GetEnumerator())
            {
                enumerator.MoveNext();
                enumerator.Current.Should().Be(Value);

                enumerator.MoveNext().Should().BeTrue();
                enumerator.Current.Should().Be(Value + 1);
            }
        }
예제 #2
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);
        }
예제 #3
0
 /// <summary>
 /// Gets a blocking enumerable that removes items from the queue. This enumerable only completes after <see cref="CompleteAdding"/> has been called.
 /// </summary>
 /// <returns>A blocking enumerable that removes items from the queue.</returns>
 public IEnumerable <Tuple <Task, bool> > GetConsumingEnumerable()
 {
     return(_queue.GetConsumingEnumerable());
 }