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 EnqueueShouldAddWorkItemsToQueueWhenThereAreNoWaiters()
        {
            IWorkItem workItem = Substitute.For<IWorkItem>();

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 100);
            IList<Task> tasks = new List<Task>
                                    {
                                        Task.Factory.StartNew(() => workItemQueue.Enqueue(workItem)),
                                        Task.Factory.StartNew(() => workItemQueue.Enqueue(workItem)),
                                        Task.Factory.StartNew(() => workItemQueue.Enqueue(workItem))
                                    };
            Task.WaitAll(tasks.ToArray());

            Assert.AreEqual(3, workItemQueue.Count);
        }
示例#3
0
        public void Post(Action continuation)
        {
            if (continuation is null)
            {
                return;
            }
            var workItem = new WorkItem(continuation);

            _lock.Enter();
            try {
                _queue.Enqueue(workItem);
            }
            finally {
                _lock.Exit();
            }
        }
        public void EnqueueShouldAddWorkItemsToQueueWhenThereAreNoWaiters()
        {
            IWorkItem workItem = Substitute.For <IWorkItem>();

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 100);
            IList <Task>  tasks         = new List <Task>
            {
                Task.Factory.StartNew(() => workItemQueue.Enqueue(workItem)),
                Task.Factory.StartNew(() => workItemQueue.Enqueue(workItem)),
                Task.Factory.StartNew(() => workItemQueue.Enqueue(workItem))
            };

            Task.WaitAll(tasks.ToArray());

            Assert.AreEqual(3, workItemQueue.Count);
        }
        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 EnqueueTrySignalMethodShouldBeCalledWhenWaitingStackIsNotEmpty()
        {
            IWorkItemWaiterEntry      workItemWaiterEntry      = Substitute.For <IWorkItemWaiterEntry>();
            IWorkItemWaiterEntryStack workItemWaiterEntryStack = Substitute.For <IWorkItemWaiterEntryStack>();

            int count = 1;

            workItemWaiterEntryStack.Count.Returns(x => count);
            workItemWaiterEntryStack.Pop().Returns(
                x =>
            {
                if (count == 1)
                {
                    count = 0;
                    return(workItemWaiterEntry);
                }

                return(null);
            });

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, workItemWaiterEntryStack);

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

            workItemQueue.Enqueue(workItem);

            workItemWaiterEntry.Received(1).TrySignal(workItem);
        }
        public void EnqueueShouldThrowExceptionWhenItIsCalledAfterShutDown()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 300);

            workItemQueue.ShutDown();

            Assert.Throws <LaboThreadingException>(() => workItemQueue.Enqueue(Substitute.For <IWorkItem>()));
        }
        public void EnqueueTrySignalMethodShouldNotBeCalledWhenWaitingStackIsEmpty()
        {
            IWorkItemWaiterEntry      workItemWaiterEntry      = Substitute.For <IWorkItemWaiterEntry>();
            IWorkItemWaiterEntryStack workItemWaiterEntryStack = Substitute.For <IWorkItemWaiterEntryStack>();

            workItemWaiterEntryStack.Count.Returns(0);

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, workItemWaiterEntryStack);

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

            workItemQueue.Enqueue(workItem);

            workItemWaiterEntry.DidNotReceiveWithAnyArgs().TrySignal(workItem);
        }
        public void EnqueueShouldNotAddWorkItemToQueueWhenWaiterIsSignaled()
        {
            IWorkItem workItem = Substitute.For<IWorkItem>();

            IWorkItemWaiterEntry workItemWaiterEntry = Substitute.For<IWorkItemWaiterEntry>();
            workItemWaiterEntry.TrySignal(workItem).Returns(true);

            IWorkItemWaiterEntryStack workItemWaiterEntryStack = Substitute.For<IWorkItemWaiterEntryStack>();
            workItemWaiterEntryStack.Count.Returns(1);
            workItemWaiterEntryStack.Pop().Returns(workItemWaiterEntry);

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, workItemWaiterEntryStack);
            workItemQueue.Enqueue(workItem);

            Assert.AreEqual(0, workItemQueue.Count);
        }
        public void EnqueueShouldAddWorkItemToQueueWhenAllWaitersTimedOut()
        {
            Stack<IWorkItemWaiterEntry> workItemWaiterEntries = new Stack<IWorkItemWaiterEntry>();
            workItemWaiterEntries.Push(CreateNotSignalableWorkItemWaiterEntry());
            workItemWaiterEntries.Push(CreateNotSignalableWorkItemWaiterEntry());

            IWorkItemWaiterEntryStack workItemWaiterEntryStack = Substitute.For<IWorkItemWaiterEntryStack>();
            workItemWaiterEntryStack.Count.Returns(x => workItemWaiterEntries.Count);
            workItemWaiterEntryStack.Pop().Returns(x => workItemWaiterEntries.Pop());

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, workItemWaiterEntryStack);

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

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

            IWorkItemWaiterEntry workItemWaiterEntry = Substitute.For <IWorkItemWaiterEntry>();

            workItemWaiterEntry.TrySignal(workItem).Returns(true);

            IWorkItemWaiterEntryStack workItemWaiterEntryStack = Substitute.For <IWorkItemWaiterEntryStack>();

            workItemWaiterEntryStack.Count.Returns(1);
            workItemWaiterEntryStack.Pop().Returns(workItemWaiterEntry);

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, workItemWaiterEntryStack);

            workItemQueue.Enqueue(workItem);

            Assert.AreEqual(0, workItemQueue.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 EnqueueShouldAddWorkItemToQueueWhenAllWaitersTimedOut()
        {
            Stack <IWorkItemWaiterEntry> workItemWaiterEntries = new Stack <IWorkItemWaiterEntry>();

            workItemWaiterEntries.Push(CreateNotSignalableWorkItemWaiterEntry());
            workItemWaiterEntries.Push(CreateNotSignalableWorkItemWaiterEntry());

            IWorkItemWaiterEntryStack workItemWaiterEntryStack = Substitute.For <IWorkItemWaiterEntryStack>();

            workItemWaiterEntryStack.Count.Returns(x => workItemWaiterEntries.Count);
            workItemWaiterEntryStack.Pop().Returns(x => workItemWaiterEntries.Pop());

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, workItemWaiterEntryStack);

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

            workItemQueue.Enqueue(workItem);

            Assert.AreEqual(1, workItemQueue.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);
        }
 public void EnqueueShouldThrowExceptionWhenItIsCalledAfterShutDown()
 {
     WorkItemQueue workItemQueue = new WorkItemQueue(1 * 300);
     workItemQueue.ShutDown();
     
     Assert.Throws<LaboThreadingException>(() => workItemQueue.Enqueue(Substitute.For<IWorkItem>()));
 }
        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 EnqueueShouldThrowExceptionWhenWorkItemIsEmpty()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, Substitute.For<IWorkItemWaiterEntryStack>());

            Assert.Throws<ArgumentNullException>(() => workItemQueue.Enqueue(null));
        }
        public void EnqueueTrySignalMethodShouldBeCalledWhenWaitingStackIsNotEmpty()
        {
            IWorkItemWaiterEntry workItemWaiterEntry = Substitute.For<IWorkItemWaiterEntry>();
            IWorkItemWaiterEntryStack workItemWaiterEntryStack = Substitute.For<IWorkItemWaiterEntryStack>();
           
            int count = 1;
            workItemWaiterEntryStack.Count.Returns(x => count);
            workItemWaiterEntryStack.Pop().Returns(
                x =>
                    {
                        if (count == 1)
                        {
                            count = 0;
                            return workItemWaiterEntry;
                        }

                        return null;
                    });

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, workItemWaiterEntryStack);

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

            workItemWaiterEntry.Received(1).TrySignal(workItem);
        }
        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 EnqueueShouldThrowExceptionWhenWorkItemIsEmpty()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, Substitute.For <IWorkItemWaiterEntryStack>());

            Assert.Throws <ArgumentNullException>(() => workItemQueue.Enqueue(null));
        }
        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);
        }
示例#23
0
 /// <summary>
 /// Enqueues a workitem and notifies the controller mainthread that something needs to be done.
 /// </summary>
 /// <param name="workItem">The workitem to enqueue.</param>
 /// <returns>The enqueued workitem.</returns>
 public void EnqueueWorkItem(WorkItem workItem)
 {
     lock (_syncObj)
         _workItemQueue.Enqueue(workItem);
     _playerThreadNotifyEvent.Set();
 }
        public void EnqueueTrySignalMethodShouldNotBeCalledWhenWaitingStackIsEmpty()
        {
            IWorkItemWaiterEntry workItemWaiterEntry = Substitute.For<IWorkItemWaiterEntry>();
            IWorkItemWaiterEntryStack workItemWaiterEntryStack = Substitute.For<IWorkItemWaiterEntryStack>();
            workItemWaiterEntryStack.Count.Returns(0);

            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, workItemWaiterEntryStack);

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

            workItemWaiterEntry.DidNotReceiveWithAnyArgs().TrySignal(workItem);
        }