Ejemplo n.º 1
0
        public async Task NoPending()
        {
            var         executionManager = new ThreadPoolExecutionManager(new LoggerFactory());
            object      locker           = new object();
            List <Task> tasks            = new List <Task>();
            int         count            = 50;
            int         finishedTasks    = 0;

            //Launch many threads.
            for (int i = 0; i < count; i++)
            {
                var t = executionManager.ProcessUpdate(async() =>
                {
                    Thread.Sleep(1000);
                    lock (locker)
                    {
                        finishedTasks++;
                    }
                });
                tasks.Add(t);
            }

            //Await all.
            await executionManager.AwaitAllPending();

            Assert.Zero(executionManager.PendingTasksCount);
            Assert.AreEqual(count, finishedTasks);
        }
Ejemplo n.º 2
0
        public async Task AllPending_TaskAwait()
        {
            var executionManager = new ThreadPoolExecutionManager(new LoggerFactory());
            var tasks            = new List <Task>();
            int count            = 200;
            int finishedTasks    = 0;
            var tasksSources     = new List <TaskCompletionSource <object> >();

            //Launch many threads.
            for (int i = 0; i < count; i++)
            {
                var t = executionManager.ProcessUpdate(async() =>
                {
                    var tcs = new TaskCompletionSource <object>(TaskContinuationOptions.RunContinuationsAsynchronously);
                    lock (tasksSources)
                    {
                        tasksSources.Add(tcs);
                    }
                    await tcs.Task;
                    finishedTasks++;
                });
                tasks.Add(t);
            }

            var pendingTasksCount = executionManager.PendingTasksCount;
            var finishedTasksRes  = finishedTasks;

            //Await all.
            await executionManager.AwaitAllPending(TimeSpan.FromMilliseconds(100));

            //Release threads.
            lock (tasksSources)
            {
                for (int i = 0; i < count; i++)
                {
                    //If we not "release all task - threads will not been locked and application will not freeze.
                    //Thats how Task works.
                    tasksSources[i].SetCanceled();
                }
            }

            await executionManager.AwaitAllPending();

            Assert.AreEqual(count, pendingTasksCount);
            Assert.Zero(finishedTasksRes);
        }
Ejemplo n.º 3
0
        public async Task AllPending_ThreadLock()
        {
            var         executionManager = new ThreadPoolExecutionManager(new LoggerFactory());
            var         locker           = new object();
            List <Task> tasks            = new List <Task>();
            int         count            = 200;
            int         finishedTasks    = 0;
            Semaphore   are = new Semaphore(0, int.MaxValue);

            //Launch many threads.
            for (int i = 0; i < count; i++)
            {
                var t = executionManager.ProcessUpdate(async() =>
                {
                    are.WaitOne();
                    finishedTasks++;
                });
                tasks.Add(t);
            }

            var pendingTasksCount = executionManager.PendingTasksCount;
            var finishedTasksRes  = finishedTasks;

            //Await all.
            await executionManager.AwaitAllPending(TimeSpan.FromMilliseconds(100));

            //Release threads.
            for (int i = 0; i < count; i++)
            {
                are.Release();
            }
            await executionManager.AwaitAllPending();

            Assert.AreEqual(count, pendingTasksCount);
            Assert.Zero(finishedTasksRes);
        }