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)); } }
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)); } }
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)); } }
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)); }
/* ----------------------------------------------------------------- */ /// /// 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)); }
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)); }
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)); }
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)); } }