示例#1
0
        static async Task Main(string[] args)
        {
            System.Console.WriteLine("Start TaskBatch Test!");
            System.Console.WriteLine("Input Example:");

            var array = new int[10] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            };

            foreach (var i in array)
            {
                System.Console.Write($"{i}:");
            }

            System.Console.WriteLine("");

            TaskBatch <ResultObject> taskBatch = new TaskBatch <ResultObject>(4);

            for (int index = 0; index < array.Length; index++)
            {
                await taskBatch.Add(NextSumValue(index, 10));
            }

            var result = await taskBatch.GetResults();

            System.Console.WriteLine("End TaskBatch process");
            System.Console.WriteLine("Example Result:");
            foreach (var r in result)
            {
                System.Console.Write($"{r.Value}:");
            }

            System.Console.ReadLine();
        }
示例#2
0
        public async Task Batching_Tasks_Less_MaxBatch_Correct()
        {
            var array = new int[10] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            };
            var resultArray = new int[10] {
                10, 11, 12, 13, 14, 15, 16, 17, 18, 19
            };

            TaskBatch <ResultObject> taskBatch = new TaskBatch <ResultObject>(5);

            for (int index = 0; index < array.Length; index++)
            {
                await taskBatch.Add(NextSumValue(index, 10));
            }

            var result = await taskBatch.GetResults();

            Assert.Equal(result.Select(r => r.Value), resultArray);
        }
示例#3
0
        public async Task Batching_Tasks_Greater_MaxBatch_Correct()
        {
            var array = new int[20] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
            };
            var resultArray = new int[20] {
                10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29
            };

            TaskBatch <ResultObject> taskBatching = new TaskBatch <ResultObject>(maxNumBatch: 5);

            for (int index = 0; index < array.Length; index++)
            {
                await taskBatching.Add(NextSumValue(index, 10));
            }

            var result = await taskBatching.GetResults();

            Assert.Equal(result.Select(r => r.Value), resultArray);
        }
示例#4
0
        public async Task Batching_Tasks_Greater_Parallel_MaxBatch_Correct()
        {
            var array = new int[20] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
            };
            var resultArray = new int[20] {
                10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29
            };

            TaskBatch <ResultObject> taskBatching = new TaskBatch <ResultObject>(maxNumBatch: 5);

            Parallel.For(0, array.Length, async(index, p) =>
            {
                await taskBatching.Add(NextSumValue(index, 10));
            });

            var result = await taskBatching.GetResults();

            Assert.True(result.All(e => resultArray.Contains(e.Value)));
        }