public void ScheduleTimerShouldOnlyCallDoOnceEvenWithMultipleBeginRequests()
        {
            int count = 0;

            var sut = new ScheduledTimer()
                      .Do(() => Interlocked.Increment(ref count))
                      .StartingAt(DateTime.Now)
                      .Every(TimeSpan.FromMilliseconds(1000));

            Task.Run(() => sut.Begin());
            Task.Run(() => sut.Begin());
            Task.Run(() => sut.Begin());

            Thread.Sleep(200);
            Assert.That(count, Is.EqualTo(1));
        }
        public void IntervalShouldBeSetTo1WhenStartWithNoParameterIsCalled()
        {
            var sut = new ScheduledTimer();

            sut.Begin();

            Assert.That(sut.TimerObject.Interval, Is.EqualTo(1));
        }
        public void StartWithNoParameterShouldUpdateTheStatusToRunning()
        {
            const ScheduledTimerStatus expected = ScheduledTimerStatus.Running;

            var sut = new ScheduledTimer();

            sut.Begin();

            Assert.That(sut.Status, Is.EqualTo(expected));
        }
        public void StartThenStopShouldUpdateStatusToStopped()
        {
            const ScheduledTimerStatus expectedRunning = ScheduledTimerStatus.Running;
            const ScheduledTimerStatus expectedStopped = ScheduledTimerStatus.Stopped;

            var sut = new ScheduledTimer();

            sut.Begin();

            Assert.That(sut.Status, Is.EqualTo(expectedRunning));

            sut.End();

            Assert.That(sut.Status, Is.EqualTo(expectedStopped));
        }
        public void SetReplicationIntervalShouldUpdateTheTimerIntervalAndAutoResetAccordingly()
        {
            var sut = new ScheduledTimer();

            var counter = 0;

            sut.Do(() => Interlocked.Increment(ref counter)).Every(TimeSpan.FromMilliseconds(100));

            Assert.That(sut.TimerObject.Interval, Is.EqualTo(100));

            sut.Begin();

            Thread.Sleep(550);

            Assert.That(counter, Is.GreaterThanOrEqualTo(5));
        }
        public void DisposeWithTimerRunningShouldStopTimerAndDisposeInternalTimer()
        {
            const ScheduledTimerStatus expected = ScheduledTimerStatus.Stopped;

            var sut = new ScheduledTimer();

            var disposed = false;

            sut.TimerObject.Disposed += ((sender, args) => disposed = true);

            sut.Begin();

            sut.Dispose();

            Assert.That(sut.Status, Is.EqualTo(expected));
            Assert.That(disposed, Is.True);
        }
        public void IntervalUpdatedBeforeStartShouldChangeTheIntervalUntilStartHasBeenCalled()
        {
            var sut = new ScheduledTimer();

            sut.Every(TimeSpan.FromMilliseconds(100));

            Assert.That(sut.TimerObject.Interval, Is.EqualTo(100));

            sut.StartingAt(DateTime.Now.AddMilliseconds(200));

            // Becuase of the nature of time, give the interval a second of leeway
            Assert.That(sut.TimerObject.Interval, Is.InRange(200d, 300d));

            sut.Begin();

            Thread.Sleep(300);

            Assert.That(sut.TimerObject.Interval, Is.EqualTo(100));
        }