-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathServiceHubServicesTests.cs
562 lines (441 loc) · 24.4 KB
/
ServiceHubServicesTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Licensed to the .NET Foundation under one or more agreements.
// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.DesignerAttribute;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.Extensions;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Remote;
using Microsoft.CodeAnalysis.Remote.Testing;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.TodoComments;
using Microsoft.CodeAnalysis.UnitTests;
using Nerdbank;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
namespace Roslyn.VisualStudio.Next.UnitTests.Remote
{
[UseExportProvider]
[Trait(Traits.Feature, Traits.Features.RemoteHost)]
public class ServiceHubServicesTests
{
private static TestWorkspace CreateWorkspace(Type[] additionalParts = null)
=> new TestWorkspace(composition: FeaturesTestCompositions.Features.WithTestHostParts(TestHost.OutOfProcess).AddParts(additionalParts));
private static Solution WithChangedOptionsFromRemoteWorkspace(Solution solution, RemoteWorkspace remoteWorkpace)
=> solution.WithChangedOptionsFrom(remoteWorkpace.Options);
[Fact]
public void TestRemoteHostCreation()
{
var remoteLogger = new TraceSource("inprocRemoteClient");
var testData = new RemoteHostTestData(new AssetStorage(), new RemoteWorkspaceManager(), isInProc: true);
var streams = FullDuplexStream.CreateStreams();
using var _ = new RemoteHostService(streams.Item1, new InProcRemoteHostClient.ServiceProvider(remoteLogger, testData));
}
[Fact]
public async Task TestRemoteHostSynchronize()
{
var code = @"class Test { void Method() { } }";
using var workspace = CreateWorkspace();
workspace.InitializeDocuments(LanguageNames.CSharp, files: new[] { code }, openDocuments: false);
using var client = await InProcRemoteHostClient.GetTestClientAsync(workspace).ConfigureAwait(false);
var solution = workspace.CurrentSolution;
await UpdatePrimaryWorkspace(client, solution);
await VerifyAssetStorageAsync(client, solution);
var remoteWorkpace = client.GetRemoteWorkspace();
solution = WithChangedOptionsFromRemoteWorkspace(solution, remoteWorkpace);
Assert.Equal(
await solution.State.GetChecksumAsync(CancellationToken.None),
await remoteWorkpace.CurrentSolution.State.GetChecksumAsync(CancellationToken.None));
}
[Fact]
public async Task TestRemoteHostTextSynchronize()
{
var code = @"class Test { void Method() { } }";
using var workspace = CreateWorkspace();
workspace.InitializeDocuments(LanguageNames.CSharp, files: new[] { code }, openDocuments: false);
var client = await InProcRemoteHostClient.GetTestClientAsync(workspace).ConfigureAwait(false);
var solution = workspace.CurrentSolution;
// sync base solution
await UpdatePrimaryWorkspace(client, solution);
await VerifyAssetStorageAsync(client, solution);
// get basic info
var oldDocument = solution.Projects.First().Documents.First();
var oldState = await oldDocument.State.GetStateChecksumsAsync(CancellationToken.None);
var oldText = await oldDocument.GetTextAsync();
// update text
var newText = oldText.WithChanges(new TextChange(TextSpan.FromBounds(0, 0), "/* test */"));
// sync
await client.RunRemoteAsync(
WellKnownServiceHubService.RemoteHost,
nameof(IRemoteHostService.SynchronizeTextAsync),
solution: null,
new object[] { oldDocument.Id, oldState.Text, newText.GetTextChanges(oldText) },
callbackTarget: null,
CancellationToken.None);
// apply change to solution
var newDocument = oldDocument.WithText(newText);
var newState = await newDocument.State.GetStateChecksumsAsync(CancellationToken.None);
// check that text already exist in remote side
Assert.True(client.TestData.AssetStorage.TryGetAsset<SourceText>(newState.Text, out var remoteText));
Assert.Equal(newText.ToString(), remoteText.ToString());
}
[Fact]
public async Task TestTodoComments()
{
var source = @"
// TODO: Test";
using var workspace = CreateWorkspace();
workspace.InitializeDocuments(LanguageNames.CSharp, files: new[] { source }, openDocuments: false);
using var client = await InProcRemoteHostClient.GetTestClientAsync(workspace).ConfigureAwait(false);
var remoteWorkspace = client.GetRemoteWorkspace();
// Ensure remote workspace is in sync with normal workspace.
var solution = workspace.CurrentSolution;
var assetProvider = await GetAssetProviderAsync(remoteWorkspace, solution);
var solutionChecksum = await solution.State.GetChecksumAsync(CancellationToken.None);
await remoteWorkspace.UpdatePrimaryBranchSolutionAsync(assetProvider, solutionChecksum, solution.WorkspaceVersion, CancellationToken.None);
var callback = new TodoCommentsListener();
var cancellationTokenSource = new CancellationTokenSource();
using var connection = await client.CreateConnectionAsync(
WellKnownServiceHubService.RemoteTodoCommentsService,
callback,
cancellationTokenSource.Token);
var invokeTask = connection.RunRemoteAsync(
nameof(IRemoteTodoCommentsService.ComputeTodoCommentsAsync),
solution: null,
arguments: Array.Empty<object>(),
cancellationTokenSource.Token);
var data = await callback.Data;
Assert.Equal(solution.Projects.Single().Documents.Single().Id, data.Item1);
Assert.Equal(1, data.Item2.Length);
var commentInfo = data.Item2[0];
Assert.Equal(new TodoCommentData
{
DocumentId = solution.Projects.Single().Documents.Single().Id,
Priority = 1,
Message = "TODO: Test",
MappedFilePath = null,
OriginalFilePath = "test1.cs",
OriginalLine = 2,
MappedLine = 2,
OriginalColumn = 3,
MappedColumn = 3,
}, commentInfo);
cancellationTokenSource.Cancel();
await invokeTask;
}
private class TodoCommentsListener : ITodoCommentsListener
{
private readonly TaskCompletionSource<(DocumentId, ImmutableArray<TodoCommentData>)> _dataSource
= new TaskCompletionSource<(DocumentId, ImmutableArray<TodoCommentData>)>();
public Task<(DocumentId, ImmutableArray<TodoCommentData>)> Data => _dataSource.Task;
public Task ReportTodoCommentDataAsync(DocumentId documentId, ImmutableArray<TodoCommentData> data, CancellationToken cancellationToken)
{
_dataSource.SetResult((documentId, data));
return Task.CompletedTask;
}
}
private static async Task<AssetProvider> GetAssetProviderAsync(Workspace remoteWorkspace, Solution solution, Dictionary<Checksum, object> map = null)
{
// make sure checksum is calculated
await solution.State.GetChecksumAsync(CancellationToken.None);
map ??= new Dictionary<Checksum, object>();
await solution.AppendAssetMapAsync(map, CancellationToken.None);
var sessionId = 0;
var storage = new AssetStorage();
storage.Initialize(new SimpleAssetSource(map));
return new AssetProvider(sessionId, storage, remoteWorkspace.Services.GetService<ISerializerService>());
}
[Fact]
public async Task TestDesignerAttributes()
{
var source = @"[System.ComponentModel.DesignerCategory(""Form"")] class Test { }";
using var workspace = CreateWorkspace();
workspace.InitializeDocuments(LanguageNames.CSharp, files: new[] { source }, openDocuments: false);
using var client = await InProcRemoteHostClient.GetTestClientAsync(workspace).ConfigureAwait(false);
var remoteWorkspace = client.GetRemoteWorkspace();
var cancellationTokenSource = new CancellationTokenSource();
var solution = workspace.CurrentSolution;
// Ensure remote workspace is in sync with normal workspace.
var assetProvider = await GetAssetProviderAsync(remoteWorkspace, solution);
var solutionChecksum = await solution.State.GetChecksumAsync(CancellationToken.None);
await remoteWorkspace.UpdatePrimaryBranchSolutionAsync(assetProvider, solutionChecksum, solution.WorkspaceVersion, CancellationToken.None);
var callback = new DesignerAttributeListener();
using var connection = await client.CreateConnectionAsync(
WellKnownServiceHubService.RemoteDesignerAttributeService,
callback,
cancellationTokenSource.Token);
var invokeTask = connection.RunRemoteAsync(
nameof(IRemoteDesignerAttributeService.StartScanningForDesignerAttributesAsync),
solution: null,
arguments: Array.Empty<object>(),
cancellationTokenSource.Token);
var infos = await callback.Infos;
Assert.Equal(1, infos.Length);
var info = infos[0];
Assert.Equal("Form", info.Category);
Assert.Equal(solution.Projects.Single().Documents.Single().Id, info.DocumentId);
cancellationTokenSource.Cancel();
await invokeTask;
}
private class DesignerAttributeListener : IDesignerAttributeListener
{
private readonly TaskCompletionSource<ImmutableArray<DesignerAttributeData>> _infosSource
= new TaskCompletionSource<ImmutableArray<DesignerAttributeData>>();
public Task<ImmutableArray<DesignerAttributeData>> Infos => _infosSource.Task;
public Task OnProjectRemovedAsync(ProjectId projectId, CancellationToken cancellationToken)
=> Task.CompletedTask;
public Task ReportDesignerAttributeDataAsync(ImmutableArray<DesignerAttributeData> infos, CancellationToken cancellationToken)
{
_infosSource.SetResult(infos);
return Task.CompletedTask;
}
}
[Fact]
public async Task TestUnknownProject()
{
var workspace = CreateWorkspace(new[] { typeof(NoCompilationLanguageServiceFactory) });
var solution = workspace.CurrentSolution.AddProject("unknown", "unknown", NoCompilationConstants.LanguageName).Solution;
using var client = await InProcRemoteHostClient.GetTestClientAsync(workspace).ConfigureAwait(false);
var remoteWorkspace = client.GetRemoteWorkspace();
await UpdatePrimaryWorkspace(client, solution);
await VerifyAssetStorageAsync(client, solution);
// Only C# and VB projects are supported in Remote workspace.
// See "RemoteSupportedLanguages.IsSupported"
Assert.Empty(remoteWorkspace.CurrentSolution.Projects);
solution = WithChangedOptionsFromRemoteWorkspace(solution, remoteWorkspace);
// No serializable remote options affect options checksum, so the checksums should match.
Assert.Equal(
await solution.State.GetChecksumAsync(CancellationToken.None),
await remoteWorkspace.CurrentSolution.State.GetChecksumAsync(CancellationToken.None));
solution = solution.RemoveProject(solution.ProjectIds.Single());
solution = WithChangedOptionsFromRemoteWorkspace(solution, remoteWorkspace);
Assert.Equal(
await solution.State.GetChecksumAsync(CancellationToken.None),
await remoteWorkspace.CurrentSolution.State.GetChecksumAsync(CancellationToken.None));
}
[Fact]
public async Task TestRemoteHostSynchronizeIncrementalUpdate()
{
using var workspace = CreateWorkspace();
using var client = await InProcRemoteHostClient.GetTestClientAsync(workspace).ConfigureAwait(false);
var remoteWorkspace = client.GetRemoteWorkspace();
var solution = Populate(workspace.CurrentSolution);
// verify initial setup
await UpdatePrimaryWorkspace(client, solution);
await VerifyAssetStorageAsync(client, solution);
solution = WithChangedOptionsFromRemoteWorkspace(solution, remoteWorkspace);
Assert.Equal(
await solution.State.GetChecksumAsync(CancellationToken.None),
await remoteWorkspace.CurrentSolution.State.GetChecksumAsync(CancellationToken.None));
// incrementally update
solution = await VerifyIncrementalUpdatesAsync(remoteWorkspace, client, solution, csAddition: " ", vbAddition: " ");
Assert.Equal(
await solution.State.GetChecksumAsync(CancellationToken.None),
await remoteWorkspace.CurrentSolution.State.GetChecksumAsync(CancellationToken.None));
// incrementally update
solution = await VerifyIncrementalUpdatesAsync(remoteWorkspace, client, solution, csAddition: "\r\nclass Addition { }", vbAddition: "\r\nClass VB\r\nEnd Class");
Assert.Equal(
await solution.State.GetChecksumAsync(CancellationToken.None),
await remoteWorkspace.CurrentSolution.State.GetChecksumAsync(CancellationToken.None));
}
[Fact]
public void TestRemoteWorkspaceCircularReferences()
{
using var tempRoot = new TempRoot();
var file = tempRoot.CreateDirectory().CreateFile("p1.dll");
file.CopyContentFrom(typeof(object).Assembly.Location);
var p1 = ProjectId.CreateNewId();
var p2 = ProjectId.CreateNewId();
var solutionInfo = SolutionInfo.Create(
SolutionId.CreateNewId(), VersionStamp.Create(), "",
new[]
{
ProjectInfo.Create(
p1, VersionStamp.Create(), "p1", "p1", LanguageNames.CSharp, outputFilePath: file.Path,
projectReferences: new [] { new ProjectReference(p2) }),
ProjectInfo.Create(
p2, VersionStamp.Create(), "p2", "p2", LanguageNames.CSharp,
metadataReferences: new [] { MetadataReference.CreateFromFile(file.Path) })
});
var languages = ImmutableHashSet.Create(LanguageNames.CSharp);
using var remoteWorkspace = new RemoteWorkspace(FeaturesTestCompositions.RemoteHost.GetHostServices(), WorkspaceKind.RemoteWorkspace);
var optionService = remoteWorkspace.Services.GetRequiredService<IOptionService>();
var options = new SerializableOptionSet(languages, optionService, ImmutableHashSet<IOption>.Empty, ImmutableDictionary<OptionKey, object>.Empty, ImmutableHashSet<OptionKey>.Empty);
// this shouldn't throw exception
remoteWorkspace.TrySetCurrentSolution(solutionInfo, workspaceVersion: 1, options, out var solution);
Assert.NotNull(solution);
}
private async Task<Solution> VerifyIncrementalUpdatesAsync(Workspace remoteWorkspace, RemoteHostClient client, Solution solution, string csAddition, string vbAddition)
{
var remoteSolution = remoteWorkspace.CurrentSolution;
var projectIds = solution.ProjectIds;
for (var i = 0; i < projectIds.Count; i++)
{
var projectName = $"Project{i}";
var project = solution.GetProject(projectIds[i]);
var documentIds = project.DocumentIds;
for (var j = 0; j < documentIds.Count; j++)
{
var documentName = $"Document{j}";
var currentSolution = UpdateSolution(solution, projectName, documentName, csAddition, vbAddition);
await UpdatePrimaryWorkspace(client, currentSolution);
var currentRemoteSolution = remoteWorkspace.CurrentSolution;
VerifyStates(remoteSolution, currentRemoteSolution, projectName, documentName);
solution = currentSolution;
remoteSolution = currentRemoteSolution;
Assert.Equal(
await solution.State.GetChecksumAsync(CancellationToken.None),
await remoteSolution.State.GetChecksumAsync(CancellationToken.None));
}
}
return solution;
}
private static void VerifyStates(Solution solution1, Solution solution2, string projectName, string documentName)
{
Assert.True(solution1.Workspace is RemoteWorkspace);
Assert.True(solution2.Workspace is RemoteWorkspace);
SetEqual(solution1.ProjectIds, solution2.ProjectIds);
var (project, document) = GetProjectAndDocument(solution1, projectName, documentName);
var projectId = project.Id;
var documentId = document.Id;
var projectIds = solution1.ProjectIds;
for (var i = 0; i < projectIds.Count; i++)
{
var currentProjectId = projectIds[i];
var projectStateShouldSame = projectId != currentProjectId;
Assert.Equal(projectStateShouldSame, object.ReferenceEquals(solution1.GetProject(currentProjectId).State, solution2.GetProject(currentProjectId).State));
if (!projectStateShouldSame)
{
SetEqual(solution1.GetProject(currentProjectId).DocumentIds, solution2.GetProject(currentProjectId).DocumentIds);
var documentIds = solution1.GetProject(currentProjectId).DocumentIds;
for (var j = 0; j < documentIds.Count; j++)
{
var currentDocumentId = documentIds[j];
var documentStateShouldSame = documentId != currentDocumentId;
Assert.Equal(documentStateShouldSame, object.ReferenceEquals(solution1.GetDocument(currentDocumentId).State, solution2.GetDocument(currentDocumentId).State));
}
}
}
}
private static async Task VerifyAssetStorageAsync(InProcRemoteHostClient client, Solution solution)
{
var map = await solution.GetAssetMapAsync(CancellationToken.None);
var storage = client.TestData.AssetStorage;
TestUtils.VerifyAssetStorage(map, storage);
}
private static Solution UpdateSolution(Solution solution, string projectName, string documentName, string csAddition, string vbAddition)
{
var (_, document) = GetProjectAndDocument(solution, projectName, documentName);
return document.WithText(GetNewText(document, csAddition, vbAddition)).Project.Solution;
}
private static SourceText GetNewText(Document document, string csAddition, string vbAddition)
{
if (document.Project.Language == LanguageNames.CSharp)
{
return SourceText.From(document.State.GetTextSynchronously(CancellationToken.None).ToString() + csAddition);
}
return SourceText.From(document.State.GetTextSynchronously(CancellationToken.None).ToString() + vbAddition);
}
private static (Project, Document) GetProjectAndDocument(Solution solution, string projectName, string documentName)
{
var project = solution.Projects.First(p => string.Equals(p.Name, projectName, StringComparison.OrdinalIgnoreCase));
var document = project.Documents.First(d => string.Equals(d.Name, documentName, StringComparison.OrdinalIgnoreCase));
return (project, document);
}
// make sure we always move remote workspace forward
private int _solutionVersion = 0;
private async Task UpdatePrimaryWorkspace(RemoteHostClient client, Solution solution)
{
await client.RunRemoteAsync(
WellKnownServiceHubService.RemoteHost,
nameof(IRemoteHostService.SynchronizePrimaryWorkspaceAsync),
solution,
new object[] { await solution.State.GetChecksumAsync(CancellationToken.None), _solutionVersion++ },
callbackTarget: null,
CancellationToken.None);
}
private static Solution Populate(Solution solution)
{
solution = AddProject(solution, LanguageNames.CSharp, new[]
{
"class CS { }",
"class CS2 { }"
}, new[]
{
"cs additional file content"
}, Array.Empty<ProjectId>());
solution = AddProject(solution, LanguageNames.VisualBasic, new[]
{
"Class VB\r\nEnd Class",
"Class VB2\r\nEnd Class"
}, new[]
{
"vb additional file content"
}, new ProjectId[] { solution.ProjectIds.First() });
solution = AddProject(solution, LanguageNames.CSharp, new[]
{
"class Top { }"
}, new[]
{
"cs additional file content"
}, solution.ProjectIds.ToArray());
solution = AddProject(solution, LanguageNames.CSharp, new[]
{
"class OrphanCS { }",
"class OrphanCS2 { }"
}, new[]
{
"cs additional file content",
"cs additional file content2"
}, Array.Empty<ProjectId>());
return solution;
}
private static Solution AddProject(Solution solution, string language, string[] documents, string[] additionalDocuments, ProjectId[] p2pReferences)
{
var projectName = $"Project{solution.ProjectIds.Count}";
var project = solution.AddProject(projectName, $"{projectName}.dll", language)
.AddMetadataReference(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
.AddAnalyzerReference(new AnalyzerFileReference(typeof(object).Assembly.Location, new TestAnalyzerAssemblyLoader()));
var projectId = project.Id;
solution = project.Solution;
for (var i = 0; i < documents.Length; i++)
{
var current = solution.GetProject(projectId);
solution = current.AddDocument($"Document{i}", SourceText.From(documents[i])).Project.Solution;
}
for (var i = 0; i < additionalDocuments.Length; i++)
{
var current = solution.GetProject(projectId);
solution = current.AddAdditionalDocument($"AdditionalDocument{i}", SourceText.From(additionalDocuments[i])).Project.Solution;
}
for (var i = 0; i < p2pReferences.Length; i++)
{
var current = solution.GetProject(projectId);
solution = current.AddProjectReference(new ProjectReference(p2pReferences[i])).Solution;
}
return solution;
}
private static void SetEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
{
var expectedSet = new HashSet<T>(expected);
var result = expected.Count() == actual.Count() && expectedSet.SetEquals(actual);
if (!result)
{
Assert.True(result);
}
}
}
}