Fix COM test failures on Windows Nano Server (#57148)
authorJeremy Koritzinsky <jekoritz@microsoft.com>
Tue, 10 Aug 2021 19:28:59 +0000 (14:28 -0500)
committerGitHub <noreply@github.com>
Tue, 10 Aug 2021 19:28:59 +0000 (19:28 +0000)
* Disable ComWrappers activation tests when running on Windows Nano Server since they require reg-free COM.

* Run the test on a separate STA thread so we can get far enough in Main to detect running on nano server before we use the STA.

src/tests/Interop/COM/ComWrappers/GlobalInstance/GlobalInstance.Marshalling.cs
src/tests/Interop/COM/ComWrappers/GlobalInstance/GlobalInstance.TrackerSupport.cs
src/tests/Interop/COM/NETClients/Lifetime/Program.cs

index d01d79f..1738dbc 100644 (file)
@@ -47,7 +47,8 @@ namespace ComWrappersTests.GlobalInstance
                     ValidatePInvokes(validateUseRegistered: false);
                 }
 
-                if(!builtInComDisabled)
+                // RegFree COM is not supported on Windows Nano Server
+                if(!builtInComDisabled && !Utilities.IsWindowsNanoServer)
                 {
                     // This calls ValidateNativeServerActivation which calls Marshal.GetTypeFromCLSID that is not supported
                     ValidateComActivation(validateUseRegistered: true);
index 0eaa5fa..dd8ea4f 100644 (file)
@@ -33,7 +33,7 @@ namespace ComWrappersTests.GlobalInstance
                 ValidateRegisterForTrackerSupport();
 #if Windows
                 ValidateNotRegisteredForMarshalling();
-#endif                
+#endif
 
                 IntPtr trackerObjRaw = MockReferenceTrackerRuntime.CreateTrackerObject();
                 var trackerObj = GlobalComWrappers.Instance.GetOrCreateObjectForComInstance(trackerObjRaw, CreateObjectFlags.TrackerObject);
@@ -50,8 +50,12 @@ namespace ComWrappersTests.GlobalInstance
                 ValidatePInvokes(validateUseRegistered: true);
                 ValidatePInvokes(validateUseRegistered: false);
 
-                ValidateComActivation(validateUseRegistered: true);
-                ValidateComActivation(validateUseRegistered: false);
+                // RegFree COM is not supported on Windows Nano Server
+                if (!Utilities.IsWindowsNanoServer)
+                {
+                    ValidateComActivation(validateUseRegistered: true);
+                    ValidateComActivation(validateUseRegistered: false);
+                }
 #endif
                 ValidateNotifyEndOfReferenceTrackingOnThread();
             }
index a8a5132..ee6fb79 100644 (file)
@@ -81,31 +81,42 @@ namespace NetClient
             Assert.IsFalse(Marshal.AreComObjectsAvailableForCleanup());
         }
 
-        [STAThread]
         static int Main(string[] doNotUse)
         {
-            // RegFree COM is not supported on Windows Nano
+            // RegFree COM and STA apartments are not supported on Windows Nano
             if (Utilities.IsWindowsNanoServer)
             {
                 return 100;
             }
 
-            try
-            {
-                // Initialization for all future tests
-                Initialize();
-                Assert.IsTrue(GetAllocationCount != null);
+            int result = 101;
 
-                Validate_COMServer_CleanUp();
-                Validate_COMServer_DisableEagerCleanUp();
-            }
-            catch (Exception e)
+            // Run the test on a new STA thread since Nano Server doesn't support the STA
+            // and as a result, the main application thread can't be made STA with the STAThread attribute
+            Thread staThread = new Thread(() =>
             {
-                Console.WriteLine($"Test Failure: {e}");
-                return 101;
-            }
-
-            return 100;
+                try
+                {
+                    // Initialization for all future tests
+                    Initialize();
+                    Assert.IsTrue(GetAllocationCount != null);
+
+                    Validate_COMServer_CleanUp();
+                    Validate_COMServer_DisableEagerCleanUp();
+                }
+                catch (Exception e)
+                {
+                    Console.WriteLine($"Test Failure: {e}");
+                    result = 101;
+                }
+                result = 100;
+            });
+
+            staThread.SetApartmentState(ApartmentState.STA);
+            staThread.Start();
+            staThread.Join();
+
+            return result;
         }
     }
 }