Skip to content

Commit 3cb22d9

Browse files
authored
Add sleep to allow background threads to quiesce (microsoft#5933)
## Issue One of the inproc tests is failing because the module is not unloaded (nor are any of the statics destroyed) after the attempt to unload it through `CoFreeUnusedLibrariesEx`. This suggests that objects are still active and the unload is doing the correct thing. While this could be a leak, the initial thinking is that this test fails due to the background threads racing to finish up their work as the main thread continues on after the completion is signaled. The name-based tests that expect the module to unload also need to free the activation factories that C++/WinRT caches before calling into the test method, which appears to be giving them enough time to complete successfully. ## Change Add a sleep option to the tests and use it for the failing one. A more invasive solution would be to use the shutdown monitoring the forcibly close out and wait for the background threads, but test only exports are a pain to manage.
1 parent 920c411 commit 3cb22d9

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/AppInstallerCLIE2ETests/InprocTestbedTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,21 @@ public void DefaultTest()
9898
/// </summary>
9999
/// <param name="leakCOM">Control whether COM should be uninitialized at the end of the process.</param>
100100
/// <param name="unloadBehavior">Set the unload behavior for the test.</param>
101+
/// <param name="workTestSleep">Sets the number of milliseconds to sleep between each work/test iteration.</param>
101102
[Test]
102103
[TestCase(false, UnloadBehavior.AtUninitialize)]
103104
[TestCase(false, UnloadBehavior.Never)]
104-
[TestCase(true, UnloadBehavior.Allow)]
105+
[TestCase(true, UnloadBehavior.Allow, 1000)]
105106
[TestCase(true, UnloadBehavior.Never)]
106-
public void CLSID_Tests(bool leakCOM, UnloadBehavior unloadBehavior)
107+
public void CLSID_Tests(bool leakCOM, UnloadBehavior unloadBehavior, int? workTestSleep = null)
107108
{
108109
this.RunInprocTestbed(new TestbedParameters()
109110
{
110111
ActivationType = ActivationType.CoCreateInstance,
111112
LeakCOM = leakCOM,
112113
UnloadBehavior = unloadBehavior,
113114
Iterations = 10,
115+
WorkTestSleepInterval = workTestSleep,
114116
});
115117
}
116118

@@ -175,6 +177,11 @@ private void RunInprocTestbed(TestbedParameters parameters, int timeout = 300000
175177
builtParameters += $"-itr {parameters.Iterations} ";
176178
}
177179

180+
if (parameters.WorkTestSleepInterval != null)
181+
{
182+
builtParameters += $"-work-test-sleep {parameters.WorkTestSleepInterval} ";
183+
}
184+
178185
var result = TestCommon.RunProcess(this.InprocTestbedPath, this.TargetPackageInformation, builtParameters, null, timeout, true);
179186
Assert.AreEqual(0, result.ExitCode);
180187
}
@@ -195,6 +202,8 @@ private class TestbedParameters
195202
internal string Test { get; init; } = "unload_check";
196203

197204
internal int? Iterations { get; init; } = null;
205+
206+
internal int? WorkTestSleepInterval { get; init; } = null;
198207
}
199208
}
200209
}

src/ComInprocTestbed/Tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ TestParameters::TestParameters(int argc, const char** argv)
246246
{
247247
SkipClearFactories = true;
248248
}
249+
else if ("-work-test-sleep"sv == argv[i])
250+
{
251+
ADVANCE_ARG_PARAMETER
252+
WorkTestSleepInterval = atoi(argv[i]);
253+
}
249254
}
250255
}
251256

@@ -259,6 +264,7 @@ void TestParameters::OutputDetails() const
259264
" Unload : " << UnloadBehavior << "\n"
260265
" Expect : " << std::boolalpha << UnloadExpected() << "\n"
261266
" Test : " << TestToRun << "\n"
267+
" Sleep : " << WorkTestSleepInterval << "\n"
262268
" Package : " << PackageName << "\n"
263269
" Source : " << SourceName << "\n"
264270
" URL : " << SourceURL << "\n"

src/ComInprocTestbed/Tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct TestParameters
7474
UnloadBehavior UnloadBehavior = UnloadBehavior::Allow;
7575
ActivationType ActivationType = ActivationType::ClassName;
7676
bool SkipClearFactories = false;
77+
DWORD WorkTestSleepInterval = 0;
7778
};
7879

7980
// Captures a snapshot of current resource usage.

src/ComInprocTestbed/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ int main(int argc, const char** argv) try
3131
winrt::clear_factory_cache();
3232
}
3333

34+
if (testParameters.WorkTestSleepInterval)
35+
{
36+
Sleep(testParameters.WorkTestSleepInterval);
37+
}
38+
3439
if (test && !test->RunIterationTest())
3540
{
3641
return 4;

0 commit comments

Comments
 (0)