Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        public async Task Scheduled_Tasks_Runs_Exactly_In_Time()
        {
            var task1 = GetFakeTimeInvoker();
            var task2 = GetFakeTimeInvoker();

            var sampleInSeconds = 5;
            var firstDelay      = 2;
            var secondDelay     = 3;

            var            sampling = TimeSpan.FromSeconds(sampleInSeconds);
            DateTimeOffset startTime;
            var            testScheduler = new TestScheduler();

            using (var scheduler = new ActionScheduler(sampling, testScheduler))
            {
                scheduler.Schedule(TimeSpan.FromSeconds(firstDelay), () => task1.Object.Invoke(testScheduler.Now));
                scheduler.Schedule(TimeSpan.FromSeconds(secondDelay), () => task2.Object.Invoke(testScheduler.Now));

                startTime = testScheduler.Now;
                await scheduler.StartAsync();

                testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks);
            }

            var correction = startTime.AddSeconds(-(startTime.Second % sampleInSeconds));

            var firstTime = correction.AddSeconds(firstDelay);

            if (startTime > firstTime)
            {
                firstTime = firstTime.Add(sampling);
            }

            var secondTime = correction.AddSeconds(secondDelay);

            if (startTime > secondTime)
            {
                secondTime = secondTime.Add(sampling);
            }

            task1.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, firstTime))), Times.Exactly(1));
            task1.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, firstTime.Add(sampling)))), Times.Exactly(1));

            task2.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, secondTime))), Times.Exactly(1));
            task2.Verify(
                x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, secondTime.Add(sampling)))),
                Times.Exactly(1));
        }
Ejemplo n.º 3
0
        public async Task Can_Schedule_Task(TimeSpan startTime, bool inclusive, TimeSpan sampling, TimeSpan delay, TimeSpan workDuration, int occurs)
        {
            var invoker       = GetFakeInvoker();
            var testScheduler = new TestScheduler();

            using (var scheduler = new ActionScheduler(sampling, testScheduler))
            {
                scheduler.Schedule(delay, () => invoker.Object.Invoke(), inclusive);

                testScheduler.AdvanceBy(startTime.Ticks);
                await scheduler.StartAsync();

                testScheduler.AdvanceBy(workDuration.Ticks - startTime.Ticks);
            }

            invoker.Verify(x => x.Invoke(), Times.Exactly(occurs));
        }
Ejemplo n.º 4
0
        public async Task Can_Schedule_Multiple_Tasks()
        {
            var task1         = GetFakeInvoker();
            var task2         = GetFakeInvoker();
            var testScheduler = new TestScheduler();

            using (var scheduler = new ActionScheduler(TimeSpan.FromSeconds(5), testScheduler))
            {
                scheduler.Schedule(TimeSpan.FromSeconds(1), () => task1.Object.Invoke());
                scheduler.Schedule(TimeSpan.FromSeconds(2), () => task2.Object.Invoke());

                await scheduler.StartAsync();

                testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks);
            }

            task1.Verify(x => x.Invoke(), Times.Exactly(2));
            task2.Verify(x => x.Invoke(), Times.Exactly(2));
        }