Пример #1
0
        public async Task TestTimer1sInterval()
        {
            const int interval = 1;
            var       sut      = new FixedStepTimer(TimeSpan.FromSeconds(interval));

            var tickResults     = new List <DateTime>();
            var firstSignalTime = DateTime.MinValue;

            sut.Tick += (s, e) =>
            {
                var signalTime = new DateTime(
                    1, 1, 1,
                    e.SignalTime.Hour,
                    e.SignalTime.Minute,
                    e.SignalTime.Second
                    );

                if (e.TickCount == 1)
                {
                    firstSignalTime = signalTime;
                }

                tickResults.Add(signalTime);
            };

            try
            {
                sut.Start(true);
                await Task.Delay(TimeSpan.FromSeconds(10));

                sut.Stop();
            }
            finally
            {
                sut.Close();
            }

            for (var i = 0; i < tickResults.Count; i++)
            {
                var want = firstSignalTime.AddSeconds(interval * i);
                var got  = tickResults[i];

                var w = want.ToString("HH:mm:ss.fff");
                var g = got.ToString("HH:mm:ss.fff");
                Trace.WriteLine($"want:[{w}]\tgot:[{g}]");

                Assert.AreEqual(want, got);
            }

            Assert.AreEqual(11, sut.TickCount);
        }
Пример #2
0
        public void TimerReset()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            FixedStepTimer timer = new FixedStepTimer(clock)
            {
                StepSize         = TimeSpan.FromMilliseconds(10),
                MaxNumberOfSteps = 5,
            };

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;
            timer.Reset();
            timer.Start();

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

            timer.Reset();
            Assert.IsFalse(timer.IsRunning);
            Assert.AreEqual(TimeSpan.Zero, timer.Time);
            Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);

            timer.Start();
            clock.Update(TimeSpan.FromMilliseconds(10));
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));

            timer.Stop();
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
            Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);

            timer.Reset();
            Assert.AreEqual(TimeSpan.Zero, timer.Time);
            Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);
        }
Пример #3
0
        public async Task TestTimer100msInterval()
        {
            const int interval = 100;
            var       sut      = new FixedStepTimer(TimeSpan.FromMilliseconds(interval));

            var tickResults     = new List <DateTime>();
            var firstSignalTime = DateTime.MinValue;

            sut.Tick += (s, e) =>
            {
                var signalTime = new DateTime(
                    1, 1, 1,
                    e.SignalTime.Hour,
                    e.SignalTime.Minute,
                    e.SignalTime.Second,
                    e.SignalTime.Millisecond
                    );

                if (e.TickCount == 1)
                {
                    firstSignalTime = signalTime;
                }

                tickResults.Add(signalTime);
            };

            try
            {
                sut.Start(true);
                await Task.Delay(TimeSpan.FromSeconds(1));

                sut.Stop();
            }
            finally
            {
                sut.Close();
            }

            const int tolerance = 10;

            for (var i = 0; i < tickResults.Count; i++)
            {
                var want = firstSignalTime.AddMilliseconds(interval * i);
                var got  = tickResults[i];

                var w    = want.ToString("HH:mm:ss.fff");
                var g    = got.ToString("HH:mm:ss.fff");
                var diff = got - want;
                Trace.WriteLine($"want:[{w}]\tgot:[{g}] {diff.Milliseconds}ms");

                var wantMillisecond = want.Millisecond;
                var gotMillisecond  = got.Millisecond;

                var lowerLimit = wantMillisecond;
                var upperLimit = wantMillisecond + tolerance;

                bool ok = lowerLimit <= gotMillisecond && gotMillisecond <= upperLimit;
                if (!ok)
                {
                    Assert.Fail($"want:[{w}]\tgot:[{g}]");
                }
            }

            Assert.AreEqual(11, sut.TickCount);
        }
Пример #4
0
        public void TimerReset()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = TimeSpan.FromMilliseconds(10),
            MaxNumberOfSteps = 5,
              };
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;
              timer.Reset();
              timer.Start();

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

              timer.Reset();
              Assert.IsFalse(timer.IsRunning);
              Assert.AreEqual(TimeSpan.Zero, timer.Time);
              Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);

              timer.Start();
              clock.Update(TimeSpan.FromMilliseconds(10));
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));

              timer.Stop();
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
              Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);

              timer.Reset();
              Assert.AreEqual(TimeSpan.Zero, timer.Time);
              Assert.AreEqual(TimeSpan.Zero, timer.DeltaTime);
        }
Пример #5
0
        public void NormalRun()
        {
            ManualClock clock = new ManualClock();
              clock.Start();

              FixedStepTimer timer = new FixedStepTimer(clock)
              {
            StepSize = TimeSpan.FromMilliseconds(10),
            MaxNumberOfSteps = 5,
              };
              timer.Idle += timer_Idle;
              timer.TimeChanged += timer_TimeChanged;

              // Clock is running. Timer is stopped.
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckNoTimeChangedEvent();

              // Start
              timer.Start();
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);

              // Pause
              timer.Stop();
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckNoTimeChangedEvent();
              Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);

              // Resume
              timer.Start();
              clock.Update(TimeSpan.FromMilliseconds(10));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              // Time step = 3 ∙ step size
              clock.Update(TimeSpan.FromMilliseconds(34));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(60), TimeSpan.FromMilliseconds(30));
              Assert.AreEqual(TimeSpan.FromMilliseconds(60), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.DeltaTime);
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              // Time step = 3 ∙ step size
              // (27 ms + 4 ms from previous step = 31 ms)
              clock.Update(TimeSpan.FromMilliseconds(27));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(90), TimeSpan.FromMilliseconds(30));
              Assert.AreEqual(TimeSpan.FromMilliseconds(90), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.DeltaTime);
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);

              // Time step = step size
              // (9 ms + 1 ms from previous step = 10 ms)
              clock.Update(TimeSpan.FromMilliseconds(9));
              CheckNoIdleEvent();
              CheckTimeChangedEvent(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(10));
              Assert.AreEqual(TimeSpan.FromMilliseconds(100), timer.Time);
              Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
              Assert.AreEqual(0, timer.AccumulatedSteps);
              Assert.AreEqual(0, timer.PendingSteps);
        }
Пример #6
0
        public void NormalRun()
        {
            ManualClock clock = new ManualClock();

            clock.Start();

            FixedStepTimer timer = new FixedStepTimer(clock)
            {
                StepSize         = TimeSpan.FromMilliseconds(10),
                MaxNumberOfSteps = 5,
            };

            timer.Idle        += timer_Idle;
            timer.TimeChanged += timer_TimeChanged;

            // Clock is running. Timer is stopped.
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckNoTimeChangedEvent();

            // Start
            timer.Start();
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));

            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);

            // Pause
            timer.Stop();
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckNoTimeChangedEvent();
            Assert.AreEqual(TimeSpan.FromMilliseconds(20), timer.Time);

            // Resume
            timer.Start();
            clock.Update(TimeSpan.FromMilliseconds(10));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            // Time step = 3 ∙ step size
            clock.Update(TimeSpan.FromMilliseconds(34));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(60), TimeSpan.FromMilliseconds(30));
            Assert.AreEqual(TimeSpan.FromMilliseconds(60), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.DeltaTime);
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            // Time step = 3 ∙ step size
            // (27 ms + 4 ms from previous step = 31 ms)
            clock.Update(TimeSpan.FromMilliseconds(27));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(90), TimeSpan.FromMilliseconds(30));
            Assert.AreEqual(TimeSpan.FromMilliseconds(90), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(30), timer.DeltaTime);
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);

            // Time step = step size
            // (9 ms + 1 ms from previous step = 10 ms)
            clock.Update(TimeSpan.FromMilliseconds(9));
            CheckNoIdleEvent();
            CheckTimeChangedEvent(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(TimeSpan.FromMilliseconds(100), timer.Time);
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), timer.DeltaTime);
            Assert.AreEqual(0, timer.AccumulatedSteps);
            Assert.AreEqual(0, timer.PendingSteps);
        }