示例#1
0
        public static void ParallelAsync_For_Func_Delay(int?maxDop)
        {
            var options = maxDop.HasValue ? new ParallelOptions {
                MaxDegreeOfParallelism = maxDop.Value
            } : null;

            var sw = new Stopwatch();

            sw.Start();

            var actual = 0;
            Func <int, Task <KeyValuePair <int, int> > > func = async n =>
            {
                Interlocked.Increment(ref actual);
                await Task.Delay(delay);

                return(new KeyValuePair <int, int>(n, n * 2));
            };

            var result = ParallelAsync.ForAsync(0, loops, options, func).Result;

            sw.Stop();

            Assert.Equal(loops, actual);
            Assert.Equal(loops, result.Count);
            Assert.True(sw.ElapsedMilliseconds < delay * loops); // Environmental factors mean we can't assert a lower boundary
        }
示例#2
0
        public static void ParallelAsync_For_Func(int?maxDop)
        {
            var data    = new int[] { 0, 1, 2 };
            var options = maxDop.HasValue ? new ParallelOptions {
                MaxDegreeOfParallelism = maxDop.Value
            } : null;

            Func <int, Task <int> > func = i =>
            {
                return(Task.FromResult(data[i] * 2));
            };

            var actual = ParallelAsync.ForAsync(0, data.Length, options, func);

            Assert.Collection(actual.Result, n => Assert.Equal(0, n.Value), n => Assert.Equal(2, n.Value), n => Assert.Equal(4, n.Value));
        }
示例#3
0
        public static void ParallelAsync_For_Action(int?maxDop)
        {
            var data    = new int[] { 0, 1, 2 };
            var options = maxDop.HasValue ? new ParallelOptions {
                MaxDegreeOfParallelism = maxDop.Value
            } : null;

            Func <int, Task> action = i =>
            {
                data[i] = data[i] * 2;
                return(Task.CompletedTask);
            };

            ParallelAsync.ForAsync(0, data.Length, options, action).Wait();

            Assert.Collection(data, n => Assert.Equal(0, n), n => Assert.Equal(2, n), n => Assert.Equal(4, n));
        }
示例#4
0
        public static void ParallelAsync_For_Func_Default_Arguments(int?maxDop)
        {
            var data    = new int[] { 0, 1, 2 };
            var options = maxDop.HasValue ? new ParallelOptions {
                MaxDegreeOfParallelism = maxDop.Value
            } : null;

            // Null body
            Func <int, Task <KeyValuePair <int, int> > > func = null;

            Assert.ThrowsAsync <ArgumentNullException>(() => ParallelAsync.ForAsync(0, data.Length, options, func));

            var actual = 0;

            func = n =>
            {
                Interlocked.Increment(ref actual);
                return(Task.FromResult(new KeyValuePair <int, int>(n, n * 2)));
            };

            // Bad range
            Assert.ThrowsAsync <ArgumentNullException>(() => ParallelAsync.ForAsync(0, -1, options, func));

            // Empty source (-1)
            actual = 0;
            var result = ParallelAsync.ForAsync(-1, -1, options, func).Result;

            Assert.Equal(0, actual);
            Assert.Empty(result);

            // Empty source (0)
            actual = 0;
            result = ParallelAsync.ForAsync(0, 0, options, func).Result;
            Assert.Equal(0, actual);
            Assert.Empty(result);

            // Empty source (100)
            actual = 0;
            result = ParallelAsync.ForAsync(100, 100, options, func).Result;
            Assert.Equal(0, actual);
            Assert.Empty(result);
        }
示例#5
0
        public static void ParallelAsync_For_Action_Default_Arguments(int?maxDop)
        {
            var data    = new int[] { 0, 1, 2 };
            var options = maxDop.HasValue ? new ParallelOptions {
                MaxDegreeOfParallelism = maxDop.Value
            } : null;

            // Null body
            Func <int, Task> action = null;

            Assert.ThrowsAsync <ArgumentNullException>(() => ParallelAsync.ForAsync(0, data.Length, options, action));

            var actual = 0;

            action = n =>
            {
                Interlocked.Increment(ref actual);
                return(Task.CompletedTask);
            };

            // Bad range
            Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => ParallelAsync.ForAsync(0, -1, options, action));

            // Empty source (-1)
            actual = 0;
            ParallelAsync.ForAsync(-1, -1, options, action).Wait();
            Assert.Equal(0, actual);

            // Empty source (0)
            actual = 0;
            ParallelAsync.ForAsync(0, 0, options, action).Wait();
            Assert.Equal(0, actual);

            // Empty source (100)
            actual = 0;
            ParallelAsync.ForAsync(100, 100, options, action).Wait();
            Assert.Equal(0, actual);
        }
示例#6
0
        public static void ParallelAsync_For_Action_Delay(int?maxDop)
        {
            var options = maxDop.HasValue ? new ParallelOptions {
                MaxDegreeOfParallelism = maxDop.Value
            } : null;

            var sw = new Stopwatch();

            sw.Start();

            var actual            = 0;
            Func <int, Task> func = async i =>
            {
                Interlocked.Increment(ref actual);
                await Task.Delay(delay);
            };

            ParallelAsync.ForAsync(0, loops, options, func).Wait();

            sw.Stop();

            Assert.Equal(loops, actual);
            Assert.True(sw.ElapsedMilliseconds < delay * loops); // Environmental factors mean we can't assert a lower boundary
        }