public void WhenActionThrowsTaskCancledThenBreakTask() { var waitHandle = new ManualResetEvent(false); timeKeeper = new TimeKeeper(TimeSpan.Zero, () => timeKeeper.Cancel()); timeKeeper.Start().Wait(); Assert.True(timeKeeper.IsCompleted); }
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); }
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); }
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); }
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); }