Esempio n. 1
0
        public async Task TestRunWithTaskScheduler1()   // Aligned with the coroutine test TestExecuteRepeated1
        {
            QueuedTaskScheduler scheduler = new QueuedTaskScheduler(TaskScheduler.Default, maxConcurrencyLevel: 1);
            var cancel = new CancellationTokenSource();

            // Create both tasks at the same time:
            Task          t1 = TaskV2.Run(SomeAsyncTask1, cancel, scheduler);
            Task <string> t2 = TaskV2.Run(SomeAsyncTask2, cancel, scheduler);

            Assert.Equal(1, scheduler.GetRemainingScheduledTaskCount()); // 1 task started and 1 waiting
            Assert.Equal("Some string result", await t2);
            Assert.Equal(0, scheduler.GetRemainingScheduledTaskCount());

            // Since the scheduler allows only one task at a time, if t2 is done, t1 also must be completed:
            Assert.True(t1.IsCompleted);
        }
Esempio n. 2
0
        public async Task TestRunWithTaskScheduler2()   // Aligned with the coroutine test TestExecuteRepeated1
        {
            var maxConcurrencyLevel       = 2;
            QueuedTaskScheduler scheduler = new QueuedTaskScheduler(TaskScheduler.Default, maxConcurrencyLevel);
            var cancel = new CancellationTokenSource();

            // Create both tasks at the same time:
            Task          t1 = TaskV2.Run(SomeAsyncTask1, cancel, scheduler);
            Task <string> t2 = TaskV2.Run(SomeAsyncTask2, cancel, scheduler);
            var           t3 = TaskV2.Run(SomeAsyncTask1, cancel, scheduler); // Add a 3rd task (will not be started)

            Assert.True(scheduler.GetRemainingScheduledTaskCount() >= 1);

            Assert.Equal("Some string result", await t2);

            // Check that now also task t3 was started:
            Assert.Equal(0, scheduler.GetRemainingScheduledTaskCount());

            // Since the scheduler allows 2 tasks at a time, t1 will not be complete when t2 is done:
            Assert.False(t1.IsCompleted);
            await t1;
        }