public void TestDIFactoryMerge() { DIFactory factory = new DIFactory(); factory.CreationMapping.Add(typeof(MyCreatingThing), (DIFactory f) => { return(new MyCreatingThing { Nuts = f.GetSetting <ThingSettings, int>(ThingSettings.One) }); }); factory.ReleaseMapping.Add(typeof(MyCreatingThing), (DIFactory f, object o) => { Logger.Info("releasing a MyCreatingThing"); }); factory.SetSetting(ThingSettings.One, 999); DIFactory factory2 = new DIFactory(); factory2.CreationMapping.Add(typeof(IThingy), (DIFactory f) => { return(new MyThingyImplemented()); }); factory2.ReleaseMapping.Add(typeof(IThingy), (DIFactory f, object o) => { Logger.Info("releasing an IThingy"); }); factory2.SetSetting(ThingSettings.Two, "A lot of whatever"); var newFactory = DIFactory.Merge(factory, factory2); Assert.IsTrue(newFactory.Create <MyCreatingThing>().Nuts == 999); Assert.IsTrue(newFactory.GetSetting <ThingSettings, string>(ThingSettings.Two) == "A lot of whatever"); MyAssert.ThrowsException(() => DIFactory.Merge(newFactory, factory)); MyAssert.ThrowsException(() => DIFactory.Merge(newFactory, factory2)); }
public void TestForceCopyDelete() { string filename = "testfile_forced.txt"; string filetext = "This is the file text. It is unique! " + Guid.NewGuid().ToString(); File.WriteAllText(filename, filetext); File.SetAttributes(filename, FileAttributes.ReadOnly); MyAssert.ThrowsException(() => File.Delete(filename)); Assert.IsTrue(File.ReadAllText(filename) == filetext); FileServices.ForceDelete(filename); Assert.IsFalse(File.Exists(filename)); string copyname = "copy" + filename; string copytext = "It's a copy " + Guid.NewGuid().ToString(); File.WriteAllText(filename, filetext); File.WriteAllText(copyname, copytext); File.SetAttributes(filename, FileAttributes.ReadOnly); MyAssert.ThrowsException(() => File.Copy(copyname, filename)); Assert.IsTrue(File.ReadAllText(filename) == filetext); Assert.IsTrue(File.ReadAllText(copyname) == copytext); FileServices.ForceCopy(copyname, filename); Assert.IsTrue(File.ReadAllText(filename) == copytext); Assert.IsTrue(File.ReadAllText(copyname) == copytext); }
public void When_accessing_an_index_outside_bounds_an_exception_is_thrown() { MyAssert.ThrowsException <IndexOutOfRangeException>(() => { _testList[-1] = 42; }); MyAssert.ThrowsException <IndexOutOfRangeException>(() => { int a = _testList[-1]; }); MyAssert.ThrowsException <IndexOutOfRangeException>(() => { _testList[10] = 42; }); MyAssert.ThrowsException <IndexOutOfRangeException>(() => { int a = _testList[10]; }); }
public void App_Setting_Not_Found() { //arrange var sut = Container.GetInstance <IConfigItemProvider <AppSetting> >(); //act+assert MyAssert.ThrowsException <ConfigItemNotFoundException>(() => sut.Get("dummy")); }
public void TestFileVersion() { Version expectedVersion = new Version("9.8.7.6"); var actualVersion = FileServices.GetFileVersionOfDll("MonolithicExtensions.UnitTest"); Assert.IsTrue(actualVersion.Equals(expectedVersion)); MyAssert.ThrowsException(() => FileServices.GetFileVersionOfDll("SomethingThatDoesntExist")); actualVersion = FileServices.GetOwnFileVersion(); }
public void TestConvertToEnum() { LogStart("TestConvertToEnum"); Assert.IsTrue("FlatEarth".ConvertToEnum <Conspiracy>() == Conspiracy.FlatEarth); Assert.IsTrue("MoonLanding".ConvertToEnum <Conspiracy>() == Conspiracy.MoonLanding); Assert.IsTrue("NorthPoleHole".ConvertToEnum <Conspiracy>() == Conspiracy.NorthPoleHole); MyAssert.ThrowsException(() => "What".ConvertToEnum <Conspiracy>()); }
public void When_clearing_the_list_no_items_are_accessible() { _testList.Add(1); _testList.Add(2); Assert.AreEqual(2, _testList.Count); _testList.Clear(); Assert.AreEqual(0, _testList.Count); MyAssert.ThrowsException <IndexOutOfRangeException>(() => { _testList[0] = 42; }); MyAssert.ThrowsException <IndexOutOfRangeException>(() => { int a = _testList[0]; }); }
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 BasicDIFactoryCreate() { DIFactory factory = new DIFactory(); factory.CreationMapping.Add(typeof(MyCreatingThing), (DIFactory f) => { return(new MyCreatingThing { Nuts = 88 }); }); Assert.IsTrue(factory.Create <MyCreatingThing>().Nuts == 88); factory.CreationMapping.Add(typeof(IThingy), (DIFactory f) => { return(new MyThingyImplemented()); }); Assert.IsTrue(factory.Create <IThingy>().GetAValue() == 123); MyAssert.ThrowsException(() => factory.Create <DIFactory>()); MyCreatingThing specialSetting = new MyCreatingThing { Nuts = 678 }; factory.SetSettingByType(specialSetting); Assert.IsTrue(factory.GetSettingByType <MyCreatingThing>().Nuts == 678); }
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); }
public void TestHttpRemoteCall() { LogStart("TestHttpRemoteCall"); var serverService = new SimpleService(); var serviceType = typeof(SimpleService); var server = new HttpRemoteCallServer(CreateJsonService(), new HttpRemoteCallServerConfig(), CreateGeneralConfig()); var client = new HttpRemoteCallClient(CreateJsonService(), new HttpRemoteCallClientConfig()); server.Start("http://+:45677", new Dictionary <string, object>() { { RemoteCallService, serverService } }); client.Endpoint = $"http://localhost:45677/{RemoteCallService}"; int result = client.CallAsync <int>(serviceType.GetMethod("AddNumbers"), new List <object>() { 5, 6 }).Result; Assert.IsTrue(result == 11); result = client.Call <int>(serviceType.GetMethod("AddNumbers"), new List <object>() { 5, 6 }); Assert.IsTrue(result == 11); result = (int)client.CallAsync <long>(serviceType.GetMethod("MultiplyNumbers"), new List <object>() { 7, 8 }).Result; Assert.IsTrue(result == 56); result = (int)client.Call <long>(serviceType.GetMethod("MultiplyNumbers"), new List <object>() { 7, 8 }); Assert.IsTrue(result == 56); Assert.IsTrue(client.CallAsync <string>(serviceType.GetMethod("GetHello"), null).Result == "Hello!"); Assert.IsTrue(client.Call <string>(serviceType.GetMethod("GetHello"), null) == "Hello!"); client.CallVoidAsync(serviceType.GetMethod("SetHello"), new List <object>() { "doggo" }).Wait(); Assert.IsTrue(client.CallAsync <string>(serviceType.GetMethod("GetHello"), null).Result == "doggo"); client.CallVoidAsync(serviceType.GetMethod("SetHello"), new List <object>() { null }).Wait(); Assert.IsTrue(client.CallAsync <string>(serviceType.GetMethod("GetHello"), null).Result == null); client.CallVoidAsync(serviceType.GetMethod("SetHello"), new List <object>() { "newstring" }).Wait(); Assert.IsTrue(client.CallAsync <string>(serviceType.GetMethod("GetHello"), null).Result == "newstring"); MyAssert.ThrowsException(() => client.CallVoidAsync(serviceType.GetMethod("ThrowException"), null).Wait()); MyAssert.ThrowsException(() => client.CallVoid(serviceType.GetMethod("ThrowException"), null)); server.Stop(); }