Example #1
0
        public void FailedTaskTest()
        {
            Debug.WriteLine("==== FailedTask ====");
            _capacity = 1;
            Init(_capacity);
            var task = MyTask <string> .New(() =>
            {
                var list = new List <string> {
                    "task #0"
                };
                return(list[5]);
            });

            Debug.WriteLine("\tenqueue one task");
            _pool.Enqueue(task);

            Debug.WriteLine("\twait for failed result");
            _sp.Restart();

            var exception = Assert.Throws <AggregateException>(() =>
            {
                Debug.WriteLine($"\tresult: {task.Result} in {_sp.ElapsedMilliseconds} ms");
            });

            Debug.WriteLine($"\tfailed result: {exception.Message}");
            Assert.NotNull(exception.InnerException);
            Assert.IsInstanceOf(typeof(ArgumentOutOfRangeException), exception.InnerException);
            Debug.WriteLine($"\twith inner exception: {exception.InnerException.Message}");
            Debug.WriteLine("=================");
        }
Example #2
0
 private IMyTask <string> GetTask(int number)
 {
     return(MyTask <string> .New(() =>
     {
         Debug.WriteLine($"\t\trun task #{number}");
         Thread.Sleep(_rnd.Next(500));
         Debug.WriteLine($"\t\tend task #{number}");
         return $"task #{number}";
     }));
 }
Example #3
0
        public void AddTasksAndDisposeTest()
        {
            Debug.WriteLine("==== AddTasksAndDispose ====");
            _capacity = 1;
            Init(_capacity);
            var task1 = MyTask <string> .New(() =>
            {
                Debug.WriteLine("\t\tinside task #1");
                Thread.Sleep(2000);
                Debug.WriteLine("\t\treturn from task #1");
                return("task #1");
            });

            var task2 = task1.ContinueWith(result =>
            {
                Debug.WriteLine("\t\tinside task #2");
                Thread.Sleep(100);
                Debug.WriteLine("\t\treturn from task #2");
                return($"task #2 after {result}");
            });

            Debug.WriteLine("\tenqueue task #1");
            _pool.Enqueue(task1);
            Debug.WriteLine("\tenqueue task #2");
            _pool.Enqueue(task2);

            _sp.Start();
            Debug.WriteLine("\twait for result #1");
            Debug.WriteLine($"\tresult #1: {task1.Result} in {_sp.ElapsedMilliseconds} ms");

            Debug.WriteLine("\tdispose threadpool");
            _pool.Dispose();

            Debug.WriteLine("\ttry to get result #2:");
            _sp.Restart();
            Debug.WriteLine($"\tresult #2: {task2.Result} in {_sp.ElapsedMilliseconds} ms");
            Debug.WriteLine("=====================");
        }