From fcd84f242afb0799e2287181087fd2f27640711a Mon Sep 17 00:00:00 2001 From: Cheena Malhotra <13396919+cheenamalhotra@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:48:29 -0700 Subject: [PATCH] Fix signature help test to run synchronously (#2186) --- .../LanguageServices/LanguageService.cs | 71 +++++++++++-------- .../LanguageServer/LanguageServiceTests.cs | 6 +- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/LanguageService.cs b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/LanguageService.cs index 195d4284..b89d4a4d 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/LanguageService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/LanguageService.cs @@ -986,45 +986,54 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices } /// - /// Update the autocomplete metadata provider when the user connects to a database + /// Starts a Task to update the autocomplete metadata provider when the user connects to a database /// - /// + /// Connection info public Task UpdateLanguageServiceOnConnection(ConnectionInfo info) { return Task.Run(() => { - if (ConnectionService.IsDedicatedAdminConnection(info.ConnectionDetails)) - { - // Intellisense cannot be run on these connections as only 1 SqlConnection can be opened on them at a time - return; - } - ScriptParseInfo scriptInfo = GetScriptParseInfo(info.OwnerUri, createIfNotExists: true); - if (Monitor.TryEnter(scriptInfo.BuildingMetadataLock, LanguageService.OnConnectionWaitTimeout)) - { - try - { - scriptInfo.ConnectionKey = this.BindingQueue.AddConnectionContext(info, Constants.LanguageServiceFeature); - scriptInfo.IsConnected = this.BindingQueue.IsBindingContextConnected(scriptInfo.ConnectionKey); - } - catch (Exception ex) - { - Logger.Error("Unknown error in OnConnection " + ex.ToString()); - scriptInfo.IsConnected = false; - } - finally - { - // Set Metadata Build event to Signal state. - // (Tell Language Service that I am ready with Metadata Provider Object) - Monitor.Exit(scriptInfo.BuildingMetadataLock); - } - } - PrepopulateCommonMetadata(info, scriptInfo, this.BindingQueue); - - // Send a notification to signal that autocomplete is ready - ServiceHostInstance.SendEvent(IntelliSenseReadyNotification.Type, new IntelliSenseReadyParams() { OwnerUri = info.OwnerUri }); + DoUpdateLanguageServiceOnConnection(info); }); } + /// + /// Update the autocomplete metadata provider when the user connects to a database synchronously + /// + /// Connection info + public void DoUpdateLanguageServiceOnConnection(ConnectionInfo info) + { + if (ConnectionService.IsDedicatedAdminConnection(info.ConnectionDetails)) + { + // Intellisense cannot be run on these connections as only 1 SqlConnection can be opened on them at a time + return; + } + ScriptParseInfo scriptInfo = GetScriptParseInfo(info.OwnerUri, createIfNotExists: true); + if (Monitor.TryEnter(scriptInfo.BuildingMetadataLock, LanguageService.OnConnectionWaitTimeout)) + { + try + { + scriptInfo.ConnectionKey = this.BindingQueue.AddConnectionContext(info, Constants.LanguageServiceFeature); + scriptInfo.IsConnected = this.BindingQueue.IsBindingContextConnected(scriptInfo.ConnectionKey); + } + catch (Exception ex) + { + Logger.Error("Unknown error in OnConnection " + ex.ToString()); + scriptInfo.IsConnected = false; + } + finally + { + // Set Metadata Build event to Signal state. + // (Tell Language Service that I am ready with Metadata Provider Object) + Monitor.Exit(scriptInfo.BuildingMetadataLock); + } + } + PrepopulateCommonMetadata(info, scriptInfo, this.BindingQueue); + + // Send a notification to signal that autocomplete is ready + ServiceHostInstance.SendEvent(IntelliSenseReadyNotification.Type, new IntelliSenseReadyParams() { OwnerUri = info.OwnerUri }); + } + /// /// Preinitialize the parser and binder with common metadata. diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/LanguageServer/LanguageServiceTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/LanguageServer/LanguageServiceTests.cs index 9c8c5765..3aec39fe 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/LanguageServer/LanguageServiceTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/LanguageServer/LanguageServiceTests.cs @@ -10,7 +10,6 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Linq; -using System.Threading; using System.Threading.Tasks; using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility; using Microsoft.SqlTools.ServiceLayer.LanguageServices; @@ -220,7 +219,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.LanguageServer /// provide signature help. /// [Test] - public async Task GetSignatureHelpReturnsNotNullIfParseInfoInitialized() + public void GetSignatureHelpReturnsNotNullIfParseInfoInitialized() { // When we make a connection to a live database Hosting.ServiceHost.SendEventIgnoreExceptions = true; @@ -244,8 +243,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.LanguageServer // If the SQL has already been parsed var service = CreateLanguageService(result.ScriptFile); - await service.UpdateLanguageServiceOnConnection(result.ConnectionInfo); - Thread.Sleep(2000); + service.DoUpdateLanguageServiceOnConnection(result.ConnectionInfo); // We should get back a non-null ScriptParseInfo ScriptParseInfo? parseInfo = service.GetScriptParseInfo(result.ScriptFile.ClientUri);