AppHost.Bundle.Tests: Fix a flaky test (#32563)
authorSwaroop Sridhar <Swaroop.Sridhar@microsoft.com>
Wed, 19 Feb 2020 23:49:18 +0000 (15:49 -0800)
committerGitHub <noreply@github.com>
Wed, 19 Feb 2020 23:49:18 +0000 (23:49 +0000)
The BundleRename test is an inherently concurrent test which needs the app and the test infrastructure to synchronize.
Originally, the app and the test synchronized using a lock file: the app creates the lock file and waits until the lock is deleted.
However, this caused problems because the test may attempt to delete the lock file even before the app has closed it.

Therefore, this change fixes the issue by using different files to trigger the app's wait and resume steps.

src/installer/test/Assets/TestProjects/AppWithWait/Program.cs
src/installer/test/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleRename.cs

index 7a39929..220c3c6 100644 (file)
@@ -10,20 +10,18 @@ namespace AppWithSubDirs
         {
             Console.Write("Hello ");
 
-            // If the caller wants the app to start and wait,
-            // it provides the name of a lock-file to write.
-            // In this case, this test app creates the lock file
-            // and waits until the file is deleted.
-            if (args.Length > 0)
+            // If the caller wants the app to start and wait, it provides the names of two files.
+            // In this case, this test app creates the waitFile, and waits until resumeFile is created
+            if (args.Length == 2)
             {
-                string writeFile = args[0];
+                string waitFile = args[0];
+                string resumeFile = args[1];
 
-                var fs = File.Create(writeFile);
-                fs.Close();
+                File.Create(waitFile).Close();
 
                 Thread.Sleep(200);
 
-                while (File.Exists(writeFile))
+                while (!File.Exists(resumeFile))
                 {
                     Thread.Sleep(100);
                 }
index 07efcce..ae01b4d 100644 (file)
@@ -29,7 +29,8 @@ namespace AppHost.Bundle.Tests
             var fixture = sharedTestState.TestFixture.Copy();
             string singleFile = BundleHelper.GetPublishedSingleFilePath(fixture);
             string renameFile = Path.Combine(BundleHelper.GetPublishPath(fixture), Path.GetRandomFileName());
-            string writeFile = Path.Combine(BundleHelper.GetPublishPath(fixture), "lock");
+            string waitFile = Path.Combine(BundleHelper.GetPublishPath(fixture), "wait");
+            string resumeFile = Path.Combine(BundleHelper.GetPublishPath(fixture), "resume");
 
             if (!renameFirstRun)
             {
@@ -43,19 +44,19 @@ namespace AppHost.Bundle.Tests
                     .HaveStdOutContaining("Hello World!");
             }
 
-            var singleExe = Command.Create(singleFile, writeFile)
+            // Once the App starts running, it creates the waitFile, and waits until resumeFile file is created.
+            var singleExe = Command.Create(singleFile, waitFile, resumeFile)
                 .CaptureStdErr()
                 .CaptureStdOut()
                 .Start();
 
-            // Once the App starts running, it creates the writeFile, and waits until the file is deleted.
-            while (!File.Exists(writeFile))
+            while (!File.Exists(waitFile))
             {
                 Thread.Sleep(100);
             }
 
             File.Move(singleFile, renameFile);
-            File.Delete(writeFile);
+            File.Create(resumeFile).Close();
 
             var result = singleExe.WaitForExit(fExpectedToFail: false);