public async Task TryListenForProgress_ProgressNotificationInvalid() { // Arrange var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(); var token = Guid.NewGuid().ToString(); using var cts = new CancellationTokenSource(); var onProgressNotifyAsyncCalled = false; using var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); // Act var listenerAdded = lspProgressListener.TryListenForProgress( token, onProgressNotifyAsync: async(value, ct) => { await Task.Delay(1); onProgressNotifyAsyncCalled = true; }, NotificationTimeout, cts.Token, out var onCompleted); // Note `Methods.ClientRegisterCapabilityName` is the wrong method, instead of `Methods.ProgressNotificationName` await lspProgressListener.ProcessProgressNotificationAsync(Methods.ClientRegisterCapabilityName, new JObject()); await onCompleted; // Assert Assert.False(onProgressNotifyAsyncCalled); }
public async Task TryListenForProgress_ProgressNotificationInvalid() { // Arrange var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(MockBehavior.Strict); var token = Guid.NewGuid().ToString(); using var cts = new CancellationTokenSource(); var onProgressNotifyAsyncCalled = false; using var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); // Act var listenerAdded = lspProgressListener.TryListenForProgress( token, onProgressNotifyAsync: (value, ct) => { onProgressNotifyAsyncCalled = true; return(Task.CompletedTask); }, delayAfterLastNotifyAsync: cancellationToken => Task.Delay(TimeSpan.FromSeconds(1), cancellationToken), cts.Token, out var onCompleted); // Note `Methods.ClientRegisterCapabilityName` is the wrong method, instead of `Methods.ProgressNotificationName` await lspProgressListener.ProcessProgressNotificationAsync(Methods.ClientRegisterCapabilityName, new JObject()).ConfigureAwait(false); await onCompleted.ConfigureAwait(false); // Assert Assert.False(onProgressNotifyAsyncCalled); }
private (LSPRequestInvoker, LSPProgressListener) MockServices(VSReferenceItem csharpLocation, out string token) { var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(); var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); var requestInvoker = new Mock <LSPRequestInvoker>(); token = Guid.NewGuid().ToString(); var parameterToken = new JObject { { "token", token }, { "value", JArray.FromObject(new[] { csharpLocation }) } }; requestInvoker.Setup(i => i.ReinvokeRequestOnServerAsync <TextDocumentPositionParams, VSReferenceItem[]>( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <TextDocumentPositionParams>(), It.IsAny <CancellationToken>())) .Callback <string, string, TextDocumentPositionParams, CancellationToken>((method, serverContentType, definitionParams, ct) => { Assert.Equal(Methods.TextDocumentReferencesName, method); Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType); _ = lspProgressListener.ProcessProgressNotificationAsync(Methods.ProgressNotificationName, parameterToken); }) .Returns(Task.FromResult(Array.Empty <VSReferenceItem>())); return(requestInvoker.Object, lspProgressListener); }
public async Task TryListenForProgress_MultipleProgressNotificationReported() { // Arrange const int NUM_NOTIFICATIONS = 50; var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(MockBehavior.Strict); var token = Guid.NewGuid().ToString(); using var cts = new CancellationTokenSource(); using var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); var parameterTokens = new List <JObject>(); for (var i = 0; i < NUM_NOTIFICATIONS; ++i) { parameterTokens.Add(new JObject { { Methods.ProgressNotificationTokenName, token }, { "value", i } }); } using var completedTokenSource = new CancellationTokenSource(); var receivedResults = new ConcurrentBag <int>(); Task OnProgressNotifyAsync(JToken value, CancellationToken ct) { receivedResults.Add(value.ToObject <int>()); if (receivedResults.Count == NUM_NOTIFICATIONS) { // All notifications received completedTokenSource.CancelAfter(0); } return(Task.CompletedTask); } // Act var listenerAdded = lspProgressListener.TryListenForProgress( token, onProgressNotifyAsync: OnProgressNotifyAsync, delayAfterLastNotifyAsync: cancellationToken => DelayAfterLastNotifyAsync(s_notificationTimeout, completedTokenSource.Token, cancellationToken), cts.Token, out var onCompleted); Parallel.ForEach(parameterTokens, parameterToken => _ = lspProgressListener.ProcessProgressNotificationAsync(Methods.ProgressNotificationName, parameterToken)); await onCompleted.ConfigureAwait(false); // Assert Assert.True(listenerAdded); var sortedResults = receivedResults.ToList(); sortedResults.Sort(); for (var i = 0; i < NUM_NOTIFICATIONS; ++i) { Assert.Equal(i, sortedResults[i]); } }
public async Task TryListenForProgress_MultipleProgressNotificationReported() { // Arrange const int NUM_NOTIFICATIONS = 50; var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(); var token = Guid.NewGuid().ToString(); using var cts = new CancellationTokenSource(); using var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); var parameterTokens = new List <JObject>(); for (var i = 0; i < NUM_NOTIFICATIONS; ++i) { parameterTokens.Add(new JObject { { Methods.ProgressNotificationTokenName, token }, { "value", i } }); } var receivedResults = new ConcurrentBag <int>(); Func <JToken, CancellationToken, Task> onProgressNotifyAsync = (value, ct) => { receivedResults.Add(value.ToObject <int>()); return(Task.CompletedTask); }; // Act var listenerAdded = lspProgressListener.TryListenForProgress( token, onProgressNotifyAsync: onProgressNotifyAsync, NotificationTimeout, cts.Token, out var onCompleted); Parallel.ForEach(parameterTokens, parameterToken => { _ = lspProgressListener.ProcessProgressNotificationAsync(Methods.ProgressNotificationName, parameterToken); }); await onCompleted; // Assert Assert.True(listenerAdded); var sortedResults = receivedResults.ToList(); sortedResults.Sort(); for (var i = 0; i < NUM_NOTIFICATIONS; ++i) { Assert.Equal(i, sortedResults[i]); } }
public async Task TryListenForProgress_SingleProgressNotificationReported() { // Arrange var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(MockBehavior.Strict); var token = Guid.NewGuid().ToString(); using var cts = new CancellationTokenSource(); var expectedValue = "abcxyz"; var parameterToken = new JObject { { Methods.ProgressNotificationTokenName, token }, { "value", JArray.FromObject(new[] { expectedValue }) } }; using var completedTokenSource = new CancellationTokenSource(); var onProgressNotifyAsyncCalled = false; Task OnProgressNotifyAsync(JToken value, CancellationToken ct) { var result = value.ToObject <string[]>(); var firstValue = Assert.Single(result); Assert.Equal(expectedValue, firstValue); onProgressNotifyAsyncCalled = true; completedTokenSource.CancelAfter(0); return(Task.CompletedTask); } using var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); // Act var listenerAdded = lspProgressListener.TryListenForProgress( token, onProgressNotifyAsync: OnProgressNotifyAsync, delayAfterLastNotifyAsync: cancellationToken => DelayAfterLastNotifyAsync(s_notificationTimeout, completedTokenSource.Token, cancellationToken), cts.Token, out var onCompleted); await lspProgressListener.ProcessProgressNotificationAsync(Methods.ProgressNotificationName, parameterToken).ConfigureAwait(false); await onCompleted.ConfigureAwait(false); // Assert Assert.True(onProgressNotifyAsyncCalled); }
public async Task TryListenForProgress_SingleProgressNotificationReported() { // Arrange var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(); var token = Guid.NewGuid().ToString(); using var cts = new CancellationTokenSource(); var expectedValue = "abcxyz"; var parameterToken = new JObject { { Methods.ProgressNotificationTokenName, token }, { "value", JArray.FromObject(new[] { expectedValue }) } }; var onProgressNotifyAsyncCalled = false; Func <JToken, CancellationToken, Task> onProgressNotifyAsync = (value, ct) => { var result = value.ToObject <string[]>(); var firstValue = Assert.Single(result); Assert.Equal(expectedValue, firstValue); onProgressNotifyAsyncCalled = true; return(Task.CompletedTask); }; using var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); // Act var listenerAdded = lspProgressListener.TryListenForProgress( token, onProgressNotifyAsync: onProgressNotifyAsync, NotificationTimeout, cts.Token, out var onCompleted); await lspProgressListener.ProcessProgressNotificationAsync(Methods.ProgressNotificationName, parameterToken); await onCompleted; // Assert Assert.True(onProgressNotifyAsyncCalled); }
public async Task HandleRequestAsync_HtmlProjection_InvokesHtmlLanguageServer() { // Arrange var lspFarEndpointCalled = false; var progressReported = false; var expectedUri1 = new Uri("C:/path/to/file1.razor"); var expectedUri2 = new Uri("C:/path/to/file2.razor"); var expectedLocation1 = GetReferenceItem(5, expectedUri1); var expectedLocation2 = GetReferenceItem(10, expectedUri2); var documentManager = new TestDocumentManager(); documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>()); var virtualHtmlUri1 = new Uri("C:/path/to/file1.razor__virtual.html"); var virtualHtmlUri2 = new Uri("C:/path/to/file2.razor__virtual.html"); var htmlLocation1 = GetReferenceItem(100, virtualHtmlUri1); var htmlLocation2 = GetReferenceItem(200, virtualHtmlUri2); var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(); using var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); var token = Guid.NewGuid().ToString(); var parameterToken = new JObject { { "token", token }, { "value", JArray.FromObject(new[] { htmlLocation1, htmlLocation2 }) } }; var requestInvoker = new Mock <LSPRequestInvoker>(); requestInvoker .Setup(r => r.ReinvokeRequestOnServerAsync <TextDocumentPositionParams, VSReferenceItem[]>(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <TextDocumentPositionParams>(), It.IsAny <CancellationToken>())) .Callback <string, string, TextDocumentPositionParams, CancellationToken>((method, serverContentType, definitionParams, ct) => { Assert.Equal(Methods.TextDocumentReferencesName, method); Assert.Equal(RazorLSPConstants.HtmlLSPContentTypeName, serverContentType); lspFarEndpointCalled = true; _ = lspProgressListener.ProcessProgressNotificationAsync(Methods.ProgressNotificationName, parameterToken); }) .Returns(Task.FromResult(Array.Empty <VSReferenceItem>())); var projectionResult = new ProjectionResult() { LanguageKind = RazorLanguageKind.Html, }; var projectionProvider = new Mock <LSPProjectionProvider>(); projectionProvider.Setup(p => p.GetProjectionAsync(It.IsAny <LSPDocumentSnapshot>(), It.IsAny <Position>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(projectionResult)); var remappingResult1 = new RazorMapToDocumentRangesResponse() { Ranges = new[] { expectedLocation1.Location.Range } }; var remappingResult2 = new RazorMapToDocumentRangesResponse() { Ranges = new[] { expectedLocation2.Location.Range } }; var documentMappingProvider = new Mock <LSPDocumentMappingProvider>(); documentMappingProvider .Setup(d => d.MapToDocumentRangesAsync(RazorLanguageKind.Html, It.IsAny <Uri>(), It.IsAny <Range[]>(), It.IsAny <CancellationToken>())) .Returns <RazorLanguageKind, Uri, Range[], CancellationToken>((languageKind, uri, ranges, ct) => Task.FromResult(uri.LocalPath.Contains("file1") ? remappingResult1 : remappingResult2)); var referencesHandler = new FindAllReferencesHandler(requestInvoker.Object, documentManager, projectionProvider.Object, documentMappingProvider.Object, lspProgressListener); referencesHandler.WaitForProgressNotificationTimeout = TestWaitForProgressNotificationTimeout; var progressToken = new ProgressWithCompletion <object>((val) => { var results = Assert.IsType <VSReferenceItem[]>(val); Assert.Collection(results, a => AssertVSReferenceItem(expectedLocation1, a), b => AssertVSReferenceItem(expectedLocation2, b)); progressReported = true; }); var referenceRequest = new ReferenceParams() { TextDocument = new TextDocumentIdentifier() { Uri = Uri }, Position = new Position(10, 5), PartialResultToken = progressToken }; // Act var result = await referencesHandler.HandleRequestAsync(referenceRequest, new ClientCapabilities(), token, CancellationToken.None).ConfigureAwait(false); // Assert Assert.True(lspFarEndpointCalled); Assert.True(progressReported); }
public async Task HandleRequestAsync_LargeProject_InvokesCSharpLanguageServer() { // Validates batching mechanism for the progress notification on large projects // Arrange var lspFarEndpointCalled = false; const int BATCH_SIZE = 10; const int NUM_BATCHES = 10; const int NUM_DOCUMENTS = BATCH_SIZE * NUM_BATCHES; const int MAPPING_OFFSET = 10; var expectedUris = new Uri[NUM_DOCUMENTS]; var virtualUris = new Uri[NUM_DOCUMENTS]; var expectedReferences = new VSReferenceItem[NUM_BATCHES][]; var csharpUnmappedReferences = new VSReferenceItem[NUM_BATCHES][]; var parameterTokens = new JObject[NUM_BATCHES]; var token = Guid.NewGuid().ToString(); var documentNumber = 0; for (var batch = 0; batch < NUM_BATCHES; ++batch) { expectedReferences[batch] = new VSReferenceItem[BATCH_SIZE]; csharpUnmappedReferences[batch] = new VSReferenceItem[BATCH_SIZE]; for (var documentInBatch = 0; documentInBatch < BATCH_SIZE; ++documentInBatch) { expectedUris[documentNumber] = new Uri($"C:/path/to/file{documentNumber}.razor"); virtualUris[documentNumber] = new Uri($"C:/path/to/file{documentNumber}.razor.g.cs"); expectedReferences[batch][documentInBatch] = GetReferenceItem(documentNumber, expectedUris[documentNumber]); var umappedOffset = documentNumber * MAPPING_OFFSET; csharpUnmappedReferences[batch][documentInBatch] = GetReferenceItem(umappedOffset, virtualUris[documentNumber]); documentNumber++; } parameterTokens[batch] = new JObject { { "token", token }, { "value", JArray.FromObject(csharpUnmappedReferences[batch]) } }; } var documentManager = new TestDocumentManager(); documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>()); var languageServiceBroker = Mock.Of <ILanguageServiceBroker2>(); using var lspProgressListener = new DefaultLSPProgressListener(languageServiceBroker); var requestInvoker = new Mock <LSPRequestInvoker>(); requestInvoker .Setup(r => r.ReinvokeRequestOnServerAsync <TextDocumentPositionParams, VSReferenceItem[]>(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <TextDocumentPositionParams>(), It.IsAny <CancellationToken>())) .Callback <string, string, TextDocumentPositionParams, CancellationToken>((method, serverContentType, definitionParams, ct) => { Assert.Equal(Methods.TextDocumentReferencesName, method); Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType); lspFarEndpointCalled = true; for (var i = 0; i < NUM_BATCHES; ++i) { _ = lspProgressListener.ProcessProgressNotificationAsync(Methods.ProgressNotificationName, parameterTokens[i]); } }) .Returns(Task.FromResult(Array.Empty <VSReferenceItem>())); var projectionResult = new ProjectionResult() { LanguageKind = RazorLanguageKind.CSharp, }; var projectionProvider = new Mock <LSPProjectionProvider>(); projectionProvider.Setup(p => p.GetProjectionAsync(It.IsAny <LSPDocumentSnapshot>(), It.IsAny <Position>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(projectionResult)); var documentMappingProvider = new Mock <LSPDocumentMappingProvider>(); documentMappingProvider .Setup(d => d.MapToDocumentRangesAsync(RazorLanguageKind.CSharp, It.IsAny <Uri>(), It.IsAny <Range[]>(), It.IsAny <CancellationToken>())) .Returns <RazorLanguageKind, Uri, Range[], CancellationToken>((languageKind, uri, ranges, ct) => { var unmappedPosition = ranges[0].Start.Line; var mappedPosition = unmappedPosition / MAPPING_OFFSET; var mappedRange = new Range() { Start = new Position(mappedPosition, mappedPosition), End = new Position(mappedPosition, mappedPosition) }; var response = new RazorMapToDocumentRangesResponse() { Ranges = new[] { mappedRange } }; return(Task.FromResult(response)); }); var referencesHandler = new FindAllReferencesHandler(requestInvoker.Object, documentManager, projectionProvider.Object, documentMappingProvider.Object, lspProgressListener); referencesHandler.WaitForProgressNotificationTimeout = TestWaitForProgressNotificationTimeout; var progressBatchesReported = new ConcurrentBag <VSReferenceItem[]>(); var progressToken = new ProgressWithCompletion <object>((val) => { var results = Assert.IsType <VSReferenceItem[]>(val); Assert.Equal(BATCH_SIZE, results.Length); progressBatchesReported.Add(results); }); var referenceRequest = new ReferenceParams() { TextDocument = new TextDocumentIdentifier() { Uri = Uri }, Position = new Position(10, 5), PartialResultToken = progressToken }; // Act var result = await referencesHandler.HandleRequestAsync(referenceRequest, new ClientCapabilities(), token, CancellationToken.None).ConfigureAwait(false); // Assert Assert.True(lspFarEndpointCalled); var sortedBatchesReported = progressBatchesReported.ToList(); sortedBatchesReported.Sort((VSReferenceItem[] a, VSReferenceItem[] b) => { var indexA = a[0].Location.Range.Start.Character; var indexB = b[0].Location.Range.Start.Character; return(indexA.CompareTo(indexB)); }); Assert.Equal(NUM_BATCHES, sortedBatchesReported.Count); for (var batch = 0; batch < NUM_BATCHES; ++batch) { for (var documentInBatch = 0; documentInBatch < BATCH_SIZE; ++documentInBatch) { AssertVSReferenceItem( expectedReferences[batch][documentInBatch], sortedBatchesReported[batch][documentInBatch]); } } }