public void Should_enqueue_given_task_and_run_into_free_worker() { var startSignal = new CountdownEvent(1); workersPool.PushTask(WorkerTasks.Task("task 1", startSignal: startSignal)); startSignal.Wait(); workersPool.TasksQueueSize.Should().Be(0); }
public void Should_reset_in_progress_tasks_when_they_completed() { var finishSignal = new CountdownEvent(3); workersPool.PushTask(WorkerTasks.Task("task 1", finishSignal: finishSignal)); workersPool.PushTask(WorkerTasks.Task("task 2", finishSignal: finishSignal)); workersPool.PushTask(WorkerTasks.Task("task 3", finishSignal: finishSignal)); finishSignal.Wait(); Thread.Sleep(100); workersPool.TaskInProgress.Should().Be(0); }
public void Should_execute_all_tasks_after_many_iterations(int tasksCount, int workersCount) { using (var newWorkersPool = new WorkersPool.WorkersPool(workersCount, new TestLog(output))) { var expectedResults = Enumerable.Range(0, tasksCount).ToList(); var actualResults = new int[tasksCount]; var startSignals = Enumerable.Range(0, tasksCount).Select(_ => new CountdownEvent(1)).ToArray(); var stopwatch = new Stopwatch(); using (new Disposables(startSignals)) using (var finishSignal = new CountdownEvent(tasksCount)) { var workerTasks = Enumerable.Range(0, tasksCount) .Select( id => WorkerTasks.Task( "task " + id, startSignal: startSignals[id], finishSignal: finishSignal, action: () => actualResults[id] = id ) ) .ToList(); stopwatch.Start(); for (var i = 0; i < workerTasks.Count; i++) { newWorkersPool.PushTask(workerTasks[i]); startSignals[i].Wait(); } finishSignal.Wait(5.Seconds()); stopwatch.Stop(); } actualResults.Should().BeEquivalentTo(expectedResults); output.WriteLine("Task finished for " + stopwatch.Elapsed); } }
public void Should_decrease_when_one_of_task_finished() { var task2FinishSignal = new CountdownEvent(1); var startSignal = new CountdownEvent(3); var task2BlockSignal = new SemaphoreSlim(0); workersPool.PushTask(WorkerTasks.UnfinishedTask("task 1", startSignal)); workersPool.PushTask(WorkerTasks.Task("task 2", task2BlockSignal, startSignal, task2FinishSignal)); workersPool.PushTask(WorkerTasks.UnfinishedTask("task 3", startSignal)); startSignal.Wait(); output.WriteLine("All task started"); var beforeOneFinish = workersPool.TaskInProgress; task2BlockSignal.Release(); task2FinishSignal.Wait(); Thread.Sleep(100); output.WriteLine("Task 2 finished"); beforeOneFinish.Should().Be(3); workersPool.TaskInProgress.Should().Be(2); }