public void TestChainlengthDictionaryUpdated() { long count = 0; int eventsChangedFired = 0; DateTime previousTime = DateTime.Now; DateTime startTime = DateTime.Now; TimeSpan elapsedTime; var logic = new ChainChecker(); string errorString = string.Empty; try { logic.ProgressChanged += () => { eventsChangedFired++; }; logic.CalculationFinished += () => {; }; logic.Start(); TimeSpan x; while (((x = (DateTime.Now - startTime)).Ticks < 150 * TimeSpan.TicksPerMillisecond) && (eventsChangedFired < 3)) { Thread.Sleep(10); } Assert.IsTrue(logic.ChainLengthDictionary.Count > 0, "ChainChecker: 'ProgessChanged' does not update ChainLengthDictionary"); Assert.IsTrue(eventsChangedFired >= 2, "ChainChecker: background task does not call 'ProgessChanged' event!"); } finally { logic?.Abort(); } }
public void TestProgressChanged() { long count = 0; int eventsChangedFired = 0; DateTime previousTime = DateTime.Now; DateTime startTime = DateTime.Now; TimeSpan elapsedTime; var logic = new ChainChecker(); string errorString = string.Empty; long eventInterval = 0; try { logic.ProgressChanged += () => { eventsChangedFired++; if (errorString == string.Empty) { //Assert.IsTrue(logic.NumbersProcessed > count, $"ProgressChanged event: NumbersProcessed has not increased!"); count = logic.PercentageCompleted; elapsedTime = DateTime.Now - startTime; //Assert.IsTrue(Math.Abs(elapsedTime.Ticks - logic.ElapsedTime.Ticks) < TimeSpan.TicksPerMillisecond * 5, if (!(Math.Abs(elapsedTime.Ticks - logic.ElapsedTime.Ticks) < TimeSpan.TicksPerMillisecond * 5)) { errorString = $"ProgressChanged event: Elapsed time not correct: expected: {elapsedTime.Ticks / TimeSpan.TicksPerMillisecond} msec, was {logic.ElapsedTime.Ticks / TimeSpan.TicksPerMillisecond} msec!"; } else { if (eventsChangedFired != 1) { elapsedTime = DateTime.Now - previousTime; eventInterval = (eventInterval * (eventsChangedFired - 2) + elapsedTime.Ticks) / (eventsChangedFired - 1); } } } previousTime = DateTime.Now; }; logic.CalculationFinished += () => {; }; logic.Start(); TimeSpan x; while (((x = (DateTime.Now - startTime)).Ticks < 300 * TimeSpan.TicksPerMillisecond) && (eventsChangedFired < 4)) { Thread.Sleep(10); } Assert.IsTrue(errorString == string.Empty, errorString); Assert.IsTrue(Math.Abs(eventInterval / TimeSpan.TicksPerMillisecond - 50) <= 15, $"ProgressChanged event: time interval not ~ 20 msec (was: {eventInterval / TimeSpan.TicksPerMillisecond } msec average)!"); Assert.IsTrue(eventsChangedFired >= 2, "ChainChecker: background task does not call 'ProgessChanged' event!"); } finally { logic?.Abort(); } }
public void TestAbort() { bool eventsChangedFired; var logic = new ChainChecker(); logic.ProgressChanged += () => { eventsChangedFired = true; }; logic.CalculationFinished += () => {; }; logic.Start(); logic.Abort(); Thread.Sleep(20); eventsChangedFired = false; Thread.Sleep(100); Assert.IsTrue(!eventsChangedFired, "ChainChecker: Abort does not halt background task within 20 msec!"); }
public void TestTaskStartedEvent() { bool eventsChangedFired = false; var logic = new ChainChecker(); try { logic.ProgressChanged += () => { eventsChangedFired = true; }; logic.CalculationFinished += () => {; }; var startTime = DateTime.Now; logic.Start(); var time = DateTime.Now; Assert.IsTrue((time - startTime).Ticks < TimeSpan.TicksPerMillisecond * 100, "ChainChecker: calling Start does not return immediately"); Thread.Sleep(100); Assert.IsTrue(eventsChangedFired, "ChainChecker: background task does not call events, is it started?"); } finally { logic?.Abort(); } }