Example #1
0
        public void ProcessingQueueOperationsInProcessDoesNotExceedMaxOperationsMultiThreaded()
        {
            const int expected = 20;

            bool maxOperationsExceeded = false;
            int  operationsProcessed   = 0;

            ManualResetEventSlim pause = new ManualResetEventSlim(false);

            var    queue  = new ThrottledProcessingQueue(10);
            Random random = new Random();

            Parallel.For(0, 20, i => queue.RegisterOperation(new Task(() =>
            {
                if (queue.MaxOperations == 20)
                {
                    maxOperationsExceeded = true;
                }

                //simulate operation running
                Thread.Sleep(TimeSpan.FromMilliseconds(random.Next(0, 200)));

                Interlocked.Increment(ref operationsProcessed);
                if (operationsProcessed == 20)
                {
                    pause.Set();
                }
                queue.UpdateProcessingQueue();
            })));

            pause.Wait(TimeSpan.FromSeconds(10));

            Assert.IsFalse(maxOperationsExceeded);
            Assert.AreEqual(expected, operationsProcessed);
        }
Example #2
0
        public void ProcessingQueueEnqueuesOperationIfProcessQueueIsFullAndCurrentlyRunningTaskFails()
        {
            bool operation1Processed = false;
            bool operation2Processed = false;

            var queue = new ThrottledProcessingQueue(1);
            ManualResetEventSlim pause = new ManualResetEventSlim(false);

            Task operation1 = new Task(() =>
            {
                //simulate a long running operation
                Thread.Sleep(TimeSpan.FromMilliseconds(100));
                operation1Processed = true;
                // throw exception to simulate failed operation
                throw new InvalidOperationException();
            });
            Task operation2 = new Task(() =>
            {
                operation2Processed = true;
                pause.Set();
            });

            queue.RegisterOperation(operation1);
            queue.RegisterOperation(operation2);
            operation1.ContinueWith(t => queue.UpdateProcessingQueue());

            pause.Wait(TimeSpan.FromSeconds(10));

            Assert.IsTrue(operation1Processed);
            Assert.IsTrue(operation2Processed);
        }
Example #3
0
        public void ProcessingQueueEnqueuesOperationsIfProcessQueueIsFullMultiThreadedAndCurrentlyRunningTaskFails()
        {
            bool operation1Processed = false;
            bool operation2Processed = false;

            var queue = new ThrottledProcessingQueue(10);
            ManualResetEventSlim pause = new ManualResetEventSlim(false);

            Parallel.For(0, 10, i => queue.RegisterOperation(new Task(() =>
            {
                //simulate long running operation
                Thread.Sleep(TimeSpan.FromMilliseconds(100));
                operation1Processed = true;

                queue.UpdateProcessingQueue();

                // throw exception to simulate failed operation
                throw new InvalidOperationException();
            })));
            queue.RegisterOperation(new Task(() =>
            {
                operation2Processed = true;
                pause.Set();
            }));

            pause.Wait(TimeSpan.FromSeconds(10));

            Assert.IsTrue(operation1Processed);
            Assert.IsTrue(operation2Processed);
        }