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 EnqueueShouldThrowExceptionWhenItIsCalledAfterShutDown()
        {
            WorkItemQueue workItemQueue = new WorkItemQueue(1 * 300);

            workItemQueue.ShutDown();

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