Skip to content

Commit

Permalink
Fix COM test failures on Windows Nano Server (#57148)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jkoritzinsky authored Aug 10, 2021
1 parent 302ad86 commit b7de262
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ static int Main(string[] doNotUse)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int Main(string[] doNotUse)
ValidateRegisterForTrackerSupport();
#if Windows
ValidateNotRegisteredForMarshalling();
#endif
#endif

IntPtr trackerObjRaw = MockReferenceTrackerRuntime.CreateTrackerObject();
var trackerObj = GlobalComWrappers.Instance.GetOrCreateObjectForComInstance(trackerObjRaw, CreateObjectFlags.TrackerObject);
Expand All @@ -50,8 +50,12 @@ static int Main(string[] doNotUse)
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();
}
Expand Down
43 changes: 27 additions & 16 deletions src/tests/Interop/COM/NETClients/Lifetime/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,42 @@ static void Validate_COMServer_DisableEagerCleanUp()
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;
}
}
}

0 comments on commit b7de262

Please sign in to comment.