public void CanItBeBusy()
 {
     WorkerThread wt = new WorkerThread(a=>Trace.WriteLine("yes, it can"));
     Assert.AreEqual(wt.Busy,false);
     wt.Busy = true;
     Assert.AreEqual(wt.Busy,true);
 }
        public void CanCompleteWork()
        {
            bool actualWorkDone = false;
            bool expectedWorkDone = true;

            WorkerThread wt = new WorkerThread(a=>Trace.WriteLine("yeah, I'm not doing this"));
            wt.WorkerAction = a => {actualWorkDone = (bool)a;};

            wt.CompleteWork(expectedWorkDone);
            Assert.AreEqual(expectedWorkDone,actualWorkDone);
        }
        public void CanEventOnError()
        {
            object o = null;
            bool expectedErrorCaught = true;
            bool actualErrorCaught = false;
            WorkerThread wt = new WorkerThread(a => {throw new Exception("this won't work");});
            wt.WorkerException += (sender, e) => actualErrorCaught = true;

            wt.CompleteWork(o);

            Assert.AreEqual(expectedErrorCaught,actualErrorCaught);
        }
        public void CanEventOnDone()
        {
            string workItem = String.Empty;
            bool expectedDoneSet = true;
            bool actualDoneSet = false;

            WorkerThread wt = new WorkerThread(a=>Trace.WriteLine("yeah, I'm not doing this"));
            wt.WorkItemComplete += (sender, e) => actualDoneSet = true;

            wt.CompleteWork(workItem);

            Assert.AreEqual(expectedDoneSet,actualDoneSet);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ThreadDistributor.Distributor"/> class.
        /// </summary>
        /// <param name="getMoreWork">Get more work function 
        /// <para>Takes an integer max number of work items to retrieve and</para>
        /// <para>returns a list of objects representing the work items.</para>
        /// </param>
        /// <param name="workerAction">Action method which takes a single object work item and acts upon it.</param>
        /// <param name="threadCount">The desired number of worker threads</param>
        /// <param name="checkInterval">A TimeSpan describing how often to check for work.
        /// <para>On successfully completing a WorkerAction, GetMoreWork is called again before the timer elapses.</para>
        /// </param>
        public Distributor(Func<int, List<object>> getMoreWork, Action<object> workerAction, int threadCount, TimeSpan checkInterval)
        {
            GetMoreWork = getMoreWork;
            _timer = new Timer(WaitToAssignWork, _dispatchResetEvent, Timeout.Infinite, Timeout.Infinite);
            _timerInterval = checkInterval;

            _availableThreads = new List<WorkerThread>(threadCount);
            for (int i=0; i<threadCount; i++)
            {
                WorkerThread wt = new WorkerThread(workerAction);
                wt.WorkerException += WorkerEncounteredException;
                wt.WorkItemComplete += ImmediatelyWorkNextItem;

                _availableThreads.Add(wt);
            }
        }
 public void WillItWorkerThread()
 {
     WorkerThread wt = new WorkerThread(a=>Trace.WriteLine("yes, it will"));
     Assert.IsInstanceOf<WorkerThread>(wt);
 }