Example #1
0
        public static void Main(string[] args)
        {
            var t = new TaskQueue(maxParallelizationCount: 2, maxQueueLength: 2);
            t.Queue(() => DoTask(1)); // Runs this on 1st batch.

            // Works even without following `Wait()`, in that case the first
            // starts immediately the second near instantly following, and third
            // waits until either 1 or 2 completes.
            t.Process().Wait();
            Console.WriteLine("First batch should have ran to completion.");
            Console.WriteLine("");

            t.Queue(() => DoTask(2)); // Runs this on 2nd batch
            t.Queue(() => DoTask(3)); // Runs this on 2nd batch

            t.Queue(() => DoTask(4)); // Not ran, capped
            t.Queue(() => DoTask(5)); // Not ran, capped
            t.Queue(() => DoTask(6)); // Not ran, capped
            t.Process().Wait();
            Console.WriteLine($"Processed second batch. Queue and running tasks should be empty.");
            Console.WriteLine($"Queue has now {t.GetQueueCount()} future tasks, and {t.GetRunningCount()} running tasks.");
            Console.WriteLine("");

            t.Queue(() => DoTask(7)); // Runs this on 2nd batch
            t.Queue(() => DoTask(8)); // Runs this on 2nd batch

            t.Queue(() => DoTask(9)); // Not ran, capped
            Console.WriteLine($"Queued. Queue should have two future tasks, and nothing running yet.");
            Console.WriteLine($"Queue has now {t.GetQueueCount()} future tasks, and {t.GetRunningCount()} running tasks.");

            t.Process().Wait();
            Console.WriteLine("Completed, press any key to quit.");
            Console.ReadLine();
            Console.WriteLine("The End!");
        }
Example #2
0
 public async Task TestEmptyRun()
 {
     var t = new TaskQueue(maxParallelizationCount: 4);
     await t.Process();
 }
Example #3
0
 public async Task TestQueueLength()
 {
     var t = new TaskQueue(maxQueueLength: 2);
     t.Queue(async () => { await Task.Delay(40); });
     t.Queue(async () => { await Task.Delay(40); });
     t.Queue(async () => { await Task.Delay(40); }); // Dropped, not ran
     t.Queue(async () => { await Task.Delay(40); }); // Dropped, not ran
     Assert.Equal(2, t.GetQueueCount());
     await t.Process();
 }
Example #4
0
        public async Task TestMaxParallelization()
        {
            var t = new TaskQueue(maxParallelizationCount: 4);
            var n = 0;
            // Sequential delays should ensure that tasks complete in order for
            // `n` to grow linearly
            t.Queue(async () => { await Task.Delay(40); n++; });
            t.Queue(async () => { await Task.Delay(50); n++; });
            t.Queue(async () => { await Task.Delay(60); n++; });
            t.Queue(async () => { await Task.Delay(70); n++; });

            // Following are queued and will be run as above tasks complete
            // Task delay for the first must be 40 because 40 + 40 > 70 
            t.Queue(async () => { await Task.Delay(40); n++; }); 
            t.Queue(async () => { await Task.Delay(50); n++; }); 
            
            // Intentionally not awaited, starts tasks asynchronously
            t.ProcessBackground();
            
            // Wait for tasks to start
            await Task.Delay(10);

            // Tasks should now be running
            Assert.Equal(4, t.GetRunningCount());
            
            await t.Process();

            // Queue and running tasks should now have ran to completion
            Assert.Equal(0, t.GetRunningCount());
            Assert.Equal(0, t.GetQueueCount());
            Assert.Equal(6, n);
        }
Example #5
0
 public async Task TestEmptyRun()
 {
     var t = new TaskQueue(maxParallelizationCount: 4);
     await t.Process();
 }