public void ChangePeriod_PeriodIncreasedTimerNotStarted_PeriodChangedBeforeFirstCall() { var invoked = new AutoResetEvent(false); var entity = new Entity { Id = "entity0" }; var period = TimeSpan.FromMilliseconds(250); var timeout = TimeSpan.FromSeconds(2); var clock = new UptimeClock(); Entity lastInvokedEntity = null; using var scheduler = new EntityActionScheduler <Entity>(e => { lastInvokedEntity = e; invoked.Set(); return(Task.CompletedTask); }, autoStart: false, runOnce: false, new TimerFactory()); scheduler.ScheduleEntity(entity, period); var newPeriod = TimeSpan.FromMilliseconds(500); scheduler.ChangePeriod(entity, newPeriod); scheduler.Start(); var before = clock.TickCount; Assert.True(invoked.WaitOne(timeout)); var elapsed = TimeSpan.FromMilliseconds(clock.TickCount - before); Assert.True(elapsed >= newPeriod); Assert.Same(entity, lastInvokedEntity); }
public void ChangePeriod_TimerStartedPeriodChangedAfterFirstCall_PeriodChangedBeforeNextCall() { var entity = new Entity { Id = "entity0" }; Entity lastInvokedEntity = null; var timerFactory = new TestTimerFactory(); using var scheduler = new EntityActionScheduler <Entity>(e => { lastInvokedEntity = e; return(Task.CompletedTask); }, autoStart: true, runOnce: false, timerFactory); scheduler.ScheduleEntity(entity, TimeSpan.FromMilliseconds(Period0)); timerFactory.VerifyTimer(0, Period0); timerFactory.FireTimer(0); var newPeriod = TimeSpan.FromMilliseconds(Period1); scheduler.ChangePeriod(entity, newPeriod); timerFactory.VerifyTimer(0, Period1); Assert.Same(entity, lastInvokedEntity); }
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); }
public void Schedule_AutoStartEnabledRunOnceDisabled_StartsAutomaticallyAndRunsIndefinitely() { var entity0 = new Entity { Id = "entity0" }; var entity1 = new Entity { Id = "entity1" }; var timerFactory = new TestTimerFactory(); Entity lastInvokedEntity = null; using var scheduler = new EntityActionScheduler <Entity>(e => { lastInvokedEntity = e; return(Task.CompletedTask); }, autoStart: true, runOnce: false, timerFactory); scheduler.ScheduleEntity(entity0, TimeSpan.FromMilliseconds(20000)); scheduler.ScheduleEntity(entity1, TimeSpan.FromMilliseconds(10000)); VerifyEntities(scheduler, entity0, entity1); Assert.Equal(2, timerFactory.Count); timerFactory.VerifyTimer(0, Period0); timerFactory.VerifyTimer(1, Period1); timerFactory.FireTimer(1); Assert.Same(entity1, lastInvokedEntity); timerFactory.FireTimer(0); Assert.Same(entity0, lastInvokedEntity); timerFactory.FireTimer(1); Assert.Same(entity1, lastInvokedEntity); timerFactory.FireTimer(0); Assert.Same(entity0, lastInvokedEntity); VerifyEntities(scheduler, entity0, entity1); Assert.Equal(2, timerFactory.Count); timerFactory.VerifyTimer(0, Period0); timerFactory.VerifyTimer(1, Period1); }
public void Schedule_AutoStartDisabledRunOnceEnabled_StartsManuallyAndRunsEachRegistrationOnlyOnce() { var invoked = new AutoResetEvent(false); var entity0 = new Entity { Id = "entity0" }; var period0 = TimeSpan.FromMilliseconds(1100); var entity1 = new Entity { Id = "entity1" }; var period1 = TimeSpan.FromMilliseconds(700); var timeout = TimeSpan.FromSeconds(2); Entity lastInvokedEntity = null; using var scheduler = new EntityActionScheduler <Entity>(e => { lastInvokedEntity = e; invoked.Set(); return(Task.CompletedTask); }, autoStart: false, runOnce: true, new TimerFactory()); scheduler.ScheduleEntity(entity0, period0); scheduler.ScheduleEntity(entity1, period1); Assert.False(invoked.WaitOne(timeout)); scheduler.Start(); VerifyEntities(scheduler, entity0, entity1); Assert.True(invoked.WaitOne(timeout)); Assert.Same(entity1, lastInvokedEntity); VerifyEntities(scheduler, entity0); Assert.True(invoked.WaitOne(timeout)); Assert.Same(entity0, lastInvokedEntity); Assert.False(scheduler.IsScheduled(entity0)); Assert.False(scheduler.IsScheduled(entity1)); }
public void Unschedule_EntityUnscheduledAfterFirstCall_CallbackInvokedOnlyOnce() { var invoked = new AutoResetEvent(false); var entity0 = new Entity { Id = "entity0" }; var period0 = TimeSpan.FromMilliseconds(1100); var entity1 = new Entity { Id = "entity1" }; var period1 = TimeSpan.FromMilliseconds(700); var timeout = TimeSpan.FromSeconds(2); Entity lastInvokedEntity = null; using var scheduler = new EntityActionScheduler <Entity>(e => { lastInvokedEntity = e; invoked.Set(); return(Task.CompletedTask); }, autoStart: true, runOnce: false, new TimerFactory()); scheduler.ScheduleEntity(entity0, period0); scheduler.ScheduleEntity(entity1, period1); VerifyEntities(scheduler, entity0, entity1); Assert.True(invoked.WaitOne(timeout)); Assert.Same(entity1, lastInvokedEntity); Assert.True(invoked.WaitOne(timeout)); Assert.Same(entity0, lastInvokedEntity); scheduler.UnscheduleEntity(entity1); VerifyEntities(scheduler, entity0); Assert.True(invoked.WaitOne(timeout)); Assert.Same(entity0, lastInvokedEntity); VerifyEntities(scheduler, entity0); }
public void Unschedule_EntityUnscheduledAfterFirstCall_CallbackInvokedOnlyOnce() { 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: true, runOnce: false, timerFactory); scheduler.ScheduleEntity(entity0, TimeSpan.FromMilliseconds(Period0)); scheduler.ScheduleEntity(entity1, TimeSpan.FromMilliseconds(Period1)); VerifyEntities(scheduler, entity0, entity1); timerFactory.FireTimer(1); Assert.Same(entity1, lastInvokedEntity); timerFactory.FireTimer(0); Assert.Same(entity0, lastInvokedEntity); scheduler.UnscheduleEntity(entity1); VerifyEntities(scheduler, entity0); timerFactory.AssertTimerDisposed(1); timerFactory.FireTimer(0); Assert.Same(entity0, lastInvokedEntity); VerifyEntities(scheduler, entity0); }