public void WaitForAllWorkToBeDone_WorkersBusyAllTheTime_Passes() { var workerQueue = new MultithreadedWorkerQueue <bool>(() => { var processer = Substitute.For <IProcesser <bool> >(); processer.When(x => x.Process(false)).Do(x => { Thread.Sleep(100); }); return(processer); }, 2); workerQueue.Start(); const int amountOfItterations = 50; for (var i = 0; i < amountOfItterations; i++) { workerQueue.Enqueue(false); } var completed = 0; while (completed != amountOfItterations) { if (workerQueue.TryDequeue(out _)) { completed++; } Thread.Sleep(10); } Assert.Equal(completed, amountOfItterations); }
/// <summary> /// Creates a new <see cref="Pathfinder{TSourceNodeNetwork,TThreadNodeNetwork, TPath}"/> /// </summary> /// <param name="definitionNodeNetwork"></param> /// <param name="pathFindAlgorithm"></param> /// <param name="processerConstructor">Used to construct the processers for each thread</param> /// <param name="threads">The amount of threads that will be used</param> public Pathfinder(TDefinitionNodeNetwork definitionNodeNetwork, IPathFindAlgorithm <TThreadNodeNetwork, TPath> pathFindAlgorithm, Func <TDefinitionNodeNetwork, IPathFindAlgorithm <TThreadNodeNetwork, TPath>, PathRequestProcesser <TThreadNodeNetwork, TPath> > processerConstructor, int threads = 1) { if (threads < 1) { throw new ArgumentException("There is a minimum of 1 thread"); } PathFindAlgorithm = pathFindAlgorithm; DefinitionNodeNetwork = definitionNodeNetwork; _multithreadedWorkerQueue = new MultithreadedWorkerQueue <PathRequest <TPath> >(() => { var processer = processerConstructor.Invoke(DefinitionNodeNetwork, pathFindAlgorithm); _pathfindNodeNetworks.Add(processer.NodeNetwork); return(processer); }, threads); }