提交 11154c25 编写于 作者: S Sam Harwell

Make sure to dispose of MockVisualStudioWorkspace

上级 637cb4c8
......@@ -21,29 +21,34 @@ namespace Microsoft.VisualStudio.LanguageServices.CSharp.UnitTests.CodeModel
public abstract class AbstractFileCodeElementTests : IDisposable
{
private readonly string _contents;
private Tuple<TestWorkspace, FileCodeModel> _workspaceAndCodeModel;
private (TestWorkspace workspace, VisualStudioWorkspace extraWorkspaceToDisposeButNotUse, FileCodeModel fileCodeModel)? _workspaceAndCodeModel;
public AbstractFileCodeElementTests(string contents)
{
_contents = contents;
}
public Tuple<TestWorkspace, FileCodeModel> WorkspaceAndCodeModel
public (TestWorkspace workspace, VisualStudioWorkspace extraWorkspaceToDisposeButNotUse, FileCodeModel fileCodeModel) WorkspaceAndCodeModel
{
get
{
return _workspaceAndCodeModel ?? (_workspaceAndCodeModel = CreateWorkspaceAndFileCodeModelAsync(_contents));
return _workspaceAndCodeModel ??= CreateWorkspaceAndFileCodeModelAsync(_contents);
}
}
protected TestWorkspace GetWorkspace()
{
return WorkspaceAndCodeModel.Item1;
return WorkspaceAndCodeModel.workspace;
}
private VisualStudioWorkspace GetExtraWorkspaceToDisposeButNotUse()
{
return WorkspaceAndCodeModel.extraWorkspaceToDisposeButNotUse;
}
protected FileCodeModel GetCodeModel()
{
return WorkspaceAndCodeModel.Item2;
return WorkspaceAndCodeModel.fileCodeModel;
}
protected Microsoft.CodeAnalysis.Solution GetCurrentSolution()
......@@ -55,7 +60,7 @@ protected Microsoft.CodeAnalysis.Project GetCurrentProject()
protected Microsoft.CodeAnalysis.Document GetCurrentDocument()
=> GetCurrentProject().Documents.Single();
protected static Tuple<TestWorkspace, EnvDTE.FileCodeModel> CreateWorkspaceAndFileCodeModelAsync(string file)
protected static (TestWorkspace workspace, VisualStudioWorkspace extraWorkspaceToDisposeButNotUse, FileCodeModel fileCodeModel) CreateWorkspaceAndFileCodeModelAsync(string file)
=> FileCodeModelTestHelpers.CreateWorkspaceAndFileCodeModel(file);
protected CodeElement GetCodeElement(params object[] path)
......@@ -79,6 +84,7 @@ protected CodeElement GetCodeElement(params object[] path)
public void Dispose()
{
GetExtraWorkspaceToDisposeButNotUse().Dispose();
GetWorkspace().Dispose();
}
......
......@@ -2,15 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Runtime.ExceptionServices;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.UnitTests;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel;
using Microsoft.VisualStudio.LanguageServices.Implementation.Interop;
using Microsoft.VisualStudio.LanguageServices.UnitTests;
......@@ -27,7 +24,7 @@ internal static class FileCodeModelTestHelpers
// finalizer complaining we didn't clean it up. Catching AVs is of course not safe, but this is balancing
// "probably not crash" as an improvement over "will crash when the finalizer throws."
[HandleProcessCorruptedStateExceptions]
public static Tuple<TestWorkspace, EnvDTE.FileCodeModel> CreateWorkspaceAndFileCodeModel(string file)
public static (TestWorkspace workspace, VisualStudioWorkspace extraWorkspaceToDisposeButNotUse, EnvDTE.FileCodeModel fileCodeModel) CreateWorkspaceAndFileCodeModel(string file)
{
var workspace = TestWorkspace.CreateCSharp(file, composition: VisualStudioTestCompositions.LanguageServices);
......@@ -59,7 +56,7 @@ internal static class FileCodeModelTestHelpers
var codeModel = FileCodeModel.Create(state, null, document, new MockTextManagerAdapter()).Handle;
return Tuple.Create(workspace, (EnvDTE.FileCodeModel)codeModel);
return (workspace, visualStudioWorkspaceMock, codeModel);
}
catch
{
......
......@@ -48,7 +48,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ObjectBrowser
Dim mockServiceProvider = New MockServiceProvider(mockComponentModel)
Dim libraryManager = CreateLibraryManager(mockServiceProvider, mockComponentModel, vsWorkspace)
result = New TestState(workspace, libraryManager)
result = New TestState(workspace, vsWorkspace, libraryManager)
Finally
If result Is Nothing Then
workspace.Dispose()
......
......@@ -15,15 +15,18 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ObjectBrowser
Implements IDisposable
Private ReadOnly _workspace As TestWorkspace
Private ReadOnly _visualStudioWorkspace As VisualStudioWorkspace
Private ReadOnly _libraryManager As AbstractObjectBrowserLibraryManager
Public Sub New(workspace As TestWorkspace, libraryManager As AbstractObjectBrowserLibraryManager)
Public Sub New(workspace As TestWorkspace, visualStudioWorkspace As VisualStudioWorkspace, libraryManager As AbstractObjectBrowserLibraryManager)
_workspace = workspace
_visualStudioWorkspace = visualStudioWorkspace
_libraryManager = libraryManager
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
_libraryManager.Dispose()
_visualStudioWorkspace.Dispose()
_workspace.Dispose()
End Sub
......
......@@ -86,6 +86,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.CodeModel
If Not Me._disposedValue Then
If disposing Then
VisualStudioWorkspace.Dispose()
Workspace.Dispose()
End If
End If
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册