public void BlockQueueUntilBelowBoundedCapacity() { var monitor = new FakeMonitor(); var threadPool = new FakeThreadPool(); var blocked = new ManualResetEvent(false); var released = new ManualResetEvent(false); var taskScheduler = new BlockingThreadPoolTaskScheduler(1, threadPool, monitor); monitor.BeforeWait = () => blocked.Set(); monitor.AfterPulse = () => released.Set(); Task.Factory.StartNew(() => { // Schedule first task (non-blocking). Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); // Schedule second task (blocking). Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); }); // Wait for second task to be blocked. Assert.True(blocked.WaitOne(TimeSpan.FromMilliseconds(100))); Assert.Equal(1, threadPool.UserWorkItems.Count); threadPool.RunNext(); // Wait for second task to be released. Assert.True(released.WaitOne(TimeSpan.FromMilliseconds(100))); threadPool.RunNext(); Assert.Equal(0, taskScheduler.ScheduledTasks.Count()); }
public void BlockQueueUntilBelowBoundedCapacity() { var monitor = new FakeMonitor(); var threadPool = new FakeThreadPool(); var blocked = new ManualResetEvent(false); var released = new ManualResetEvent(false); var taskScheduler = new BlockingThreadPoolTaskScheduler(1, threadPool, monitor); monitor.BeforeWait = () => blocked.Set(); monitor.AfterPulse = () => released.Set(); Task.Factory.StartNew(() => { // Schedule first task (non-blocking). Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); // Schedule second task (blocking). Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); }); // Wait for second task to be blocked. Assert.True(blocked.WaitOne(TimeSpan.FromMilliseconds(100))); Assert.Equal(1, threadPool.UserWorkItems.Count); threadPool.RunNext(); // Wait for second task to be released. Assert.True(released.WaitOne(TimeSpan.FromMilliseconds(100))); threadPool.RunNext(); Assert.Equal(0, taskScheduler.ScheduledTasks.Count()); }
public void QueueImmediatelyIfBelowBoundedCapacity() { var taskScheduler = new BlockingThreadPoolTaskScheduler(1); var task = Task.Factory.StartNew(() => { Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); }); Assert.True(task.Wait(TimeSpan.FromMilliseconds(100))); Task.WaitAll(taskScheduler.ScheduledTasks.ToArray()); }
public void QueueImmediatelyIfBelowBoundedCapacity() { var taskScheduler = new BlockingThreadPoolTaskScheduler(1); var task = Task.Factory.StartNew(() => { Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); }); Assert.True(task.Wait(TimeSpan.FromMilliseconds(100))); Task.WaitAll(taskScheduler.ScheduledTasks.ToArray()); }
public void AllowsInlineExecutionAfterBeingQueued() { var executions = 0; var taskScheduler = new BlockingThreadPoolTaskScheduler(); var task = new Task(() => { executions++; }, CancellationToken.None, TaskCreationOptions.AttachedToParent); task.RunSynchronously(taskScheduler); Assert.Equal(1, executions); Assert.Equal(0, taskScheduler.ScheduledTasks.Count()); }
public void RunOnThreadPoolThreadIfNotLongRunning() { var isThreadPool = false; var taskComplete = new ManualResetEvent(false); var taskScheduler = new BlockingThreadPoolTaskScheduler(1); Task.Factory.StartNew(() => { isThreadPool = Thread.CurrentThread.IsThreadPoolThread; taskComplete.Set(); }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); Assert.True(taskComplete.WaitOne(TimeSpan.FromMilliseconds(100))); Assert.True(isThreadPool); }
public void AllowsInlineExecutionAfterBeingQueued() { var executions = 0; var monitor = new FakeMonitor(); var threadPool = new FakeThreadPool(); var taskScheduler = new BlockingThreadPoolTaskScheduler(1, threadPool, monitor); Task.Factory.StartNew(() => { executions++; }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); threadPool.RunNext(2); Assert.Equal(1, executions); Assert.Equal(0, taskScheduler.ScheduledTasks.Count()); }
public void AllowsInlineExecutionAfterBeingQueued() { var executions = 0; var monitor = new FakeMonitor(); var threadPool = new FakeThreadPool(); var taskScheduler = new BlockingThreadPoolTaskScheduler(1, threadPool, monitor); Task.Factory.StartNew(() => { executions++; }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); threadPool.RunNext(2); Assert.Equal(1, executions); Assert.Equal(0, taskScheduler.ScheduledTasks.Count()); }
public void AllowsInlineExecutionAfterBeingQueued() { var executions = 0; var taskScheduler = new BlockingThreadPoolTaskScheduler(); var task = new Task(() => { executions++; }, CancellationToken.None, TaskCreationOptions.AttachedToParent); task.RunSynchronously(taskScheduler); Assert.Equal(1, executions); Assert.Equal(0, taskScheduler.ScheduledTasks.Count()); }
public void RunOnThreadPoolThreadIfNotLongRunning() { var isThreadPool = false; var taskComplete = new ManualResetEvent(false); var taskScheduler = new BlockingThreadPoolTaskScheduler(1); Task.Factory.StartNew(() => { isThreadPool = Thread.CurrentThread.IsThreadPoolThread; taskComplete.Set(); }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler); Assert.True(taskComplete.WaitOne(TimeSpan.FromMilliseconds(100))); Assert.True(isThreadPool); }