public void ReturnsTrueWhileUntilActionIsInvoked()
            {
                var timer = new TaskTimerInternal {
                    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 CancelsPreviousActionWhenStartIsCalledMultipleTimes()
            {
                var timer = new TaskTimerInternal {
                    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 InvokesActionAfterDelay()
            {
                var timer = new TaskTimerInternal {
                    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()
            {
                TaskTimerInternal timer = new TaskTimerInternal {
                    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());
                }
            }
Exemplo n.º 5
0
            public async Task AbortsPreviousAction()
            {
                var timer = new TaskTimerInternal {
                    Delay = TimeSpan.FromMilliseconds(1)
                };

                bool actionInvoked = false;

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

                await Task.Delay(TimeSpan.FromMilliseconds(20));

                Assert.IsFalse(actionInvoked);
            }
Exemplo n.º 6
0
            public void DoesNotLogErrorsIfCallbackReturnsNull()
            {
                using (TestEventListener listener = new TestEventListener())
                {
                    listener.EnableEvents(CoreEventSource.Log, EventLevel.Error);

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

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

                    Assert.IsTrue(actionInvoked.Wait(1000));
                    // Listener will wait for up to 5 seconds for incoming messages so no need to delay/sleep here.
                    Assert.IsNull(listener.Messages.FirstOrDefault());
                }
            }
            public void DoesNotLogErrorsIfCallbackReturnsNull()
            {
                using (TestEventListener listener = new TestEventListener())
                {
                    listener.EnableEvents(CoreEventSource.Log, EventLevel.Error);

                    var timer = new TaskTimerInternal {
                        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());
                }
            }