public void BlockQueueUntilBelowBoundedCapacity()
            {
                var monitor       = new FakeMonitor();
                var threadPool    = new FakeThreadPool();
                var blocked       = new ManualResetEvent(false);
                var released      = new ManualResetEvent(false);
                var taskScheduler = new PartitionedTaskScheduler(_ => 0, 1, 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());
            }
Example #2
0
            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 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());
            }