public async Task UnhandledExceptions2()
        {
            Exception testEx  = new Exception();
            Exception testEx2 = new Exception();

            ForEach <int> _forEach = Builder
                                     .For(Enumerable.Range(1, 100))
                                     .Each((T, token) =>
            {
                if (T == 50)
                {
                    throw testEx;
                }

                return(Task.Delay(10, token));
            })
                                     .WithInitialDegreeOfParallelism(10)
                                     .WhenException((_, ex) =>
            {
                Assert.AreEqual(testEx, ex);
                throw testEx2;
            })
                                     .Build();

            AggregateException aggr = await Assert.ThrowsExceptionAsync <AggregateException>(() => _forEach.Run(CancellationToken.None));

            Assert.AreEqual(testEx2, aggr.InnerExceptions.Single());

            Assert.IsTrue(_forEach.RunStats.Completed >= 40);
            Assert.IsTrue(_forEach.RunStats.Started <= 60);
        }
        public async Task Parallelization()
        {
            bool f1 = false;

            ForEach <int> _forEach = Builder
                                     .For(new int[] { 1, 2, 3 })
                                     .Each(async(T, token) =>
            {
                if (T == 1)
                {
                    await Task.Delay(200);
                    f1 = true;
                }

                if (T == 2)
                {
                    await Task.Delay(100);
                }

                if (T == 3)
                {
                    Assert.IsTrue(f1);
                }
            })
                                     .WithInitialDegreeOfParallelism(2)
                                     .Build();

            RunResult result = await _forEach.Run(CancellationToken.None);

            Assert.AreEqual(RunResult.Finished, result);
        }
        public async Task Forget()
        {
            ForEach <int> _forEach = Builder
                                     .For(Enumerable.Range(1, 100))
                                     .Each(async(T, token) =>
            {
                if (T == 50)
                {
                    throw new Exception();
                }

                try
                {
                    await Task.Delay(100, token);
                }
                catch (OperationCanceledException)
                {
                    Assert.Fail();
                }
            })
                                     .WithInitialDegreeOfParallelism(10)
                                     .WhenException((T, ex) => ExceptionResolution.Forget)
                                     .Build();

            RunResult result = await _forEach.Run(CancellationToken.None);

            Assert.AreEqual(RunResult.Interrupted, result);
            Assert.IsTrue(_forEach.RunStats.Started <= 60);
            Assert.IsTrue(_forEach.RunStats.Completed >= 40);
        }
예제 #4
0
        public void ForEach_Create_Monthly_Periods()
        {
            ForEach fe = ForEach.Month;

            List <Period> periods = fe.GetPeriods();

            Assert.AreEqual(12, periods.Count);

            Assert.AreEqual(1, periods[0].From.Day);
            Assert.AreEqual(31, periods[0].To.Day);
        }
        public async Task BasicCancellation()
        {
            int started = 0;

            ForEach <int> _forEach = Builder
                                     .For(new int[] { 1, 2, 3, 4, 5 })
                                     .Each(async(T, token) =>
            {
                Interlocked.Increment(ref started);

                try
                {
                    await Task.Delay(1000, token);
                }
                finally
                {
                    Assert.IsTrue(token.IsCancellationRequested);
                }

                Assert.Fail();
            })
                                     .WithInitialDegreeOfParallelism(4)
                                     .WhenException((_, ex) =>
            {
                Assert.Fail();
                return(ExceptionResolution.Abandon);
            })
                                     .Build();

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                cts.CancelAfter(500);

                try
                {
                    await _forEach.Run(cts.Token);

                    Assert.Fail();
                }
                catch (OperationCanceledException ex)
                {
                    Assert.AreEqual(ex.CancellationToken, cts.Token);
                }
            }

            Assert.AreEqual(_forEach.CurrentDegreeOfParallelism, started);
            await Task.Delay(1500);

            Assert.AreEqual(_forEach.CurrentDegreeOfParallelism, started);
        }
예제 #6
0
        public void ForEach_Create_Monthly_Periods_StartingFrom_10thOfMont()
        {
            ForEach fe = ForEach.Month.StartingFrom(The.Year(2012).On.January.The10th);

            List <Period> periods = fe.GetPeriods();

            Assert.AreEqual(12, periods.Count);
            Assert.AreEqual(1, periods[0].From.Month);
            Assert.AreEqual(2, periods[0].To.Month);

            Assert.AreEqual(10, periods[0].From.Day);
            Assert.AreEqual(9, periods[0].To.Day);

            Assert.AreEqual(10, periods[1].From.Day);
            Assert.AreEqual(9, periods[1].To.Day);
        }
        public async Task ParallelismVariance()
        {
            ForEach <int> _forEach = Builder
                                     .For(Enumerable.Range(1, 100))
                                     .Each((T, token) => Task.Delay(100))
                                     .WithInitialDegreeOfParallelism(1)
                                     .Build();

            var t = _forEach.Run(CancellationToken.None);

            var t1 = Task.Run(async() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    _forEach.IncreaseParallelism(1);
                    await Task.Delay(50);
                }
            });

            var t2 = Task.Run(async() =>
            {
                for (int i = 1; i <= 10; i++)
                {
                    _forEach.IncreaseParallelism(i);
                    await Task.Delay(50);
                }
            });

            var t3 = Task.Run(async() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    _forEach.DecreaseParallelism(1);
                    await Task.Delay(50);
                }
            });

            RunResult result = await t;

            Assert.AreEqual(RunResult.Finished, result);

            await Task.WhenAll(t1, t2, t3);

            Assert.AreEqual(56, _forEach.CurrentDegreeOfParallelism);
        }
예제 #8
0
        public void ForEach_Create_Daily_Recurrency()
        {
            ForEach fe = ForEach.Day.StartingFrom(The.Year(2012).On.January.The1st);

            List <Period> periods = fe.GetPeriods();

            Assert.AreEqual(1, periods[0].From.Day);
            Assert.AreEqual(1, periods[0].From.Month);
            Assert.AreEqual(2012, periods[0].From.Year);

            Assert.AreEqual(2, periods[1].From.Day);
            Assert.AreEqual(1, periods[1].From.Month);
            Assert.AreEqual(2012, periods[1].From.Year);

            Assert.AreEqual(31, periods.Last().From.Day);
            Assert.AreEqual(12, periods.Last().From.Month);
            Assert.AreEqual(2012, periods.Last().From.Year);
        }
        public async Task Basic()
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

            ForEach <int> _forEach = Builder
                                     .For(new int[] { 1, 2, 3 })
                                     .Each((T, token) => Task.Delay(T * 100, token).ContinueWith(_ => tcs.Task))
                                     .Build();

            Task <RunResult> t = _forEach.Run(CancellationToken.None);

            await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => _forEach.Run(CancellationToken.None));

            tcs.SetResult(true);

            RunResult result = await t;

            Assert.AreEqual(RunResult.Finished, result);
            Assert.IsTrue(_sw.ElapsedMilliseconds >= 600);
        }
        public async Task Swallow()
        {
            int swallowed = 0;

            ForEach <int> _forEach = Builder
                                     .For(new int[] { 1, 2, 3, 4, 5, 6 })
                                     .Each((T, token) => throw new Exception())
                                     .WhenException((T, ex) =>
            {
                Interlocked.Increment(ref swallowed);
                return(ExceptionResolution.Swallow);
            })
                                     .Build();


            await _forEach.Run(CancellationToken.None);

            Assert.AreEqual(6, swallowed);
            Assert.AreEqual(6, _forEach.RunStats.ExceptionsCaught);
            Assert.AreEqual(6, _forEach.RunStats.ExceptionsSwallowed);
        }
        public async Task SoftStop()
        {
            ForEach <int> _forEach = Builder
                                     .For(Enumerable.Range(1, 100))
                                     .Each((T, token) =>
            {
                if (T == 50)
                {
                    throw new Exception();
                }

                return(Task.Delay(100, token));
            })
                                     .WithInitialDegreeOfParallelism(10)
                                     .WhenException((T, ex) => ExceptionResolution.SoftStop)
                                     .Build();

            RunResult result = await _forEach.Run(CancellationToken.None);

            Assert.AreEqual(RunResult.Interrupted, result);
            Assert.IsTrue(_forEach.RunStats.Started <= 60);
            Assert.IsTrue(_forEach.RunStats.Completed >= 40);
        }