public void CanBeChangedByConfigurableChannelComponents()
            {
                var timer = new TaskTimer();

                timer.Delay = TimeSpan.FromSeconds(42);
                Assert.Equal(42, timer.Delay.TotalSeconds);
            }
            public void CanBeSetToInfiniteToPreventTimerFromFiring()
            {
                var timer = new TaskTimer();

                timer.Delay = new TimeSpan(0, 0, 0, 0, -1);
                Assert.Equal(new TimeSpan(0, 0, 0, 0, -1), timer.Delay);
            }
            public void ReturnsTrueWhileUntilActionIsInvoked()
            {
                var timer = new TaskTimer {
                    Delay = TimeSpan.FromMilliseconds(1)
                };

                var actionStarted   = new ManualResetEventSlim();
                var actionCanFinish = new ManualResetEventSlim();

                timer.Start(
                    () => Task.Factory.StartNew(
                        () =>
                {
                    actionStarted.Set();
                    actionCanFinish.Wait();
                }));

                Assert.True(timer.IsStarted);

                actionStarted.Wait(50);

                Assert.False(timer.IsStarted);

                actionCanFinish.Set();
            }
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (this.pauseTimer != null)
         {
             this.pauseTimer.Dispose();
             this.pauseTimer = null;
         }
     }
 }
            public void InvokesActionAfterDelay()
            {
                var timer = new TaskTimer {
                    Delay = TimeSpan.FromMilliseconds(1)
                };

                var actionInvoked = new ManualResetEventSlim();

                timer.Start(() => Task.Factory.StartNew(actionInvoked.Set));

                Assert.False(actionInvoked.IsSet);
                Assert.True(actionInvoked.Wait(50));
            }
            public void HandlesSyncExceptionThrownByTheDelegate()
            {
                TaskTimer timer = new TaskTimer {
                    Delay = TimeSpan.FromMilliseconds(1)
                };

                using (TestEventListener listener = new TestEventListener())
                {
                    listener.EnableEvents(CoreEventSource.Log, EventLevel.LogAlways);
                    timer.Start(() => { throw new Exception(); });

                    Assert.NotNull(listener.Messages.FirstOrDefault());
                }
            }
            public void AbortsPreviousAction()
            {
                AsyncTest.Run(async() =>
                {
                    var timer = new TaskTimer {
                        Delay = TimeSpan.FromMilliseconds(1)
                    };

                    bool actionInvoked = false;
                    timer.Start(() => Task.Factory.StartNew(() => actionInvoked = true));
                    timer.Cancel();

                    await Task.Delay(20);

                    Assert.False(actionInvoked);
                });
            }
            public void DoesNotLogErrorsIfCallbackReturnsNull()
            {
                using (TestEventListener listener = new TestEventListener())
                {
                    listener.EnableEvents(CoreEventSource.Log, EventLevel.Error);

                    var timer = new TaskTimer {
                        Delay = TimeSpan.FromMilliseconds(1)
                    };
                    var actionInvoked = new ManualResetEventSlim();

                    timer.Start(() => { actionInvoked.Set(); return(null); });

                    Assert.True(actionInvoked.Wait(50));
                    Thread.Sleep(1000);

                    Assert.Null(listener.Messages.FirstOrDefault());
                }
            }
            public void CancelsPreviousActionWhenStartIsCalledMultipleTimes()
            {
                var timer = new TaskTimer {
                    Delay = TimeSpan.FromMilliseconds(1)
                };

                int invokationCount   = 0;
                var lastActionInvoked = new ManualResetEventSlim();

                timer.Start(() => Task.Factory.StartNew(() => Interlocked.Increment(ref invokationCount)));
                timer.Start(
                    () => Task.Factory.StartNew(
                        () =>
                {
                    Interlocked.Increment(ref invokationCount);
                    lastActionInvoked.Set();
                }));

                Assert.True(lastActionInvoked.Wait(50));
                Assert.Equal(1, invokationCount);
            }
            public void ReturnsTrueWhileUntilActionIsInvoked()
            {
                var timer = new TaskTimer { Delay = TimeSpan.FromMilliseconds(1) };

                var actionStarted = new ManualResetEventSlim();
                var actionCanFinish = new ManualResetEventSlim();
                timer.Start(
                    () => Task.Factory.StartNew(
                        () =>
                            {
                                actionStarted.Set();
                                actionCanFinish.Wait();
                            }));

                Assert.True(timer.IsStarted);

                actionStarted.Wait(50);

                Assert.False(timer.IsStarted);

                actionCanFinish.Set();
            }
            public void ReturnsFalseIfTimerWasNeverStarted()
            {
                var timer = new TaskTimer();

                Assert.False(timer.IsStarted);
            }
            public void ThrowsArgumentOutOfRangeExceptionWhenNewValueIsMoreThanMaxIntMilliseconds()
            {
                var timer = new TaskTimer();

                Assert.Throws <ArgumentOutOfRangeException>(() => timer.Delay = TimeSpan.FromMilliseconds((double)int.MaxValue + 1));
            }
            public void ThrowsArgumentOutOfRangeExceptionWhenNewValueIsZeroOrLess()
            {
                var timer = new TaskTimer();

                Assert.Throws <ArgumentOutOfRangeException>(() => timer.Delay = TimeSpan.Zero);
            }
            public void HandlesSyncExceptionThrownByTheDelegate()
            {
                TaskTimer timer = new TaskTimer { Delay = TimeSpan.FromMilliseconds(1) };

                using (TestEventListener listener = new TestEventListener())
                {
                    listener.EnableEvents(CoreEventSource.Log, EventLevel.LogAlways);
                    timer.Start(() => { throw new Exception(); });

                    Assert.NotNull(listener.Messages.FirstOrDefault());
                }
            }
     public void AbortsPreviousAction()
     {
         AsyncTest.Run(async () =>
         {
             var timer = new TaskTimer { Delay = TimeSpan.FromMilliseconds(1) };
 
             bool actionInvoked = false;
             timer.Start(() => Task.Factory.StartNew(() => actionInvoked = true));
             timer.Cancel();
 
             await TaskEx.Delay(20);
 
             Assert.False(actionInvoked);
         });
     }
            public void DefaultValueIsOneMinuteBecauseItHasToBeSomethingValid()
            {
                var timer = new TaskTimer();

                Assert.Equal(TimeSpan.FromMinutes(1), timer.Delay);
            }
 public void ThrowsArgumentOutOfRangeExceptionWhenNewValueIsMoreThanMaxIntMilliseconds()
 {
     var timer = new TaskTimer();
     Assert.Throws<ArgumentOutOfRangeException>(() => timer.Delay = TimeSpan.FromMilliseconds((double)int.MaxValue + 1));
 }
 public void CanBeChangedByConfigurableChannelComponents()
 {
     var timer = new TaskTimer();
     timer.Delay = TimeSpan.FromSeconds(42);
     Assert.Equal(42, timer.Delay.TotalSeconds);
 }
 public void CanBeSetToInfiniteToPreventTimerFromFiring()
 {
     var timer = new TaskTimer();
     timer.Delay = new TimeSpan(0, 0, 0, 0, -1);
     Assert.Equal(new TimeSpan(0, 0, 0, 0, -1), timer.Delay);
 }
            public void InvokesActionAfterDelay()
            {
                var timer = new TaskTimer { Delay = TimeSpan.FromMilliseconds(1) };

                var actionInvoked = new ManualResetEventSlim();
                timer.Start(() => Task.Factory.StartNew(actionInvoked.Set));

                Assert.False(actionInvoked.IsSet);
                Assert.True(actionInvoked.Wait(50));
            }
 public void ReturnsFalseIfTimerWasNeverStarted()
 {
     var timer = new TaskTimer();
     Assert.False(timer.IsStarted);
 }
            public void CancelsPreviousActionWhenStartIsCalledMultipleTimes()
            {
                var timer = new TaskTimer { Delay = TimeSpan.FromMilliseconds(1) };

                int invokationCount = 0;
                var lastActionInvoked = new ManualResetEventSlim();
                timer.Start(() => Task.Factory.StartNew(() => Interlocked.Increment(ref invokationCount)));
                timer.Start(
                    () => Task.Factory.StartNew(
                        () =>
                            {
                                Interlocked.Increment(ref invokationCount);
                                lastActionInvoked.Set();
                            }));

                Assert.True(lastActionInvoked.Wait(50));
                Assert.Equal(1, invokationCount);
            }
 public void DefaultValueIsOneMinuteBecauseItHasToBeSomethingValid()
 {
     var timer = new TaskTimer();
     Assert.Equal(TimeSpan.FromMinutes(1), timer.Delay);
 }
            public void DoesNotLogErrorsIfCallbackReturnsNull()
            {
                using (TestEventListener listener = new TestEventListener())
                {
                    listener.EnableEvents(CoreEventSource.Log, EventLevel.Error);

                    var timer = new TaskTimer {Delay = TimeSpan.FromMilliseconds(1)};
                    var actionInvoked = new ManualResetEventSlim();

                    timer.Start(() => { actionInvoked.Set(); return null; });

                    Assert.True(actionInvoked.Wait(50));
                    Thread.Sleep(1000);

                    Assert.Null(listener.Messages.FirstOrDefault());
                }
            }
 public void ThrowsArgumentOutOfRangeExceptionWhenNewValueIsZeroOrLess()
 {
     var timer = new TaskTimer();
     Assert.Throws<ArgumentOutOfRangeException>(() => timer.Delay = TimeSpan.Zero);
 }