예제 #1
0
        public void ActionScheduler_ToleratesUnlimitedFailures()
        {
            using (var scheduler = new ActionScheduler(-1))
            {
                var actionCounter = 0;
                var tcs           = new TaskCompletionSource <bool>();

                scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
                {
                    if (actionCounter < 10)
                    {
                        actionCounter++;
                        throw new Exception();
                    }
                    else
                    {
                        actionCounter++;
                        tcs.SetResult(true);
                    }
                });

                tcs.Task.Wait();
                scheduler.Stop();

                actionCounter.Should().Be(11);
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: elukash/RealTime
        private static async Task MainAsync()
        {
            var tenSeconds = TimeSpan.FromSeconds(60);
            var scheduler  = new ActionScheduler(tenSeconds);

            scheduler.Schedule(
                TimeSpan.FromSeconds(1),
                () => WriteLine("First second"));

            scheduler.Schedule(
                TimeSpan.FromSeconds(5),
                () => WriteLine("Fifth second"));

            // this task run with delay but will be executed in correct time in the next iteration
            scheduler.Schedule(
                TimeSpan.FromSeconds(5),
                async() =>
            {
                WriteLine("Fifth second with delay");
                await Task.Delay(TimeSpan.FromSeconds(1));
            });

            await scheduler.StartAsync();

            Console.ReadKey();
            scheduler.Stop();
        }
예제 #3
0
        private static void Test_NoTolerance(ActionScheduler scheduler)
        {
            var actionCounter = 0;

            scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
            {
                actionCounter++;
                throw new Exception();
            });

            Thread.Sleep(200);
            scheduler.Stop();

            actionCounter.Should().Be(1);
        }
예제 #4
0
        public void ActionScheduler_ExecutesScheduledActionWithToken()
        {
            using (ActionScheduler scheduler = new ActionScheduler())
            {
                int data = 0;
                var tcs  = new TaskCompletionSource <bool>();

                scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
                {
                    data++;
                    tcs.SetResult(true);
                });

                tcs.Task.Wait();
                scheduler.Stop();
                data.Should().Be(1);
            }
        }
예제 #5
0
        public void ActionScheduler_ExecutesScheduledFunction()
        {
            using (ActionScheduler scheduler = new ActionScheduler())
            {
                var tcs  = new TaskCompletionSource <bool>();
                int data = 0;

                Func <CancellationToken, Task> function = (t) => Task.Factory.StartNew(() =>
                {
                    data++;
                    tcs.SetResult(true);
                });

                scheduler.Start(TimeSpan.FromMilliseconds(10), function);
                tcs.Task.Wait();
                scheduler.Stop();

                data.Should().Be(1);
            }
        }
예제 #6
0
        public void ActionScheduler_ReportsErrorInCaseOfToleratedFailures()
        {
            using (var scheduler = new ActionScheduler(3))
            {
                var actionCounter = 0;
                var tcs           = new TaskCompletionSource <bool>();
                var errorCounter  = 0;

                Metric.Config.WithErrorHandler(e =>
                {
                    if (e.Message == "reports_error")
                    {
                        errorCounter++;
                    }
                });

                scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
                {
                    if (actionCounter < 3)
                    {
                        actionCounter++;
                        throw new Exception("reports_error");
                    }
                    else
                    {
                        actionCounter++;
                        tcs.SetResult(true);
                    }
                });

                tcs.Task.Wait();
                scheduler.Stop();

                actionCounter.Should().Be(4);
                errorCounter.Should().Be(3);
            }
        }
예제 #7
0
        public void ActionScheduler_ReportsExceptionWithGlobalMetricHandler()
        {
            Exception x   = null;
            var       tcs = new TaskCompletionSource <bool>();

            Metric.Config.WithErrorHandler(e =>
            {
                x = e;
                tcs.SetResult(true);
            });

            using (ActionScheduler scheduler = new ActionScheduler())
            {
                scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
                {
                    throw new InvalidOperationException("boom");
                });

                tcs.Task.Wait(1000);
                scheduler.Stop();
            }

            x.Should().NotBeNull();
        }