Пример #1
0
        public void WhenActionThrowsTaskCancledThenBreakTask()
        {
            var waitHandle = new ManualResetEvent(false);

            timeKeeper = new TimeKeeper(TimeSpan.Zero, () => timeKeeper.Cancel());
            timeKeeper.Start().Wait();
            Assert.True(timeKeeper.IsCompleted);
        }
Пример #2
0
        public void WhenActionThrowsThenBreakTask()
        {
            timeKeeper = new TimeKeeper(TimeSpan.Zero, () => throw new Exception("Some Exception!"));
            Action start = () => timeKeeper.Start().Wait();

            var aggregateException = Assert.Throws <AggregateException>(start);
            var exception          = Assert.IsType <Exception>(aggregateException.InnerException);

            Assert.Equal("Some Exception!", exception.Message);
        }
Пример #3
0
        public void WhenCancelThenStopTask()
        {
            var waitHandle = new ManualResetEvent(false);

            timeKeeper = new TimeKeeper(TimeSpan.Zero, () => waitHandle.Set());
            timeKeeper.Start();
            timeKeeper.Cancel();

            waitHandle.WaitOne(TimeSpan.FromSeconds(1));

            Assert.True(timeKeeper.IsCompleted);
        }
Пример #4
0
        public void WhenStartCalledAfterCancelThenThrowTaskCanceledException()
        {
            Mock <Action> someAction = new Mock <Action>();

            timeKeeper = new TimeKeeper(TimeSpan.Zero, someAction.Object);
            timeKeeper.Cancel();
            Action start = () => timeKeeper.Start().Wait();

            var aggregateException = Assert.Throws <AggregateException>(start);

            Assert.IsType <TaskCanceledException>(aggregateException.InnerException);
            someAction.Verify(a => a(), Times.Never);
        }
Пример #5
0
        public void WhenActionThrowsTaskCanceledExceptionThenDoNothing()
        {
            Mock <Action> someAction = new Mock <Action>();

            someAction.SetupSequence(a => a()).Throws(new TaskCanceledException("Some Exceptiom!")).Pass();

            timeKeeper = new TimeKeeper(TimeSpan.Zero, () =>
            {
                someAction.Object();
                timeKeeper.Cancel();
            });
            timeKeeper.Start().Wait();

            Assert.True(timeKeeper.IsCompleted);
            someAction.Verify(a => a(), Times.Exactly(2));
        }
Пример #6
0
        private Dictionary <DateTime, Dictionary <string, WorkItem> > ProcessWorkItemList(JSONQueryItem[] items)
        {
            var workItems = new Dictionary <DateTime, Dictionary <string, WorkItem> >();
            int finished  = 0;

            timeKeeper.IntervalAction = () => logger.LogToConsole($"Finished processing {finished}/{items.Length} Work Items. {workItems.Count} valid Work Items");
            timeKeeper.Start();

            Parallel.ForEach(items, item =>
            {
                ProcessWorkItem(workItems, item);
                finished++;
            });

            timeKeeper.Cancel();
            return(workItems);
        }
Пример #7
0
        public void WhenStartThenRunThread()
        {
            Mock <Action> someAction = new Mock <Action>();

            var    waitHandle = new ManualResetEvent(false);
            Action action     = () =>
            {
                someAction.Object();
                waitHandle.Set();
            };

            timeKeeper = new TimeKeeper(TimeSpan.Zero, action);
            timeKeeper.Start();

            waitHandle.WaitOne(TimeSpan.FromSeconds(1));
            timeKeeper.Cancel();

            someAction.Verify(a => a(), Times.AtLeastOnce);
        }