public void TestWaitOnAction() { //Dim startTime As DateTime DateTime failEnd = default(DateTime); Action testAction = () => { if (DateTime.Now < failEnd) { throw new Exception("Still not time to complete"); } }; failEnd = DateTime.Now.AddMilliseconds(40); Assert.IsTrue(ThreadingServices.TryWaitOnAction(testAction, TimeSpan.FromSeconds(2))); failEnd = DateTime.Now.AddSeconds(10); Assert.IsFalse(ThreadingServices.TryWaitOnAction(testAction, TimeSpan.FromSeconds(1))); }
public void TestTimeout() { LogStart("TestTimeout"); int value = 0; ThreadingServices.Timeout(TimeSpan.FromMilliseconds(300), () => value = 5); Assert.IsTrue(value == 0); Thread.Sleep(400); Assert.IsTrue(value == 5); var source = new CancellationTokenSource(); ThreadingServices.Timeout(TimeSpan.FromMilliseconds(300), (c) => value = 10, source.Token); source.Cancel(); Assert.IsTrue(value == 5); Thread.Sleep(400); Assert.IsTrue(value == 5); }
public void TestRestartManagerManualShutdown() { var ReplacementProcessCopy = ReplacementProcess + "2"; File.Copy(ReplacementProcess, ReplacementProcessCopy, true); //Start up the service and TRY to move the file. It should fail Process proc = Process.Start(ReplacementProcess); //We're hoping this will fail, as the process SHOULD be holding onto this guy MyAssert.ThrowsException(() => File.Copy(ReplacementProcessCopy, ReplacementProcess, true)); //Now startup the restart manager and lets hope the process will be restarted. RestartManagerExtendedSession manager = new RestartManagerExtendedSession(); manager.ManualRestartProcesses.Add(ReplacementProcess); manager.StartSession(); manager.RegisterResources(new List <string>() { ReplacementProcess }); RM_REBOOT_REASON rebootReason = default(RM_REBOOT_REASON); var processes = RestartManager.RmProcessToNetProcess(manager.GetList(ref rebootReason)); Assert.IsTrue(rebootReason == RM_REBOOT_REASON.RmRebootReasonNone); Assert.IsTrue(processes.Count > 0); //After shutdown, the file should be copyable manager.Shutdown(); ThreadingServices.WaitOnAction(() => File.Copy(ReplacementProcessCopy, ReplacementProcess, true), TimeSpan.FromSeconds(3)); //Now try to restart everything manager.Restart(); //We're hoping this will fail, as the service SHOULD be holding onto this guy again MyAssert.ThrowsException(() => ThreadingServices.WaitOnAction(() => File.Copy(ReplacementProcessCopy, ReplacementProcess, true), TimeSpan.FromSeconds(2))); manager.EndSession(); System.Diagnostics.Process.GetProcessesByName(ReplacementProcess.Replace(".exe", "")).ToList().ForEach(x => x.Kill()); }
public void TestForceSingleInstance() { int myValue = 10; AutoResetEvent firstTaskSignal = new AutoResetEvent(false); //The first task just waits around for 2 seconds, then sets a value. It SHOULD be able to lock Task firstTask = Task.Run(() => { ThreadingServices.LockGlobalMutexDuringAction(() => { firstTaskSignal.Set(); System.Threading.Thread.Sleep(2000); myValue = 20; }, TimeSpan.FromSeconds(1)); }); firstTaskSignal.WaitOne(); Assert.IsTrue(myValue == 10); //The second task immediately tries to set the value, HOWEVER since hte first task is still technically //running, the second task SHOULD throw an exception. MyAssert.ThrowsException(() => { ThreadingServices.LockGlobalMutexDuringAction(() => myValue = 30, TimeSpan.FromSeconds(1)); }); Assert.IsTrue(myValue == 10); //Now just wait for the first to exit and ensure the value is updated accordingly firstTask.Wait(); Assert.IsTrue(myValue == 20); }
public void TestRestartManagerServiceFileMove() { //Start up the service and TRY to move the file. It should fail StartService(TestHelpers.TestService); //We're hoping this will fail, as the service SHOULD be holding onto this guy MyAssert.ThrowsException(() => File.Copy(ReplacementFile, ReplacementFileServicePath, true)); //Now startup the restart manager and lets hope the service will be restarted. RestartManagerSession manager = new RestartManagerSession(); manager.StartSession(); manager.RegisterResources(new List <string>() { ReplacementFileServicePath }); RM_REBOOT_REASON rebootReason = default(RM_REBOOT_REASON); var processes = RestartManager.RmProcessToNetProcess(manager.GetList(ref rebootReason)); Assert.IsTrue(rebootReason == RM_REBOOT_REASON.RmRebootReasonNone); Assert.IsTrue(processes.Count > 0); //After shutdown, the file should be copyable manager.Shutdown(); ThreadingServices.WaitOnAction(() => File.Copy(ReplacementFile, ReplacementFileServicePath, true), TimeSpan.FromSeconds(3)); //Now try to restart everything manager.Restart(); //We're hoping this will fail, as the service SHOULD be holding onto this guy again MyAssert.ThrowsException(() => File.Copy(ReplacementFile, ReplacementFileServicePath, true)); manager.EndSession(); StopService(TestHelpers.TestService); }