public void ShouldNotSuccessfullyUninstallWithDefaultUninstallMethod() { // by default, Uninstall _should_ wait for the stop / uninstall // but _not_ kill (in case of loss of data) // Arrange var serviceExe = StubbornServiceExe; var serviceName = $"stubborn-service{Guid.NewGuid()}"; var util = Create( serviceName, serviceName, serviceExe ); using var _ = new AutoResetter(Noop, () => util.Uninstall( ControlOptions.Force | ControlOptions.Wait )); util.InstallAndStart(); // Act Expect(() => util.Uninstall()) .To.Throw <ServiceOperationException>() .With.Message.Containing("Unable to perform Stop"); // Assert Expect(util.IsInstalled) .To.Be.True(); Process process = null; Expect(() => process = Process.GetProcessById(util.ServicePID)) .Not.To.Throw(); Expect(process.HasExited) .To.Be.False(); }
public void ShouldSetPortHintFromEnvironmentWhenAvailable() { // Arrange using var resetter = new AutoResetter <int?>( StoreCurrentPortHint, RestorePortHint); var expected = GetRandomInt(15000, 20000); Environment.SetEnvironmentVariable( TempDbMySqlServerSettings.EnvironmentVariables.PORT_HINT, expected.ToString() ); Expect( Environment.GetEnvironmentVariable( TempDbMySqlServerSettings.EnvironmentVariables.PORT_HINT ) ).To.Equal(expected.ToString()); // Act var sut = new TempDbMySqlServerSettings() { Options = { DefaultSchema = GetRandomAlphaString(8, 8) }, InnodbFlushLogAtTrxCommit = 2, SlowQueryLog = 0 }; // Assert Expect(sut.Options.PortHint) .To.Equal(expected); }
public void ShouldKillTheCorrectService() { // Arrange var service1Name = $"service-1-{Guid.NewGuid()}"; var service2Name = $"service-2-{Guid.NewGuid()}"; var args1 = GetRandomArray <string>(2); var args2 = GetRandomArray <string>(2); var cli1 = $"\"{SpacedServiceExe}\" {string.Join(" ", args1)}"; var cli2 = $"\"{SpacedServiceExe}\" {string.Join(" ", args2)}"; var util1 = Create(service1Name, service1Name, cli1); var util2 = Create(service2Name, service2Name, cli2); util1.InstallAndStart(); util2.InstallAndStart(); using var _ = new AutoResetter(Noop, () => { TryDo(() => util1.Uninstall()); TryDo(() => util2.Uninstall()); }); var processes = Process.GetProcesses() .Where(p => HasMainModule(p, SpacedServiceExe)) .ToArray(); var p1 = processes.FirstOrDefault( p => { var testArgs = p.QueryCommandline() .SplitCommandline() .Skip(1) .ToArray(); return(testArgs.Matches(args1)); } ); var p2 = processes.FirstOrDefault( p => p.QueryCommandline() .SplitCommandline() .Skip(1) .Matches(args2) ); Expect(p1) .Not.To.Be.Null(() => $"Can't find process for {cli1}"); Expect(p2) .Not.To.Be.Null(() => $"Can't find process for {cli2}"); // Act util1.KillService(); // Assert Expect(p1.HasExited) .To.Be.True(); Expect(p2.HasExited) .To.Be.False(); }
public void Dispose_ShouldCallDisposalActionExactlyOnce() { //---------------Set up test pack------------------- var disposeCalls = 0; var sut = new AutoResetter(() => { }, () => disposeCalls++); //---------------Assert Precondition---------------- Expect(disposeCalls).To.Equal(0); //---------------Execute Test ---------------------- sut.Dispose(); //---------------Test Result ----------------------- Expect(disposeCalls).To.Equal(1); }
public void Dispose_ShouldNotRecallDisposeActionWhenCalledAgain() { //---------------Set up test pack------------------- var disposeCalls = 0; var sut = new AutoResetter(() => { }, () => disposeCalls++); //---------------Assert Precondition---------------- Expect(disposeCalls).To.Equal(0); //---------------Execute Test ---------------------- sut.Dispose(); sut.Dispose(); //---------------Test Result ----------------------- Expect(disposeCalls).To.Equal(1); }
public void Dispose_ShouldOnlyCallActionOfT_Once() { //---------------Set up test pack------------------- var disposeCalls = 0; var sut = new AutoResetter <int>(() => 0, v => disposeCalls++); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- sut.Dispose(); Expect(disposeCalls).To.Equal(1); sut.Dispose(); //---------------Test Result ----------------------- Expect(disposeCalls).To.Equal(1); }
public void ShouldRetryOnError() { // Arrange using var arena = new TestArena(); var options = Substitute.For <IOptions>(); options.Retries.Returns(3); using var resetter = new AutoResetter <int>(() => { var original = StreamSource.MaxBuffer; StreamSource.MaxBuffer = 1; return(original); }, original => StreamSource.MaxBuffer = original); var haveErrored = false; var errorPassThrough = new GenericPassThrough( (data, size) => { if (!haveErrored) { haveErrored = true; throw new DirectoryNotFoundException("foo"); } }, () => { } ); // keep the size small since we're forcing a flush at every byte var sourceFile = arena.CreateSourceFile(data: GetRandomBytes(10, 20)); var expected = arena.TargetPathFor(sourceFile); var sut = Create( intermediatePipes: new IPassThrough[] { errorPassThrough }, options: options); // Act sut.Synchronize(arena.SourceFileSystem, arena.TargetFileSystem); // Assert Expect(expected) .To.Exist(); Expect(File.ReadAllBytes(expected)) .To.Equal(sourceFile.Data); }
public void InstallingServiceWithSpacedPath() { // Arrange Expect(SpacedServiceExe) .Not.To.Be.Null(); var sut = Create( SpacedServiceName, SpacedServiceDisplayName, $"\"{SpacedServiceExe}\" --arg --another-arg" ); sut.ServiceStateExtraWaitSeconds = 30; // Act var completed = false; using var _ = new AutoResetter(Noop, () => { if (completed) { return; } EnsureServiceNotRunning( SpacedServiceName, SpacedServiceExe ); }); Expect(sut.Install) .Not.To.Throw(); // Assert Expect(sut.Start) .Not.To.Throw(); Expect(sut.State) .To.Equal(ServiceState.Running); sut.Stop(); Expect(sut.State) .To.Equal(ServiceState.Stopped); sut.Uninstall(); // signal the safety-net that it doesn't have to do anything completed = true; }
public void ShouldRespectNoSnakeCaseOnRequest() { // Arrange GlobalSetup.PerformDefaultConfiguration(); using var _ = new AutoResetter(DisableSnakeCase, EnableSnakeCase); using var conn = GlobalSetup.ConnectToTempDb(); CreateCaseTestingTableOn(conn); var titleCaseValue = GetRandomString(4); var snakeCaseValue = GetAnother(titleCaseValue, () => GetRandomString(4)); var id = CommandExecutor.Execute( new CreateCaseTestingEntity( titleCaseValue, snakeCaseValue ) ); var query = new FindCaseTestingEntity(id); // Act var whenSnakeCaseDisabled = QueryExecutor.Execute(query); EnableSnakeCase(); var whenSnakeCaseEnabled = QueryExecutor.Execute(query); // Assert Expect(whenSnakeCaseDisabled) .Not.To.Be.Null("entity not found at all"); Expect(whenSnakeCaseDisabled.TitleCased) .To.Equal(titleCaseValue, "TitleCased should always be mapped"); Expect(whenSnakeCaseDisabled.SnakeCased) .To.Be.Null("snake_case is disabled -- should not map the property"); Expect(whenSnakeCaseEnabled) .Not.To.Be.Null("entity not found at all"); Expect(whenSnakeCaseEnabled.TitleCased) .To.Equal(titleCaseValue, "TitleCased should always be mapped"); Expect(whenSnakeCaseEnabled.SnakeCased) .To.Equal(snakeCaseValue, "snake_case is enabled -- should be mapped"); }
public void ShouldPassWhenNegatingAFailingComposition() { // Arrange using var _ = new AutoResetter( () => Assertions.RegisterAssertionsFactory( s => new AssertionException(s) ), Assertions.UseDefaultAssertionsFactory ); var person = new Person() { Gender = Genders.Unknown, Name = "Blergschootz" }; // Act Assert.That(() => { Expect(person) .Not.To.Be.A.Benny(); }, Throws.Nothing); // Assert }
public void ServiceWithArgs() { // Arrange var myPath = new Uri(GetType().Assembly.Location).LocalPath; var myDir = Path.GetDirectoryName(myPath); var serviceExe = Path.Combine(myDir, "ServiceWithArgs.exe"); var logFile = Path.Combine(myDir, "service.log"); var arg1 = GetRandomString(3); var arg2 = GetRandomString(3); var args = new[] { logFile, arg1, arg2 }.Select(p => p.QuoteIfSpaced()); var serviceName = "test-service-foo-bar"; var displayName = "Test Service - Foo,Bar"; var commandline = string.Join( " ", new[] { serviceExe } .Concat(args) ); var util = new WindowsServiceUtil( serviceName, displayName, commandline ); if (util.IsInstalled) { util.Uninstall(); } if (File.Exists(logFile)) { File.Delete(logFile); } // Act using var resetter = new AutoResetter( () => util.Install(), () => util.Uninstall(true)); util.Install(); // Assert Expect(util.IsInstalled) .To.Be.True(); util.Start(); Expect(util.State) .To.Equal(ServiceState.Running); Expect(logFile) .To.Exist(); var logData = TryReadAllLinesFrom(logFile); Expect(logData) .Not.To.Be.Empty(); Expect(logData[0]) .To.Contain(arg1) .Then(arg2); util.Stop(); Expect(util.State) .To.Equal(ServiceState.Stopped); var anotherUtil = Create(serviceName); Expect(anotherUtil.Commandline) .To.Equal(commandline); Expect(anotherUtil.ServiceExe) .To.Equal(serviceExe); Expect(anotherUtil.DisplayName) .To.Equal(displayName); }