コード例 #1
0
        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());
        }
コード例 #2
0
        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();
            }
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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();
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        public void EnqueueShouldThrowExceptionWhenItIsCalledAfterShutDown()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 300);

            workItemQueue.ShutDown();

            Assert.Throws <LaboThreadingException>(() => workItemQueue.Enqueue(Substitute.For <IWorkItem>()));
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
 public void EnqueueShouldThrowExceptionWhenItIsCalledAfterShutDown()
 {
     WorkItemQueue workItemQueue = new WorkItemQueue(1 * 300);
     workItemQueue.ShutDown();
     
     Assert.Throws<LaboThreadingException>(() => workItemQueue.Enqueue(Substitute.For<IWorkItem>()));
 }
コード例 #16
0
        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);
        }
コード例 #17
0
        public void EnqueueShouldThrowExceptionWhenWorkItemIsEmpty()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, Substitute.For<IWorkItemWaiterEntryStack>());

            Assert.Throws<ArgumentNullException>(() => workItemQueue.Enqueue(null));
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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());
        }
コード例 #20
0
        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();
        }
コード例 #21
0
        public void EnqueueShouldThrowExceptionWhenWorkItemIsEmpty()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 1000, Substitute.For <IWorkItemWaiterEntryStack>());

            Assert.Throws <ArgumentNullException>(() => workItemQueue.Enqueue(null));
        }
コード例 #22
0
        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();
 }
コード例 #24
0
        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);
        }