public void PoolShouldNotAcceptNewTasksAfterShutdown() { var pool = new CustomThreading.MyThreadPool(10); pool.AddTask(() => 5); pool.Shutdown(); for (int i = 0; i < 10; ++i) { Assert.AreEqual(null, pool.AddTask(() => 10)); } }
public void SimpleTestShouldWork() { var pool = new CustomThreading.MyThreadPool(10); var task = pool.AddTask(() => 5); Assert.AreEqual(5, task.Result); }
public void AvailableThreadCounterShouldWorkCorrectlyInNonEmptyPool() { var pool = new CustomThreading.MyThreadPool(10); var task = pool.AddTask(() => { System.Threading.Thread.Sleep(50); return(5); }); Assert.AreEqual(pool.MaxThreadCount, pool.AvailableThreadCount); }
public void ContinueWithShouldWorkCorrectlyOnSimpleTest() { var pool = new CustomThreading.MyThreadPool(5); var task = pool.AddTask(() => 5); var t = task.Result; var nestedTask = task.ContinueWith(five => five + 5); Assert.AreEqual(10, nestedTask.Result); Assert.AreEqual(5, task.Result, "Result of main task was corrupted!"); }
public void NestedTaskShouldWorkCorrectlyWithLongMainTask() { var pool = new CustomThreading.MyThreadPool(5); var mainTask = pool.AddTask(() => { System.Threading.Thread.Sleep(100); return(10); }); var nestedTask = mainTask.ContinueWith(mainValue => mainValue + 15); Assert.AreEqual(25, nestedTask.Result); }
public void AvailableThreadCountShouldBeMaximalWhenPoolIsFullyBusy() { var pool = new CustomThreading.MyThreadPool(5); for (int i = 0; i < 10; ++i) { pool.AddTask(() => { System.Threading.Thread.Sleep(50); return(5); }); } Assert.AreEqual(pool.MaxThreadCount, pool.AvailableThreadCount); }
public void PoolShouldProceedSeveralNestedTasksFromOneTaskCorrectly() { var pool = new CustomThreading.MyThreadPool(5); var mainTask = pool.AddTask(() => 5); var nestedTasks = new CustomThreading.IMyTask <int> [10]; for (int i = 0; i < 10; ++i) { var localI = i; nestedTasks[i] = mainTask.ContinueWith(mainValue => mainValue + localI); } for (int i = 0; i < 10; ++i) { Assert.AreEqual(5 + i, nestedTasks[i].Result); } }
public void ShouldWorkCorrectlyWhenTaskCountMoreThanThreadCount() { var pool = new CustomThreading.MyThreadPool(3); var tasks = new CustomThreading.IMyTask <int> [10]; for (int i = 0; i < 10; ++i) { var localI = i; tasks[i] = pool.AddTask(() => { System.Threading.Thread.Sleep(100); return(localI); }); } for (int i = 0; i < 10; ++i) { Assert.AreEqual(i, tasks[i].Result); } }
public static void Main(string[] args) { const int taskCount = 10; const int availableThreadsInPool = 3; var pool = new CustomThreading.MyThreadPool(availableThreadsInPool); var tasks = new CustomThreading.IMyTask <int> [taskCount]; for (int i = 0; i < tasks.Length; ++i) { tasks[i] = pool.AddTask(TaskReturn15); } Console.WriteLine("There should be 15 (x{0}):", taskCount); foreach (var task in tasks) { Console.WriteLine(task.Result); } var nestedTasks = new CustomThreading.IMyTask <int> [taskCount]; for (int i = 0; i < tasks.Length; ++i) { nestedTasks[i] = tasks[i].ContinueWith(TaskReturnOldPlus38); } Console.WriteLine("There should be 53 (x{0}):", taskCount); foreach (var task in nestedTasks) { Console.WriteLine(task.Result); } Console.WriteLine("And there should be 15 (x{0}) again:", taskCount); foreach (var task in tasks) { Console.WriteLine(task.Result); } }
public void PoolShutdownShouldNotAffectPreviousTasks() { var pool = new CustomThreading.MyThreadPool(10); var tasks = new CustomThreading.IMyTask <int> [10]; for (int i = 0; i < 10; ++i) { var localI = i; tasks[i] = pool.AddTask(() => { System.Threading.Thread.Sleep(50); return(localI); }); } System.Threading.Thread.Sleep(50); pool.Shutdown(); for (int i = 0; i < 10; ++i) { Assert.AreEqual(i, tasks[i].Result); } }