public void TestCompositeTaskRunner() { int attempts = 0; CompositeTaskRunner runner = new CompositeTaskRunner(); CountingTask task1 = new CountingTask("task1", 100); CountingTask task2 = new CountingTask("task2", 200); runner.AddTask( task1 ); runner.AddTask( task2 ); runner.Wakeup(); while( attempts++ != 10 ) { Thread.Sleep( 1000 ); if(task1.Count == 100 && task2.Count == 200) { break; } } Assert.IsTrue(task1.Count == 100); Assert.IsTrue(task2.Count == 200); runner.RemoveTask(task1); runner.RemoveTask(task2); }
public void TestCompositeTaskRunner() { int attempts = 0; CompositeTaskRunner runner = new CompositeTaskRunner(); CountingTask task1 = new CountingTask("task1", 100); CountingTask task2 = new CountingTask("task2", 200); runner.AddTask(task1); runner.AddTask(task2); runner.Wakeup(); while (attempts++ != 10) { Thread.Sleep(1000); if (task1.Count == 100 && task2.Count == 200) { break; } } Assert.IsTrue(task1.Count == 100); Assert.IsTrue(task2.Count == 200); runner.RemoveTask(task1); runner.RemoveTask(task2); }
public void CompositeTaskRunnerDoesntHoldLockWhileCallingIterate() { object lockObj = new object(); // Start a task running that takes a shared lock during it's iterate. CompositeTaskRunner runner = new CompositeTaskRunner(); runner.AddTask(new LockingTask(lockObj)); // Start a separate thread that holds that same lock whilst manipulating the CompositeTaskRunner (See InactivityMonitor for real example). AutoResetEvent resetEvent = new AutoResetEvent(false); ThreadPool.QueueUserWorkItem(_ => { for (int i = 0; i < 10000; i++) { lock (lockObj) { var countingTask = new CountingTask("task1", 100); runner.AddTask(countingTask); runner.RemoveTask(countingTask); } } resetEvent.Set(); }); // Wait for the second thread to finish 10000 attempts. Assert.That(resetEvent.WaitOne(TimeSpan.FromSeconds(10)), "The secondary lock user didn't finish 10000 iterations in less than 10 seconds. Probably dead locked!"); runner.Shutdown(); }