Skip to content

Commit cf7e485

Browse files
Remove LegacyDispatcher from tooling tests
1 parent 60e8124 commit cf7e485

File tree

7 files changed

+328
-255
lines changed

7 files changed

+328
-255
lines changed

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DefaultGeneratedDocumentPublisherTest.cs

Lines changed: 87 additions & 71 deletions
Large diffs are not rendered by default.

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DefaultRazorProjectServiceTest.cs

Lines changed: 190 additions & 79 deletions
Large diffs are not rendered by default.

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Diagnostics;
2626

2727
public class RazorDiagnosticsPublisherTest : LanguageServerTestBase
2828
{
29-
private static readonly RazorDiagnostic[] s_emptyRazorDiagnostics = Array.Empty<RazorDiagnostic>();
30-
31-
private static readonly RazorDiagnostic[] s_singleRazorDiagnostic = new RazorDiagnostic[]
32-
{
29+
private static readonly RazorDiagnostic[] s_emptyRazorDiagnostics = [];
30+
private static readonly RazorDiagnostic[] s_singleRazorDiagnostic =
31+
[
3332
RazorDiagnosticFactory.CreateDirective_BlockDirectiveCannotBeImported("test")
34-
};
33+
];
3534

36-
private static readonly Diagnostic[] s_emptyCSharpDiagnostics = Array.Empty<Diagnostic>();
37-
private static readonly Diagnostic[] s_singleCSharpDiagnostic = new Diagnostic[]
38-
{
35+
private static readonly Diagnostic[] s_emptyCSharpDiagnostics = [];
36+
private static readonly Diagnostic[] s_singleCSharpDiagnostic =
37+
[
3938
new Diagnostic()
4039
{
4140
Code = "TestCode",
@@ -47,7 +46,7 @@ public class RazorDiagnosticsPublisherTest : LanguageServerTestBase
4746
End = new Position(0, 1)
4847
}
4948
}
50-
};
49+
];
5150

5251
private readonly ProjectSnapshotManager _projectManager;
5352
private readonly IDocumentSnapshot _closedDocument;
@@ -83,7 +82,7 @@ public RazorDiagnosticsPublisherTest(ITestOutputHelper testOutput)
8382
}
8483

8584
[Fact]
86-
public void DocumentProcessed_NewWorkQueued_RestartsTimer()
85+
public async Task DocumentProcessed_NewWorkQueued_RestartsTimer()
8786
{
8887
// Arrange
8988
Assert.NotNull(_openedDocument.FilePath);
@@ -110,19 +109,21 @@ public void DocumentProcessed_NewWorkQueued_RestartsTimer()
110109
var documentContextFactory = new TestDocumentContextFactory(_openedDocument.FilePath, codeDocument);
111110
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(Mock.Of<IRazorDocumentMappingService>(MockBehavior.Strict), LoggerFactory);
112111

113-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServerDocument, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory)
112+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServerDocument, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory)
114113
{
115114
BlockBackgroundWorkCompleting = new ManualResetEventSlim(initialState: true),
116115
NotifyBackgroundWorkCompleting = new ManualResetEventSlim(initialState: false),
117116
};
118117

119118
publisher.Initialize(_projectManager);
120-
publisher.DocumentProcessed(_testCodeDocument, processedOpenDocument);
119+
await RunOnDispatcherThreadAsync(() =>
120+
publisher.DocumentProcessed(_testCodeDocument, processedOpenDocument));
121121
Assert.True(publisher.NotifyBackgroundWorkCompleting.Wait(TimeSpan.FromSeconds(2)));
122122
publisher.NotifyBackgroundWorkCompleting.Reset();
123123

124124
// Act
125-
publisher.DocumentProcessed(_testCodeDocument, processedOpenDocument);
125+
await RunOnDispatcherThreadAsync(() =>
126+
publisher.DocumentProcessed(_testCodeDocument, processedOpenDocument));
126127
publisher.BlockBackgroundWorkCompleting.Set();
127128

128129
// Assert
@@ -206,7 +207,7 @@ public async Task PublishDiagnosticsAsync_NewDocumentDiagnosticsGetPublished(boo
206207
var documentMappingService = new RazorDocumentMappingService(filePathService, documentContextFactory, LoggerFactory);
207208
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(documentMappingService, LoggerFactory);
208209

209-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
210+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
210211
publisher.Initialize(_projectManager);
211212

212213
// Act
@@ -257,7 +258,7 @@ public async Task PublishDiagnosticsAsync_NewRazorDiagnosticsGetPublished()
257258
var documentContextFactory = new TestDocumentContextFactory(_openedDocument.FilePath, codeDocument);
258259
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(Mock.Of<IRazorDocumentMappingService>(MockBehavior.Strict), LoggerFactory);
259260

260-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
261+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
261262
publisher.PublishedRazorDiagnostics[processedOpenDocument.FilePath] = s_emptyRazorDiagnostics;
262263
publisher.Initialize(_projectManager);
263264

@@ -315,7 +316,7 @@ public async Task PublishDiagnosticsAsync_NewCSharpDiagnosticsGetPublished()
315316
var documentContextFactory = new TestDocumentContextFactory(_openedDocument.FilePath, codeDocument);
316317
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(Mock.Of<IRazorDocumentMappingService>(MockBehavior.Strict), LoggerFactory);
317318

318-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
319+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
319320
publisher.Initialize(_projectManager);
320321
await publisher.PublishDiagnosticsAsync(processedOpenDocument);
321322
arranging = false;
@@ -352,7 +353,7 @@ public async Task PublishDiagnosticsAsync_NoopsIfRazorDiagnosticsAreSameAsPrevio
352353
var documentMappingService = new RazorDocumentMappingService(filePathService, documentContextFactory, LoggerFactory);
353354
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(documentMappingService, LoggerFactory);
354355

355-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
356+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
356357
publisher.PublishedRazorDiagnostics[processedOpenDocument.FilePath] = s_singleRazorDiagnostic;
357358
publisher.Initialize(_projectManager);
358359

@@ -403,7 +404,7 @@ public async Task PublishDiagnosticsAsync_NoopsIfCSharpDiagnosticsAreSameAsPrevi
403404
var documentContextFactory = new TestDocumentContextFactory(_openedDocument.FilePath, codeDocument);
404405
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(Mock.Of<IRazorDocumentMappingService>(MockBehavior.Strict), LoggerFactory);
405406

406-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
407+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
407408
publisher.Initialize(_projectManager);
408409
await publisher.PublishDiagnosticsAsync(processedOpenDocument);
409410
arranging = false;
@@ -433,7 +434,7 @@ public void ClearClosedDocuments_ClearsDiagnosticsForClosedDocument()
433434
var documentContextFactory = new TestDocumentContextFactory();
434435
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(Mock.Of<IRazorDocumentMappingService>(MockBehavior.Strict), LoggerFactory);
435436

436-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
437+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
437438
Assert.NotNull(_closedDocument.FilePath);
438439
publisher.PublishedRazorDiagnostics[_closedDocument.FilePath] = s_singleRazorDiagnostic;
439440
publisher.PublishedCSharpDiagnostics[_closedDocument.FilePath] = s_singleCSharpDiagnostic;
@@ -454,7 +455,7 @@ public void ClearClosedDocuments_NoopsIfDocumentIsStillOpen()
454455
var documentContextFactory = new TestDocumentContextFactory();
455456
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(Mock.Of<IRazorDocumentMappingService>(MockBehavior.Strict), LoggerFactory);
456457

457-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
458+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
458459
Assert.NotNull(_openedDocument.FilePath);
459460
publisher.PublishedRazorDiagnostics[_openedDocument.FilePath] = s_singleRazorDiagnostic;
460461
publisher.PublishedCSharpDiagnostics[_openedDocument.FilePath] = s_singleCSharpDiagnostic;
@@ -472,7 +473,7 @@ public void ClearClosedDocuments_NoopsIfDocumentIsClosedButNoDiagnostics()
472473
var documentContextFactory = new TestDocumentContextFactory();
473474
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(Mock.Of<IRazorDocumentMappingService>(MockBehavior.Strict), LoggerFactory);
474475

475-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
476+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
476477
Assert.NotNull(_closedDocument.FilePath);
477478
publisher.PublishedRazorDiagnostics[_closedDocument.FilePath] = s_emptyRazorDiagnostics;
478479
publisher.PublishedCSharpDiagnostics[_closedDocument.FilePath] = s_emptyCSharpDiagnostics;
@@ -490,7 +491,7 @@ public void ClearClosedDocuments_RestartsTimerIfDocumentsStillOpen()
490491
var documentContextFactory = new TestDocumentContextFactory();
491492
var translateDiagnosticsService = new RazorTranslateDiagnosticsService(Mock.Of<IRazorDocumentMappingService>(MockBehavior.Strict), LoggerFactory);
492493

493-
using var publisher = new TestRazorDiagnosticsPublisher(LegacyDispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
494+
using var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory);
494495
Assert.NotNull(_closedDocument.FilePath);
495496
Assert.NotNull(_openedDocument.FilePath);
496497
publisher.PublishedRazorDiagnostics[_closedDocument.FilePath] = s_emptyRazorDiagnostics;

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/MonitorProjectConfigurationFilePathEndpointTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task Handle_Disposed_Noops()
3737
directoryPathResolver.Setup(resolver => resolver.Resolve())
3838
.Throws<XunitException>();
3939
var configurationFileEndpoint = new MonitorProjectConfigurationFilePathEndpoint(
40-
LegacyDispatcher,
40+
Dispatcher,
4141
directoryPathResolver.Object,
4242
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
4343
TestLanguageServerFeatureOptions.Instance,
@@ -62,7 +62,7 @@ public async Task Handle_ConfigurationFilePath_UntrackedMonitorNoops()
6262
directoryPathResolver.Setup(resolver => resolver.Resolve())
6363
.Throws<XunitException>();
6464
var configurationFileEndpoint = new MonitorProjectConfigurationFilePathEndpoint(
65-
LegacyDispatcher,
65+
Dispatcher,
6666
directoryPathResolver.Object,
6767
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
6868
TestLanguageServerFeatureOptions.Instance,
@@ -85,7 +85,7 @@ public async Task Handle_ConfigurationFilePath_TrackedMonitor_StopsMonitor()
8585
var detector = new TestFileChangeDetector();
8686
var configurationFileEndpoint = new TestMonitorProjectConfigurationFilePathEndpoint(
8787
() => detector,
88-
LegacyDispatcher,
88+
Dispatcher,
8989
_directoryPathResolver,
9090
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
9191
LoggerFactory);
@@ -117,7 +117,7 @@ public async Task Handle_InWorkspaceDirectory_Noops()
117117
var detector = new TestFileChangeDetector();
118118
var configurationFileEndpoint = new TestMonitorProjectConfigurationFilePathEndpoint(
119119
() => detector,
120-
LegacyDispatcher,
120+
Dispatcher,
121121
_directoryPathResolver,
122122
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
123123
LoggerFactory);
@@ -142,7 +142,7 @@ public async Task Handle_DuplicateMonitors_Noops()
142142
var detector = new TestFileChangeDetector();
143143
var configurationFileEndpoint = new TestMonitorProjectConfigurationFilePathEndpoint(
144144
() => detector,
145-
LegacyDispatcher,
145+
Dispatcher,
146146
_directoryPathResolver,
147147
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
148148
LoggerFactory);
@@ -169,7 +169,7 @@ public async Task Handle_ChangedConfigurationOutputPath_StartsWithNewPath()
169169
var detector = new TestFileChangeDetector();
170170
var configurationFileEndpoint = new TestMonitorProjectConfigurationFilePathEndpoint(
171171
() => detector,
172-
LegacyDispatcher,
172+
Dispatcher,
173173
_directoryPathResolver,
174174
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
175175
LoggerFactory);
@@ -201,7 +201,7 @@ public async Task Handle_ChangedConfigurationExternalToInternal_StopsWithoutRest
201201
var detector = new TestFileChangeDetector();
202202
var configurationFileEndpoint = new TestMonitorProjectConfigurationFilePathEndpoint(
203203
() => detector,
204-
LegacyDispatcher,
204+
Dispatcher,
205205
_directoryPathResolver,
206206
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
207207
LoggerFactory);
@@ -237,7 +237,7 @@ public async Task Handle_ProjectPublished()
237237
var detectors = new[] { projectOpenDebugDetector, releaseDetector, postPublishDebugDetector };
238238
var configurationFileEndpoint = new TestMonitorProjectConfigurationFilePathEndpoint(
239239
() => detectors[callCount++],
240-
LegacyDispatcher,
240+
Dispatcher,
241241
_directoryPathResolver,
242242
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
243243
LoggerFactory);
@@ -284,7 +284,7 @@ public async Task Handle_MultipleProjects_StartedAndStopped()
284284
var detectors = new[] { debug1Detector, debug2Detector, release1Detector };
285285
var configurationFileEndpoint = new TestMonitorProjectConfigurationFilePathEndpoint(
286286
() => detectors[callCount++],
287-
LegacyDispatcher,
287+
Dispatcher,
288288
_directoryPathResolver,
289289
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
290290
LoggerFactory);

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorFileSynchronizerTest.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
4+
using System.Threading.Tasks;
65
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
76
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
87
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
@@ -15,64 +14,68 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer;
1514
public class RazorFileSynchronizerTest(ITestOutputHelper testOutput) : LanguageServerTestBase(testOutput)
1615
{
1716
[Fact]
18-
public void RazorFileChanged_Added_AddsRazorDocument()
17+
public async Task RazorFileChanged_Added_AddsRazorDocument()
1918
{
2019
// Arrange
2120
var filePath = "/path/to/file.razor";
2221
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
2322
projectService.Setup(service => service.AddDocument(filePath)).Verifiable();
24-
var synchronizer = new RazorFileSynchronizer(LegacyDispatcher, projectService.Object);
23+
var synchronizer = new RazorFileSynchronizer(Dispatcher, projectService.Object);
2524

2625
// Act
27-
synchronizer.RazorFileChanged(filePath, RazorFileChangeKind.Added);
26+
await RunOnDispatcherThreadAsync(() =>
27+
synchronizer.RazorFileChanged(filePath, RazorFileChangeKind.Added));
2828

2929
// Assert
3030
projectService.VerifyAll();
3131
}
3232

3333
[Fact]
34-
public void RazorFileChanged_Added_AddsCSHTMLDocument()
34+
public async Task RazorFileChanged_Added_AddsCSHTMLDocument()
3535
{
3636
// Arrange
3737
var filePath = "/path/to/file.cshtml";
3838
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
3939
projectService.Setup(service => service.AddDocument(filePath)).Verifiable();
40-
var synchronizer = new RazorFileSynchronizer(LegacyDispatcher, projectService.Object);
40+
var synchronizer = new RazorFileSynchronizer(Dispatcher, projectService.Object);
4141

4242
// Act
43-
synchronizer.RazorFileChanged(filePath, RazorFileChangeKind.Added);
43+
await RunOnDispatcherThreadAsync(() =>
44+
synchronizer.RazorFileChanged(filePath, RazorFileChangeKind.Added));
4445

4546
// Assert
4647
projectService.VerifyAll();
4748
}
4849

4950
[Fact]
50-
public void RazorFileChanged_Removed_RemovesRazorDocument()
51+
public async Task RazorFileChanged_Removed_RemovesRazorDocument()
5152
{
5253
// Arrange
5354
var filePath = "/path/to/file.razor";
5455
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
5556
projectService.Setup(service => service.RemoveDocument(filePath)).Verifiable();
56-
var synchronizer = new RazorFileSynchronizer(LegacyDispatcher, projectService.Object);
57+
var synchronizer = new RazorFileSynchronizer(Dispatcher, projectService.Object);
5758

5859
// Act
59-
synchronizer.RazorFileChanged(filePath, RazorFileChangeKind.Removed);
60+
await RunOnDispatcherThreadAsync(() =>
61+
synchronizer.RazorFileChanged(filePath, RazorFileChangeKind.Removed));
6062

6163
// Assert
6264
projectService.VerifyAll();
6365
}
6466

6567
[Fact]
66-
public void RazorFileChanged_Removed_RemovesCSHTMLDocument()
68+
public async Task RazorFileChanged_Removed_RemovesCSHTMLDocument()
6769
{
6870
// Arrange
6971
var filePath = "/path/to/file.cshtml";
7072
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
7173
projectService.Setup(service => service.RemoveDocument(filePath)).Verifiable();
72-
var synchronizer = new RazorFileSynchronizer(LegacyDispatcher, projectService.Object);
74+
var synchronizer = new RazorFileSynchronizer(Dispatcher, projectService.Object);
7375

7476
// Act
75-
synchronizer.RazorFileChanged(filePath, RazorFileChangeKind.Removed);
77+
await RunOnDispatcherThreadAsync(() =>
78+
synchronizer.RazorFileChanged(filePath, RazorFileChangeKind.Removed));
7679

7780
// Assert
7881
projectService.VerifyAll();

0 commit comments

Comments
 (0)