Esempio n. 1
0
        public void When_waiting_for_consumption_to_complete()
        {
            var          exceptions = new ConcurrentBag <Exception>();
            var          numbers    = new ConcurrentQueue <int>();
            Action <int> work       = n =>
            {
                Thread.Sleep(50.Milliseconds());
                numbers.Enqueue(n);
            };

            using (var pcq = new ProducerConsumerQueue <int>(work, 1))
            {
                pcq.OnException += (sender, exception) => exceptions.Add(exception);

                pcq.Add(1);
                pcq.Add(2);
                pcq.Add(3);
                pcq.CompleteAdding();

                pcq.Completion.Result.ShouldBeTrue();

                numbers.ShouldBe(new[] { 1, 2, 3 });
                exceptions.ShouldBeEmpty();

                pcq.Capacity.ShouldBe(-1);
                pcq.PendingCount.ShouldBe((uint)0);
                pcq.PendingItems.ShouldBeEmpty();
            }
        }
Esempio n. 2
0
        public void ShutdownQueueAfterSomeAddsShouldResultInTheProcessOfAllAddedItemsBeforeDisposal()
        {
            var consumed = new ConcurrentBag <MyClass>();

            Action <MyClass> consumer = x => { consumed.Add(x); };

            var queue = new ProducerConsumerQueue <MyClass>(consumer, 1);
            ProducerConsumerQueueException thrownException = null;

            queue.OnException += (sender, args) =>
            {
                thrownException = args;
            };

            5.Times(n => { queue.Add(_getData(n)); });
            queue.CompleteAdding();

            queue.Completion.Wait(10.Seconds()).ShouldBeTrue();

            Action afterDisposedEnqueues = () => 3.Times(n => { queue.Add(_getData(n)); });

            afterDisposedEnqueues.ShouldNotThrow();

            Thread.Sleep(100);

            consumed.Count.ShouldBe(5);

            Thread.Sleep(1.Seconds());
            thrownException.ShouldNotBeNull();
            thrownException.Message.ShouldBe("Exception occurred when adding item.");
        }
Esempio n. 3
0
        public void DisposedQueueShouldCancelConsumersCorrectly()
        {
            Exception exceptionThrown = null;
            var       consumed        = new ConcurrentBag <int>();

            var queue = new ProducerConsumerQueue <int>(i =>
            {
                Thread.Sleep(50.Milliseconds());
                consumed.Add(i);
            }, 1);

            queue.OnException += (sender, args) =>
            {
                using (consumed.Lock(100.Milliseconds())) { exceptionThrown = args; }
            };

            queue.Add(1);
            queue.Add(2);
            queue.Add(3);
            queue.Add(4);
            queue.Add(5);
            queue.CompleteAdding();

            queue.Completion.Wait(500.Milliseconds()).ShouldBeTrue();

            consumed.Count.ShouldBeGreaterThanOrEqualTo(2);

            Thread.Sleep(1.Seconds());
            exceptionThrown.ShouldBeNull();
        }
Esempio n. 4
0
        public void DisposedQueueShouldNotAllowAddingNewItems()
        {
            var consumed = new ConcurrentBag <MyClass>();
            Action <MyClass> consumer = x => { consumed.Add(x); };

            var queue = new ProducerConsumerQueue <MyClass>(consumer, 1);

            ProducerConsumerQueueException thrownException = null;

            queue.OnException += (sender, args) =>
            {
                thrownException = args;
            };

            const int WorkItems = 10;

            for (var i = 0; i < 10; i++)
            {
                queue.Add(_getData(i));
            }
            queue.CompleteAdding();

            queue.Completion.Wait(5.Seconds()).ShouldBeTrue();

            Thread.Sleep(50.Milliseconds());
            consumed.Count.ShouldBe(WorkItems);

            Assert.DoesNotThrow(() => queue.Add(new MyClass()));

            Thread.Sleep(1.Seconds());
            thrownException.ShouldNotBeNull();
            thrownException.Message.ShouldBe("Exception occurred when adding item.");
        }
Esempio n. 5
0
        public static IDictionary <string, uint> GetTopWordsProducerConsumerEasier(FileInfo InputFile, char[] Separators, uint TopCount)
        {
            // Limitations
            const int WorkerCount     = 12;
            const int BoundedCapacity = 10000;
            var       result          = new ConcurrentDictionary <string, uint>(StringComparer.InvariantCultureIgnoreCase);

            // Declare the worker
            Action <string> work = line =>
            {
                // Loop through words in line, filter seperators
                foreach (var word in line.Split(Separators, StringSplitOptions.RemoveEmptyEntries))
                {
                    // Valid word
                    if (!TrackWordsClass.IsValidWord(word))
                    {
                        continue;
                    }
                    // Update word list
                    result.AddOrUpdate(word, 1, (key, oldVal) => oldVal + 1);
                }
            };

            // Setup the queue
            var pcq = new ProducerConsumerQueue <string>(work, WorkerCount, BoundedCapacity);

            pcq.OnException += (sender, ex) => Console.WriteLine("Oooops: " + ex.Message);

            // Begin producing
            foreach (var line in File.ReadLines(InputFile.FullName))
            {
                pcq.Add(line);
            }
            pcq.CompleteAdding();
            // End of producing

            // Wait for workers to finish their work
            pcq.Completion.Wait();
            // Return ordered dictionary
            return(result
                   .OrderByDescending(kv => kv.Value)
                   .Take((int)TopCount)
                   .ToDictionary(kv => kv.Key, kv => kv.Value));
        }
Esempio n. 6
0
        public void ConsumerWithOneWorkerShouldProcessEveryAddedItems()
        {
            Exception exceptionThrown = null;
            var       consumed        = new ConcurrentBag <MyClass>();

            Action <MyClass> consumer = x => { consumed.Add(x); };

            var queue = new ProducerConsumerQueue <MyClass>(consumer, 1);

            queue.OnException += (sender, args) =>
            {
                using (consumed.Lock(100.Milliseconds())) { exceptionThrown = args; }
            };

            10.Times(n => queue.Add(_getData(n)));
            queue.CompleteAdding();

            queue.Completion.Wait(100.Milliseconds()).ShouldBeTrue();

            consumed.Count.ShouldBe(10);
            exceptionThrown.ShouldBeNull();
        }