public void DequeueShouldReturnQueuedWorkItemWhenQueueIsNotEmpty()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000);

            IWorkItem workItemFirst = Substitute.For <IWorkItem>();
            IWorkItem workItemLast  = Substitute.For <IWorkItem>();

            workItemQueue.Enqueue(workItemFirst);
            workItemQueue.Enqueue(workItemLast);

            Assert.AreEqual(workItemFirst, workItemQueue.Dequeue());
            Assert.AreEqual(workItemLast, workItemQueue.Dequeue());
        }
        public void ShutDownShouldNotBlockDequeueWhenThereAreWorkItemsInTheQueue()
        {
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            const int        workItemWaiterTimeOutInMilliSeconds = 1000;
            WorkItemQueue    workItemQueue = new WorkItemQueue(workItemWaiterTimeOutInMilliSeconds);

            IWorkItem workItem = Substitute.For <IWorkItem>();

            workItemQueue.Enqueue(workItem);

            new Thread(
                () =>
            {
                manualResetEvent.Set();
                manualResetEvent.Reset();
                manualResetEvent.WaitOne(300);

                IWorkItem dequeuedWorkItem = workItemQueue.Dequeue();

                Assert.AreEqual(workItem, dequeuedWorkItem);
            }).Start();

            manualResetEvent.WaitOne(300);

            workItemQueue.ShutDown();

            manualResetEvent.Set();
        }
        public void ShutDownShouldNotWaitMoreThanWaiterTimeOutWhenThereIsNoWorkItemsInTheQueue()
        {
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            const int      workItemWaiterTimeOutInMilliSeconds = 1000;
            WorkItemQueue  workItemQueue = new WorkItemQueue(workItemWaiterTimeOutInMilliSeconds);

            new Thread(
                () =>
            {
                Thread.Sleep(100);

                autoResetEvent.Set();

                IWorkItem workItem = workItemQueue.Dequeue();

                autoResetEvent.Set();

                Assert.AreEqual(null, workItem);
            }).Start();

            autoResetEvent.WaitOne(500);

            Stopwatch stopwatch = Stopwatch.StartNew();

            workItemQueue.ShutDown();
            autoResetEvent.WaitOne(workItemWaiterTimeOutInMilliSeconds + 100);
            stopwatch.Stop();

            Assert.Less(stopwatch.ElapsedMilliseconds, workItemWaiterTimeOutInMilliSeconds);
        }
        public void DequeueShouldReturnQueuedWorkItemWhenTheWaiterIsNotTimedOut()
        {
            IWorkItem workItem = Substitute.For <IWorkItem>();

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000);

            new Thread(
                () =>
            {
                Thread.Sleep(200);

                workItemQueue.Enqueue(workItem);
            }).Start();


            Assert.AreEqual(workItem, workItemQueue.Dequeue());
            Assert.AreEqual(0, workItemQueue.Count);
            Assert.AreEqual(0, workItemQueue.WorkItemWaiterEntryStack.Count);
        }
        public void DequeueShouldReturnNullWhenTheWaiterIsTimedOut()
        {
            IWorkItem workItem = Substitute.For <IWorkItem>();

            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            WorkItemQueue  workItemQueue  = new WorkItemQueue(1 * 300);

            new Thread(
                () =>
            {
                Thread.Sleep(1000);

                workItemQueue.Enqueue(workItem);
                autoResetEvent.Set();
            }).Start();

            Assert.AreEqual(null, workItemQueue.Dequeue());

            autoResetEvent.WaitOne();

            Assert.AreEqual(1, workItemQueue.Count);
            Assert.AreEqual(0, workItemQueue.WorkItemWaiterEntryStack.Count);
        }
Ejemplo n.º 6
0
 public WorkItem DequeueWorkItem()
 {
     lock (_syncObj)
         return(_workItemQueue.Count == 0 ? null : _workItemQueue.Dequeue());
 }
        public void ShutDownShouldNotBlockDequeueWhenThereAreWorkItemsInTheQueue()
        {
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            const int workItemWaiterTimeOutInMilliSeconds = 1000;
            WorkItemQueue workItemQueue = new WorkItemQueue(workItemWaiterTimeOutInMilliSeconds);

            IWorkItem workItem = Substitute.For<IWorkItem>();
            workItemQueue.Enqueue(workItem);
            
            new Thread(
                () =>
                {
                    manualResetEvent.Set();
                    manualResetEvent.Reset();
                    manualResetEvent.WaitOne(300);

                    IWorkItem dequeuedWorkItem = workItemQueue.Dequeue();

                    Assert.AreEqual(workItem, dequeuedWorkItem);
                }).Start();

            manualResetEvent.WaitOne(300);

            workItemQueue.ShutDown();

            manualResetEvent.Set();
        }
        public void ShutDownShouldNotWaitMoreThanWaiterTimeOutWhenThereIsNoWorkItemsInTheQueue()
        {
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            const int workItemWaiterTimeOutInMilliSeconds = 1000;
            WorkItemQueue workItemQueue = new WorkItemQueue(workItemWaiterTimeOutInMilliSeconds);
            new Thread(
                () =>
                {
                    Thread.Sleep(100);

                    autoResetEvent.Set();

                    IWorkItem workItem = workItemQueue.Dequeue();
                   
                    autoResetEvent.Set();

                    Assert.AreEqual(null, workItem);
                }).Start();

            autoResetEvent.WaitOne(500);

            Stopwatch stopwatch = Stopwatch.StartNew();
            workItemQueue.ShutDown();
            autoResetEvent.WaitOne(workItemWaiterTimeOutInMilliSeconds + 100);
            stopwatch.Stop();

            Assert.Less(stopwatch.ElapsedMilliseconds, workItemWaiterTimeOutInMilliSeconds);
        }
        public void DequeueShouldReturnNullWhenTheWaiterIsTimedOut()
        {
            IWorkItem workItem = Substitute.For<IWorkItem>();

            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 300);
            new Thread(
                () =>
                    {
                        Thread.Sleep(1000);

                        workItemQueue.Enqueue(workItem);
                        autoResetEvent.Set();
                    }).Start();

            Assert.AreEqual(null, workItemQueue.Dequeue());

            autoResetEvent.WaitOne();

            Assert.AreEqual(1, workItemQueue.Count);
            Assert.AreEqual(0, workItemQueue.WorkItemWaiterEntryStack.Count);
        }
        public void DequeueShouldReturnQueuedWorkItemWhenTheWaiterIsNotTimedOut()
        {
            IWorkItem workItem = Substitute.For<IWorkItem>();

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000);
            new Thread(
                () =>
                    {
                        Thread.Sleep(200);

                        workItemQueue.Enqueue(workItem);
                    }).Start();
            

            Assert.AreEqual(workItem, workItemQueue.Dequeue());
            Assert.AreEqual(0, workItemQueue.Count);
            Assert.AreEqual(0, workItemQueue.WorkItemWaiterEntryStack.Count);
        }
        public void DequeueShouldReturnQueuedWorkItemWhenQueueIsNotEmpty()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000);

            IWorkItem workItemFirst = Substitute.For<IWorkItem>();
            IWorkItem workItemLast = Substitute.For<IWorkItem>();

            workItemQueue.Enqueue(workItemFirst);
            workItemQueue.Enqueue(workItemLast);

            Assert.AreEqual(workItemFirst, workItemQueue.Dequeue());
            Assert.AreEqual(workItemLast, workItemQueue.Dequeue());
        }