提交 5458b371 编写于 作者: H Heejae Chang

rename some locals add async operation waiter

cleaned code little bit based on PR feedback
上级 6771d030
......@@ -10,7 +10,7 @@ namespace Microsoft.CodeAnalysis.Editor.Shared.Preview
{
internal class PreviewWorkspace : Workspace
{
private ISolutionCrawlerRegistrationService _workCoordinatorService;
private ISolutionCrawlerRegistrationService _registrationService;
public PreviewWorkspace()
: base(MefHostServices.DefaultHost, WorkspaceKind.Preview)
......@@ -33,10 +33,10 @@ public PreviewWorkspace(Solution solution)
public void EnableDiagnostic()
{
_workCoordinatorService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
if (_workCoordinatorService != null)
_registrationService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
if (_registrationService != null)
{
_workCoordinatorService.Register(this);
_registrationService.Register(this);
}
}
......@@ -90,10 +90,10 @@ protected override void Dispose(bool finalize)
{
base.Dispose(finalize);
if (_workCoordinatorService != null)
if (_registrationService != null)
{
_workCoordinatorService.Unregister(this);
_workCoordinatorService = null;
_registrationService.Unregister(this);
_registrationService = null;
}
this.ClearSolution();
......
......@@ -42,11 +42,11 @@ public void Test_TagSourceDiffer()
Assert.True(arg.Spans.First().Span.Contains(new Span(0, 1)));
};
var solutionWorkCoordinator = workspace.Services.GetService<ISolutionCrawlerRegistrationService>() as SolutionCrawlerRegistrationService;
var service = workspace.Services.GetService<ISolutionCrawlerRegistrationService>() as SolutionCrawlerRegistrationService;
var incrementalAnalyzers = ImmutableArray.Create(analyzerService.CreateIncrementalAnalyzer(workspace));
// test first update
solutionWorkCoordinator.WaitUntilCompletion_ForTestingPurposesOnly(workspace, incrementalAnalyzers);
service.WaitUntilCompletion_ForTestingPurposesOnly(workspace, incrementalAnalyzers);
diagnosticWaiter.CreateWaitTask().PumpingWait();
squiggleWaiter.CreateWaitTask().PumpingWait();
......@@ -58,7 +58,7 @@ public void Test_TagSourceDiffer()
var text = document.GetTextAsync().Result;
workspace.TryApplyChanges(document.WithText(text.WithChanges(new TextChange(new TextSpan(text.Length - 1, 1), string.Empty))).Project.Solution);
solutionWorkCoordinator.WaitUntilCompletion_ForTestingPurposesOnly(workspace, incrementalAnalyzers);
service.WaitUntilCompletion_ForTestingPurposesOnly(workspace, incrementalAnalyzers);
diagnosticWaiter.CreateWaitTask().PumpingWait();
squiggleWaiter.CreateWaitTask().PumpingWait();
......
......@@ -127,8 +127,8 @@ public void TestPreviewServices()
{
using (var previewWorkspace = new PreviewWorkspace(MefV1HostServices.Create(TestExportProvider.ExportProviderWithCSharpAndVisualBasic.AsExportProvider())))
{
var workcoordinatorService = previewWorkspace.Services.GetService<ISolutionCrawlerRegistrationService>();
Assert.True(workcoordinatorService is PreviewSolutionCrawlerRegistrationService);
var service = previewWorkspace.Services.GetService<ISolutionCrawlerRegistrationService>();
Assert.True(service is PreviewSolutionCrawlerRegistrationService);
var persistentService = previewWorkspace.Services.GetService<IPersistentStorageService>();
Assert.NotNull(persistentService);
......
......@@ -701,6 +701,7 @@ public void ProgressReporterTest()
var reporter = service.GetProgressReporter(workspace);
Assert.False(reporter.InProgress);
// set up events
bool started = false;
reporter.Started += (o, a) => { started = true; };
......@@ -710,13 +711,27 @@ public void ProgressReporterTest()
var registrationService = workspace.Services.GetService<ISolutionCrawlerRegistrationService>();
registrationService.Register(workspace);
// first mutation
workspace.OnSolutionAdded(solution);
Wait((SolutionCrawlerRegistrationService)registrationService, workspace);
registrationService.Unregister(workspace);
Assert.True(started);
Assert.True(stopped);
// reset
started = false;
stopped = false;
// second mutation
workspace.OnDocumentAdded(DocumentInfo.Create(DocumentId.CreateNewId(solution.Projects[0].Id), "D6"));
Wait((SolutionCrawlerRegistrationService)registrationService, workspace);
Assert.True(started);
Assert.True(stopped);
registrationService.Unregister(workspace);
}
}
......
......@@ -40,8 +40,8 @@ protected static IEnumerable<ITagSpan<IErrorTag>> GetErrorSpans(TestWorkspace wo
var foregroundService = new TestForegroundNotificationService();
var taggerSource = new DiagnosticsSquiggleTaggerProvider.TagSource(buffer, foregroundService, diagnosticService, optionsService, squiggleWaiter);
var solutionWorkCoordinator = workspace.Services.GetService<ISolutionCrawlerRegistrationService>() as SolutionCrawlerRegistrationService;
solutionWorkCoordinator.WaitUntilCompletion_ForTestingPurposesOnly(workspace, ImmutableArray.Create(analyzerService.CreateIncrementalAnalyzer(workspace)));
var service = workspace.Services.GetService<ISolutionCrawlerRegistrationService>() as SolutionCrawlerRegistrationService;
service.WaitUntilCompletion_ForTestingPurposesOnly(workspace, ImmutableArray.Create(analyzerService.CreateIncrementalAnalyzer(workspace)));
diagnosticWaiter.CreateWaitTask().PumpingWait();
squiggleWaiter.CreateWaitTask().PumpingWait();
......
......@@ -293,8 +293,8 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.UnitTests
Dim analyzerService = New DiagnosticAnalyzerService(compilerAnalyzersMap)
' CollectErrors generates interleaved background and foreground tasks.
Dim solutionWorkCoordinator = DirectCast(workspace.Services.GetService(Of ISolutionCrawlerRegistrationService)(), SolutionCrawlerRegistrationService)
solutionWorkCoordinator.WaitUntilCompletion_ForTestingPurposesOnly(workspace, SpecializedCollections.SingletonEnumerable(analyzerService.CreateIncrementalAnalyzer(workspace)).WhereNotNull().ToImmutableArray())
Dim service = DirectCast(workspace.Services.GetService(Of ISolutionCrawlerRegistrationService)(), SolutionCrawlerRegistrationService)
service.WaitUntilCompletion_ForTestingPurposesOnly(workspace, SpecializedCollections.SingletonEnumerable(analyzerService.CreateIncrementalAnalyzer(workspace)).WhereNotNull().ToImmutableArray())
Return analyzerService
End Function
......
......@@ -20,7 +20,7 @@ internal interface ISolutionCrawlerProgressReporter
event EventHandler Started;
/// <summary>
/// Raised when there is no more pending work in solutino crawler.
/// Raised when there is no more pending work in solution crawler.
/// </summary>
event EventHandler Stopped;
}
......
......@@ -3,6 +3,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.SolutionCrawler
......@@ -18,12 +19,23 @@ internal partial class SolutionCrawlerRegistrationService : ISolutionCrawlerRegi
/// </summary>
private class SolutionCrawlerProgressReporter : ISolutionCrawlerProgressReporter
{
private IAsynchronousOperationListener _listener;
// use event map and event queue so that we can guarantee snapshot and sequencial ordering of events from
// multiple consumer from possibly multiple threads
private readonly SimpleTaskQueue _eventQueue = new SimpleTaskQueue(TaskScheduler.Default);
private readonly EventMap _eventMap = new EventMap();
private readonly SimpleTaskQueue _eventQueue;
private readonly EventMap _eventMap;
private int _count;
private int _count = 0;
public SolutionCrawlerProgressReporter(IAsynchronousOperationListener listener)
{
_listener = listener;
_eventQueue = new SimpleTaskQueue(TaskScheduler.Default);
_eventMap = new EventMap();
_count = 0;
}
public bool InProgress
{
......@@ -63,7 +75,8 @@ public Task Start()
{
if (Interlocked.Increment(ref _count) == 1)
{
return RaiseStarted();
var asyncToken = _listener.BeginAsyncOperation("ProgressReportStart");
return RaiseStarted().CompletesAsyncOperation(asyncToken);
}
return SpecializedTasks.EmptyTask;
......@@ -73,7 +86,8 @@ public Task Stop()
{
if (Interlocked.Decrement(ref _count) == 0)
{
return RaiseStopped();
var asyncToken = _listener.BeginAsyncOperation("ProgressReportStop");
return RaiseStopped().CompletesAsyncOperation(asyncToken);
}
return SpecializedTasks.EmptyTask;
......
......@@ -29,11 +29,12 @@ internal partial class SolutionCrawlerRegistrationService : ISolutionCrawlerRegi
[ImportMany] IEnumerable<Lazy<IAsynchronousOperationListener, FeatureMetadata>> asyncListeners)
{
_gate = new object();
_progressReporter = new SolutionCrawlerProgressReporter();
_analyzerProviders = analyzerProviders.ToImmutableArray();
_documentWorkCoordinatorMap = new Dictionary<Workspace, WorkCoordinator>(ReferenceEqualityComparer.Instance);
_listener = new AggregateAsynchronousOperationListener(asyncListeners, FeatureAttribute.SolutionCrawler);
_progressReporter = new SolutionCrawlerProgressReporter(_listener);
}
public void Register(Workspace workspace)
......
......@@ -10,7 +10,7 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.Interactive
{
internal class InteractiveWorkspace : Workspace
{
private readonly ISolutionCrawlerRegistrationService _workCoordinatorService;
private readonly ISolutionCrawlerRegistrationService _registrationService;
internal InteractiveEvaluator Engine { get; private set; }
private SourceTextContainer _openTextContainer;
......@@ -22,14 +22,14 @@ internal InteractiveWorkspace(InteractiveEvaluator engine, HostServices hostServ
this.Engine = engine;
// register work coordinator for this workspace
_workCoordinatorService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
_workCoordinatorService.Register(this);
_registrationService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
_registrationService.Register(this);
}
protected override void Dispose(bool finalize)
{
// workspace is going away. unregister this workspace from work coordinator
_workCoordinatorService.Unregister(this, blockingShutdown: true);
_registrationService.Unregister(this, blockingShutdown: true);
base.Dispose(finalize);
}
......
......@@ -39,7 +39,7 @@ internal sealed partial class MiscellaneousFilesWorkspace : Workspace, IVsRunnin
private uint _runningDocumentTableEventsCookie;
// document worker coordinator
private ISolutionCrawlerRegistrationService _workCoordinatorService;
private ISolutionCrawlerRegistrationService _registrationService;
[ImportingConstructor]
public MiscellaneousFilesWorkspace(
......@@ -69,14 +69,14 @@ public void RegisterLanguage(Guid languageGuid, string languageName, string scri
internal void StartSolutionCrawler()
{
if (_workCoordinatorService == null)
if (_registrationService == null)
{
lock (this)
{
if (_workCoordinatorService == null)
if (_registrationService == null)
{
_workCoordinatorService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
_workCoordinatorService.Register(this);
_registrationService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
_registrationService.Register(this);
}
}
}
......@@ -84,14 +84,14 @@ internal void StartSolutionCrawler()
internal void StopSolutionCrawler()
{
if (_workCoordinatorService != null)
if (_registrationService != null)
{
lock (this)
{
if (_workCoordinatorService != null)
if (_registrationService != null)
{
_workCoordinatorService.Unregister(this, blockingShutdown: true);
_workCoordinatorService = null;
_registrationService.Unregister(this, blockingShutdown: true);
_registrationService = null;
}
}
}
......
......@@ -44,7 +44,7 @@ internal abstract class VisualStudioWorkspaceImpl : VisualStudioWorkspace
private VisualStudioProjectTracker _projectTracker;
// document worker coordinator
private ISolutionCrawlerRegistrationService _workCoordinatorService;
private ISolutionCrawlerRegistrationService _registrationService;
public VisualStudioWorkspaceImpl(
SVsServiceProvider serviceProvider,
......@@ -913,14 +913,14 @@ public override string GetFilePath(DocumentId documentId)
internal void StartSolutionCrawler()
{
if (_workCoordinatorService == null)
if (_registrationService == null)
{
lock (this)
{
if (_workCoordinatorService == null)
if (_registrationService == null)
{
_workCoordinatorService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
_workCoordinatorService.Register(this);
_registrationService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
_registrationService.Register(this);
}
}
}
......@@ -928,14 +928,14 @@ internal void StartSolutionCrawler()
internal void StopSolutionCrawler()
{
if (_workCoordinatorService != null)
if (_registrationService != null)
{
lock (this)
{
if (_workCoordinatorService != null)
if (_registrationService != null)
{
_workCoordinatorService.Unregister(this, blockingShutdown: true);
_workCoordinatorService = null;
_registrationService.Unregister(this, blockingShutdown: true);
_registrationService = null;
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册