[TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11 public void IsTestFile_TimeoutIfConfigLocked() { // Arrange // We'll lock the file and sleep for long enough for the task to timeout string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result; using (FileStream lockingStream = File.OpenWrite(configFile)) { System.Threading.Tasks.Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds + 600); // sleep for longer than the timeout period lockingStream.Close(); }); result = task.Execute(); } Assert.IsFalse(result, "Expecting the task to fail"); dummyEngine.AssertSingleMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString()); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleErrorExists(); }
[TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11 public void IsTestFile_RetryIfConfigLocked() { // Arrange // We'll lock the file and sleep for long enough for the retry period to occur, but // not so long that the task times out int lockPeriodInMilliseconds = 1000; Assert.IsTrue(lockPeriodInMilliseconds < IsTestFileByName.MaxConfigRetryPeriodInMilliseconds, "Test setup error: the test is sleeping for too long"); string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result; Stopwatch testDuration = Stopwatch.StartNew(); using (FileStream lockingStream = File.OpenWrite(configFile)) { System.Threading.Tasks.Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(lockPeriodInMilliseconds); // unlock the file after a short delay lockingStream.Close(); }); result = task.Execute(); } testDuration.Stop(); Assert.IsTrue(testDuration.ElapsedMilliseconds > 1000, "Test error: expecting the test to have taken at least {0} milliseconds to run. Actual: {1}", lockPeriodInMilliseconds, testDuration.ElapsedMilliseconds); Assert.IsTrue(result, "Expecting the task to succeed"); Assert.IsTrue(task.IsTest, "Expecting the file to be recognised as a test"); dummyEngine.AssertSingleMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString()); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); }
public void AttachBW_MissingConfigFile_TaskFails() { // Arrange string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, null /* output folder */, 0 /* returns success code */ ); // Remove the config file File.Delete(taskContext.ConfigFilePath); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, binFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsFalse(result, "Expecting task to fail"); dummyEngine.AssertSingleMessageExists(SonarQube.MSBuild.Tasks.Resources.Shared_ConfigFileNotFound); dummyEngine.AssertNoWarnings(); AssertLogFileDoesNotExist(taskContext); }
public void AttachBW_NotAttached_ExeReturnsSuccess_TaskSucceeds() { // Arrange string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config"); string outputFolder = Path.Combine(binFolder, "output"); // expected location: does not exist DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, outputFolder, 0 /* returns success code */ ); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, configFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsTrue(result, "Expecting task to succeed"); Assert.IsTrue(Directory.Exists(outputFolder), "Expecting the output folder to have been created"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleMessageExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_AttachedSuccessfully); // Check the parameters passed to the exe string logPath = AssertLogFileExists(taskContext); DummyExeHelper.AssertExpectedLogContents(logPath, "--msbuild-task", Process.GetCurrentProcess().Id.ToString(), outputFolder); }
public void AttachBW_NotAttached_ConsoleOutputIsCaptured() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string outputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "bw_output"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(testFolder, testFolder, outputFolder, 0 /* returns success code */, // Embed additional code in the dummy exe @"System.Console.WriteLine(""AAA standard output should be captured""); System.Console.Error.WriteLine(""BBB standard error should be captured"");"); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, testFolder, testFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsTrue(result, "Expecting the task to succeed"); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleMessageExists("AAA standard output should be captured"); dummyEngine.AssertSingleErrorExists("BBB standard error should be captured"); }
public void IsTestFile_TimeoutIfConfigLocked() { // Arrange // We'll lock the file and sleep for long enough for the task to timeout string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result; using (FileStream lockingStream = File.OpenWrite(configFile)) { System.Threading.Tasks.Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds + 600); // sleep for longer than the timeout period lockingStream.Close(); }); result = task.Execute(); } Assert.IsFalse(result, "Expecting the task to fail"); dummyEngine.AssertSingleMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString()); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleErrorExists(); }
public void IsTestFile_RetryIfConfigLocked() { // Arrange // We'll lock the file and sleep for long enough for the retry period to occur, but // not so long that the task times out int lockPeriodInMilliseconds = 1000; Assert.IsTrue(lockPeriodInMilliseconds < IsTestFileByName.MaxConfigRetryPeriodInMilliseconds, "Test setup error: the test is sleeping for too long"); string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result; Stopwatch testDuration = Stopwatch.StartNew(); using (FileStream lockingStream = File.OpenWrite(configFile)) { System.Threading.Tasks.Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(lockPeriodInMilliseconds); // unlock the file after a short delay lockingStream.Close(); }); result = task.Execute(); } testDuration.Stop(); Assert.IsTrue(testDuration.ElapsedMilliseconds > 1000, "Test error: expecting the test to have taken at least {0} milliseconds to run. Actual: {1}", lockPeriodInMilliseconds, testDuration.ElapsedMilliseconds); Assert.IsTrue(result, "Expecting the task to succeed"); Assert.IsTrue(task.IsTest, "Expecting the file to be recognised as a test"); dummyEngine.AssertSingleMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString()); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); }