Esempio n. 1
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));
            }
        }
Esempio n. 2
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));
            }
        }
Esempio n. 3
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));
                }
        }
Esempio n. 4
0
 public void Properties()
 {
     using (var src = new WakeableTimer())
     {
         Assert.That(src.State, Is.EqualTo(TimerState.Stop));
         Assert.That(src.Interval, Is.EqualTo(TimeSpan.FromSeconds(1)));
         Assert.That(src.Last.HasValue, Is.False);
     }
 }
Esempio n. 5
0
 public void Timer_Properties()
 {
     using (var timer = new WakeableTimer())
     {
         Assert.That(timer.Interval, Is.EqualTo(TimeSpan.FromSeconds(1)));
         Assert.That(timer.LastPublished, Is.EqualTo(DateTime.MinValue));
         Assert.That(timer.State, Is.EqualTo(TimerState.Stop));
     }
 }
Esempio n. 6
0
 public void Start_InitialDelay()
 {
     using (var src = new WakeableTimer())
     {
         src.Interval = TimeSpan.FromHours(1);
         Assert.That(src.Last.HasValue, Is.False);
         Assert.That(Execute(src, 200, 1), "Timeout");
         Assert.That(src.Last, Is.Not.EqualTo(DateTime.MinValue));
     }
 }
Esempio n. 7
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));
        }
Esempio n. 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));
        }
Esempio n. 9
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="count">
        /// Number of callbacks that the timer waits.
        /// </param>
        ///
        /// <returns>true for success.</returns>
        ///
        /* ----------------------------------------------------------------- */
        private bool Execute(WakeableTimer src, int msec, int count)
        {
            var n   = 0;
            var cts = new CancellationTokenSource();

            using (src.Subscribe(Synchronous.AsTask(() =>
            {
                if (++n >= count)
                {
                    src.Stop();
                    cts.Cancel();
                }
            }))) return(Execute(src, msec, cts));
        }
Esempio n. 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));
        }
Esempio n. 11
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));
        }
Esempio n. 12
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));
        }
Esempio n. 13
0
        public void Start()
        {
            using (var src = new WakeableTimer())
            {
                src.Interval = TimeSpan.FromMilliseconds(100);
                src.Interval = TimeSpan.FromMilliseconds(100); // ignore
                Assert.That(src.Last.HasValue, Is.False);
                Assert.That(Execute(src, 0, 1), "Timeout");

                var time = src.Last;
                Assert.That(time, Is.Not.EqualTo(DateTime.MinValue));

                src.Reset();
                Assert.That(src.Last, Is.EqualTo(time));
                Assert.That(src.Interval.TotalMilliseconds, Is.EqualTo(100).Within(1.0));
            }
        }
Esempio n. 14
0
        public void Start_Burstly()
        {
            var cts   = new CancellationTokenSource();
            var count = 0;

            using (var src = new WakeableTimer())
            {
                src.Interval = TimeSpan.FromMilliseconds(10);
                _            = src.Subscribe(async() =>
                {
                    ++count;
                    await Task.Delay(200).ConfigureAwait(false);
                    src.Stop();
                    cts.Cancel();
                });

                Assert.That(Execute(src, 0, cts), "Timeout");
                Assert.That(count, Is.EqualTo(1));
            }
        }
Esempio n. 15
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));
            }
        }
Esempio n. 16
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));
     }
 }
Esempio n. 17
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));
     }
 }
Esempio n. 18
0
 /* ----------------------------------------------------------------- */
 ///
 /// Create
 ///
 /// <summary>
 /// Creates a new instance of the WakeableTimer and executes
 /// the specified action.
 /// </summary>
 ///
 /* ----------------------------------------------------------------- */
 private void Create(Action <WakeableTimer> action)
 {
     using (var src = new WakeableTimer()) action(src);
 }