Пример #1
0
        public void Resume()
        {
            var cts   = new CancellationTokenSource();
            var count = 0;

            using (var src = new WakeableTimer())
            {
                src.Interval = TimeSpan.FromSeconds(1);
                src.Start(TimeSpan.FromMilliseconds(100));
                _ = src.Subscribe(Synchronous.AsTask(() =>
                {
                    ++count;
                    src.Stop();
                    cts.Cancel();
                }));
                src.Start();
                src.Suspend();

                Assert.That(count, Is.EqualTo(0));
                Task.Delay(300).Wait();
                Assert.That(count, Is.EqualTo(0));
                Assert.That(Execute(src, 0, cts), "Timeout");
                Assert.That(count, Is.EqualTo(1));
            }
        }
Пример #2
0
        /* ----------------------------------------------------------------- */
        ///
        /// Execute
        ///
        /// <summary>
        /// Waits for the timer to execute the specified number of callbacks.
        /// </summary>
        ///
        /// <param name="src">Timer object.</param>
        /// <param name="msec">Initial delay.</param>
        /// <param name="cts">Cancellation token.</param>
        ///
        /// <returns>true for success.</returns>
        ///
        /* ----------------------------------------------------------------- */
        private bool Execute(WakeableTimer src, int msec, CancellationTokenSource cts)
        {
            Task.Run(() =>
            {
                if (msec <= 0)
                {
                    src.Start();
                }
                else
                {
                    src.Start(TimeSpan.FromMilliseconds(msec));
                }
            }).Forget();

            return(Wait(cts));
        }
Пример #3
0
        public void PowerMode_Scenario()
        {
            using (var timer = new WakeableTimer())
            {
                var count   = 0;
                var chagned = 0;
                var power   = new PowerModeContext(Power.Mode);

                Power.Configure(power);

                timer.Interval          = TimeSpan.FromMilliseconds(200);
                timer.PowerModeChanged += (s, e) => ++ chagned;
                timer.Subscribe(() => ++ count);
                timer.Start();

                Task.Delay(50).Wait();
                power.Mode = PowerModes.Suspend;
                Task.Delay(100).Wait();
                power.Mode = PowerModes.Resume;
                Task.Delay(150).Wait();
                timer.Stop();

                Assert.That(Power.Mode, Is.EqualTo(PowerModes.Resume));
                Assert.That(chagned, Is.EqualTo(2), nameof(timer.PowerModeChanged));
            }
        }
Пример #4
0
        public void Transition_PowerMode()
        {
            var pmc   = new PowerModeContext(Power.Mode);
            var count = 0;
            var dummy = 0;

            Power.Configure(pmc);

            using (var src = new WakeableTimer())
                using (src.Subscribe(Synchronous.AsTask(() => ++ dummy)))
                {
                    src.PowerModeChanged += (s, e) => ++ count;
                    src.Start();

                    pmc.Mode = PowerModes.Suspend;
                    Assert.That(Power.Mode, Is.EqualTo(PowerModes.Suspend));
                    Assert.That(src.State, Is.EqualTo(TimerState.Suspend));

                    pmc.Mode = PowerModes.Resume;
                    Assert.That(Power.Mode, Is.EqualTo(PowerModes.Resume));
                    Assert.That(src.State, Is.EqualTo(TimerState.Run));

                    src.Stop();
                    Assert.That(Power.Mode, Is.EqualTo(PowerModes.Resume));
                    Assert.That(src.State, Is.EqualTo(TimerState.Stop));
                }
        }
Пример #5
0
        public void Start_Immediately()
        {
            var count = 0;

            using (var timer = new WakeableTimer())
            {
                var disposable = timer.Subscribe(() => ++ count);
                timer.Start(TimeSpan.Zero);
                Task.Delay(50).Wait();
                timer.Stop();
                disposable.Dispose();
                timer.Start(TimeSpan.Zero);
                Task.Delay(50).Wait();
                timer.Stop();
            }
            Assert.That(count, Is.EqualTo(1));
        }
Пример #6
0
        public void Properties_Disposed()
        {
            var src = new WakeableTimer();

            src.Start();
            Task.Delay(100).Wait();
            Assert.That(src.State, Is.EqualTo(TimerState.Run));
            src.Dispose();
            Assert.That(src.State, Is.EqualTo(TimerState.Stop));
        }
Пример #7
0
 public void Transition_State()
 {
     using (var src = new WakeableTimer())
     {
         Assert.That(src.State, Is.EqualTo(TimerState.Stop));
         src.Start();
         Assert.That(src.State, Is.EqualTo(TimerState.Run));
         src.Start(); // ignore
         Assert.That(src.State, Is.EqualTo(TimerState.Run));
         src.Suspend();
         Assert.That(src.State, Is.EqualTo(TimerState.Suspend));
         src.Suspend();
         Assert.That(src.State, Is.EqualTo(TimerState.Suspend));
         src.Start();
         Assert.That(src.State, Is.EqualTo(TimerState.Run));
         src.Stop();
         Assert.That(src.State, Is.EqualTo(TimerState.Stop));
         src.Stop(); // ignore
         Assert.That(src.State, Is.EqualTo(TimerState.Stop));
     }
 }
Пример #8
0
        public void Start_InitialDelay()
        {
            var count = 0;

            using (var timer = new WakeableTimer())
            {
                timer.Subscribe(() => ++ count);
                timer.Start(TimeSpan.FromSeconds(1));
                timer.Stop();
            }
            Assert.That(count, Is.EqualTo(0));
        }
Пример #9
0
 public void State_Scenario()
 {
     using (var timer = new WakeableTimer())
     {
         Assert.That(timer.State, Is.EqualTo(TimerState.Stop));
         timer.Start();
         Assert.That(timer.State, Is.EqualTo(TimerState.Run));
         timer.Start();
         Assert.That(timer.State, Is.EqualTo(TimerState.Run));
         timer.Suspend();
         Assert.That(timer.State, Is.EqualTo(TimerState.Suspend));
         timer.Suspend();
         Assert.That(timer.State, Is.EqualTo(TimerState.Suspend));
         timer.Start();
         Assert.That(timer.State, Is.EqualTo(TimerState.Run));
         timer.Stop();
         Assert.That(timer.State, Is.EqualTo(TimerState.Stop));
         timer.Stop();
         Assert.That(timer.State, Is.EqualTo(TimerState.Stop));
     }
 }
Пример #10
0
        public void Start()
        {
            var count = 0;

            using (var timer = new WakeableTimer())
            {
                timer.Subscribe(() => ++ count);
                timer.Interval = TimeSpan.FromMilliseconds(100);
                timer.Start();
                Task.Delay(300).Wait();
                timer.Stop();
            }
            Assert.That(count, Is.GreaterThanOrEqualTo(3));
        }
Пример #11
0
        public void Reset()
        {
            using (var timer = new WakeableTimer())
            {
                var ms = 100;

                timer.Interval = TimeSpan.FromMilliseconds(ms);
                timer.Interval = TimeSpan.FromMilliseconds(ms); // ignore
                timer.Start();
                Task.Delay(ms * 2).Wait();
                timer.Stop();

                var last = timer.LastPublished;
                Assert.That(last, Is.Not.EqualTo(DateTime.MinValue));

                timer.Reset();
                Assert.That(timer.LastPublished, Is.EqualTo(last));
                Assert.That(timer.Interval.TotalMilliseconds, Is.EqualTo(ms).Within(1.0));
            }
        }