public void WaitForAllWork_should_return_immediately_if_no_work_queued() { var stopWatch = new Stopwatch(); "Given no work going on".Context(() => { Assert.False(ParallelWork.IsWorkOrTimerQueued()); }); var result = default(bool); "When WaitForAllWork is called".Do(() => { stopWatch.Start(); result = ParallelWork.WaitForAllWork(TimeSpan.FromSeconds(1)); }); "It should return immediately without going into any wait period".Assert(() => { Assert.True(stopWatch.Elapsed < TimeSpan.FromSeconds(1)); }); "It should return true".Assert(() => { Assert.True(result); }); }
//[Specification] //[STAThread] //public void IsAnyWorkRunning_should_return_true_only_if_some_thread_running() //{ // DateTime workStartedAt = default(DateTime); // TimeSpan waitDuration = TimeSpan.FromSeconds(2); // TimeSpan howLongWorkTakes = TimeSpan.FromSeconds(1); // TimeSpan timeout = waitDuration.Add(howLongWorkTakes.Add(TimeSpan.FromMilliseconds(500))); // DispatcherFrame frame = default(DispatcherFrame); // "Given a timed work queued that hasn't started yet".Context(() => // { // Assert.False(ParallelWork.IsWorkOrTimerQueued()); // frame = new DispatcherFrame(); // workStartedAt = default(DateTime); // ParallelWork.DoWorkAfter(() => // { // workStartedAt = DateTime.Now; // Thread.Sleep(howLongWorkTakes); // }, // () => // { // frame.Continue = false; // }, waitDuration); // }); // var result = default(bool); // "When IsAnyWorkRunning is called".Do(() => // { // result = ParallelWork.IsAnyWorkRunning(); // }); // "It should return false if the work hasn't started yet".Assert(() => // { // Assert.False(result); // Assert.True(WaitForWorkDoneAndFireCallback(timeout, frame)); // }); // "It should return true when a work is still going on".Assert(() => // { // Dispatcher.PushFrame(frame); // Thread.Sleep(waitDuration); // Thread.Sleep(howLongWorkTakes.Milliseconds / 2); // Assert.NotEqual(default(DateTime), workStartedAt); // Assert.True(result); // Assert.True(WaitForWorkDoneAndFireCallback(timeout, frame)); // }); //} private bool WaitForWorkDoneAndFireCallback(TimeSpan timeout, DispatcherFrame frame) { if (ParallelWork.WaitForAllWork(timeout)) { // Let the Disptacher.BeginInvoke calls proceed Dispatcher.PushFrame(frame); return(true); } else { // waiting timed out. Work did not finish on time. return(false); } }