Exemple #1
0
        public void Schedule_AutoStartDisabledRunOnceEnabled_StartsManuallyAndRunsEachRegistrationOnlyOnce()
        {
            var entity0 = new Entity {
                Id = "entity0"
            };
            var entity1 = new Entity {
                Id = "entity1"
            };
            Entity lastInvokedEntity = null;
            var    timerFactory      = new TestTimerFactory();

            using var scheduler = new EntityActionScheduler <Entity>(e =>
            {
                lastInvokedEntity = e;
                return(Task.CompletedTask);
            }, autoStart: false, runOnce: true, timerFactory);

            scheduler.ScheduleEntity(entity0, TimeSpan.FromMilliseconds(Period0));
            scheduler.ScheduleEntity(entity1, TimeSpan.FromMilliseconds(Period1));
            Assert.Equal(2, timerFactory.Count);
            timerFactory.VerifyTimer(0, Timeout.Infinite);
            timerFactory.VerifyTimer(1, Timeout.Infinite);

            scheduler.Start();

            VerifyEntities(scheduler, entity0, entity1);
            Assert.Equal(2, timerFactory.Count);
            timerFactory.VerifyTimer(0, Period0);
            timerFactory.VerifyTimer(1, Period1);

            timerFactory.FireTimer(1);

            Assert.Same(entity1, lastInvokedEntity);

            VerifyEntities(scheduler, entity0);

            timerFactory.FireTimer(0);

            Assert.Same(entity0, lastInvokedEntity);

            Assert.False(scheduler.IsScheduled(entity0));
            Assert.False(scheduler.IsScheduled(entity1));
            timerFactory.AssertTimerDisposed(0);
            timerFactory.AssertTimerDisposed(1);
        }
Exemple #2
0
        private void VerifyEntities(EntityActionScheduler <Entity> scheduler, params Entity[] entities)
        {
            var actualCount = 0;

            foreach (var entity in entities)
            {
                Assert.True(scheduler.IsScheduled(entity));
                actualCount++;
            }
            Assert.Equal(entities.Length, actualCount);
        }