未验证 提交 752fffb5 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #28516 from sharwell/improve-instance-factory

Code cleanup in VisualStudioInstanceFactory found during integration test work
......@@ -30,7 +30,10 @@ public sealed class VisualStudioInstanceFactory : IDisposable
/// </summary>
private VisualStudioInstance _currentlyRunningInstance;
private bool _hasCurrentlyActiveContext;
/// <summary>
/// Identifies the first time a Visual Studio instance is launched during an integration test run.
/// </summary>
private static bool _firstLaunch = true;
static VisualStudioInstanceFactory()
{
......@@ -103,12 +106,10 @@ private static Assembly AssemblyResolveHandler(object sender, ResolveEventArgs e
/// </summary>
public async Task<VisualStudioInstanceContext> GetNewOrUsedInstanceAsync(ImmutableHashSet<string> requiredPackageIds)
{
ThrowExceptionIfAlreadyHasActiveContext();
try
{
bool shouldStartNewInstance = ShouldStartNewInstance(requiredPackageIds);
await UpdateCurrentlyRunningInstanceAsync(requiredPackageIds, shouldStartNewInstance).ConfigureAwait(false);
await UpdateCurrentlyRunningInstanceAsync(requiredPackageIds, shouldStartNewInstance).ConfigureAwait(true);
return new VisualStudioInstanceContext(_currentlyRunningInstance, this);
}
......@@ -122,10 +123,6 @@ public async Task<VisualStudioInstanceContext> GetNewOrUsedInstanceAsync(Immutab
internal void NotifyCurrentInstanceContextDisposed(bool canReuse)
{
ThrowExceptionIfAlreadyHasActiveContext();
_hasCurrentlyActiveContext = false;
if (!canReuse)
{
_currentlyRunningInstance?.Close();
......@@ -145,14 +142,6 @@ private bool ShouldStartNewInstance(ImmutableHashSet<string> requiredPackageIds)
|| !_currentlyRunningInstance.IsRunning;
}
private void ThrowExceptionIfAlreadyHasActiveContext()
{
if (_hasCurrentlyActiveContext)
{
throw new Exception($"The previous integration test failed to call {nameof(VisualStudioInstanceContext)}.{nameof(Dispose)}. Ensure that test does that to ensure the Visual Studio instance is correctly cleaned up.");
}
}
/// <summary>
/// Starts up a new <see cref="VisualStudioInstance"/>, shutting down any instances that are already running.
/// </summary>
......@@ -181,7 +170,7 @@ private async Task UpdateCurrentlyRunningInstanceAsync(ImmutableHashSet<string>
}
// We wait until the DTE instance is up before we're good
dte = await IntegrationHelper.WaitForNotNullAsync(() => IntegrationHelper.TryLocateDteForProcess(hostProcess)).ConfigureAwait(false);
dte = await IntegrationHelper.WaitForNotNullAsync(() => IntegrationHelper.TryLocateDteForProcess(hostProcess)).ConfigureAwait(true);
}
else
{
......@@ -195,7 +184,7 @@ private async Task UpdateCurrentlyRunningInstanceAsync(ImmutableHashSet<string>
Debug.Assert(_currentlyRunningInstance != null);
hostProcess = _currentlyRunningInstance.HostProcess;
dte = await IntegrationHelper.WaitForNotNullAsync(() => IntegrationHelper.TryLocateDteForProcess(hostProcess)).ConfigureAwait(false);
dte = await IntegrationHelper.WaitForNotNullAsync(() => IntegrationHelper.TryLocateDteForProcess(hostProcess)).ConfigureAwait(true);
supportedPackageIds = _currentlyRunningInstance.SupportedPackageIds;
installationPath = _currentlyRunningInstance.InstallationPath;
......@@ -303,11 +292,16 @@ private static Process StartNewVisualStudioProcess(string installationPath)
{
var vsExeFile = Path.Combine(installationPath, @"Common7\IDE\devenv.exe");
// BUG: Currently building with /p:DeployExtension=true does not always cause the MEF cache to recompose...
// So, run clearcache and updateconfiguration to workaround https://devdiv.visualstudio.com/DevDiv/_workitems?id=385351.
Process.Start(vsExeFile, $"/clearcache {VsLaunchArgs}").WaitForExit();
Process.Start(vsExeFile, $"/updateconfiguration {VsLaunchArgs}").WaitForExit();
Process.Start(vsExeFile, $"/resetsettings General.vssettings /command \"File.Exit\" {VsLaunchArgs}").WaitForExit();
if (_firstLaunch)
{
// BUG: Currently building with /p:DeployExtension=true does not always cause the MEF cache to recompose...
// So, run clearcache and updateconfiguration to workaround https://devdiv.visualstudio.com/DevDiv/_workitems?id=385351.
Process.Start(vsExeFile, $"/clearcache {VsLaunchArgs}").WaitForExit();
Process.Start(vsExeFile, $"/updateconfiguration {VsLaunchArgs}").WaitForExit();
Process.Start(vsExeFile, $"/resetsettings General.vssettings /command \"File.Exit\" {VsLaunchArgs}").WaitForExit();
_firstLaunch = false;
}
// Make sure we kill any leftover processes spawned by the host
IntegrationHelper.KillProcess("DbgCLR");
......@@ -331,9 +325,6 @@ public void Dispose()
_currentlyRunningInstance?.Close();
_currentlyRunningInstance = null;
// We want to make sure everybody cleaned up their contexts by the end of everything
ThrowExceptionIfAlreadyHasActiveContext();
AppDomain.CurrentDomain.FirstChanceException -= FirstChanceExceptionHandler;
AppDomain.CurrentDomain.AssemblyResolve -= AssemblyResolveHandler;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册