diff --git a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/AutoCompleteService.cs b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/AutoCompleteService.cs index a390eae2..9eaa411c 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/AutoCompleteService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/AutoCompleteService.cs @@ -53,34 +53,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices new Dictionary(new ConnectionSummaryComparer()); private Object cachesLock = new Object(); // Used when we insert/remove something from the cache dictionary - private ISqlConnectionFactory factory; - private Object factoryLock = new Object(); - - /// - /// Internal for testing purposes only - /// - internal ISqlConnectionFactory ConnectionFactory - { - get - { - lock(factoryLock) - { - if(factory == null) - { - factory = new SqlConnectionFactory(); - } - } - return factory; - } - set - { - lock(factoryLock) - { - factory = value; - } - } - } - private ConnectionService connectionService = null; /// @@ -112,14 +84,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices ConnectionServiceInstance.RegisterOnDisconnectTask(RemoveAutoCompleteCacheUriReference); } - private async Task UpdateAutoCompleteCache(ConnectionInfo connectionInfo) - { - if (connectionInfo != null) - { - await UpdateAutoCompleteCache(connectionInfo.ConnectionDetails); - } - } - /// /// Intellisense cache count access for testing. /// @@ -158,21 +122,24 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices /// /// Update the cached autocomplete candidate list when the user connects to a database /// - /// - public async Task UpdateAutoCompleteCache(ConnectionDetails details) + /// + public async Task UpdateAutoCompleteCache(ConnectionInfo info) { - IntellisenseCache cache; - lock(cachesLock) + if (info != null) { - if(!caches.TryGetValue(details, out cache)) + IntellisenseCache cache; + lock(cachesLock) { - cache = new IntellisenseCache(ConnectionFactory, details); - caches[cache.DatabaseInfo] = cache; + if(!caches.TryGetValue(info.ConnectionDetails, out cache)) + { + cache = new IntellisenseCache(info.Factory, info.ConnectionDetails); + caches[cache.DatabaseInfo] = cache; + } + cache.ReferenceCount++; } - cache.ReferenceCount++; + + await cache.UpdateCache(); } - - await cache.UpdateCache(); } /// diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/LanguageServer/LanguageServiceTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/LanguageServer/LanguageServiceTests.cs index 09706f6c..9462d384 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/LanguageServer/LanguageServiceTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/LanguageServer/LanguageServiceTests.cs @@ -181,12 +181,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny())) .Returns(CreateMockDbConnection(new[] {data})); - var connectionService = TestObjects.GetTestConnectionService(); + var connectionService = new ConnectionService(mockFactory.Object); var autocompleteService = new AutoCompleteService(); autocompleteService.ConnectionServiceInstance = connectionService; autocompleteService.InitializeService(Microsoft.SqlTools.ServiceLayer.Hosting.ServiceHost.Instance); - - autocompleteService.ConnectionFactory = mockFactory.Object; // Open a connection // The cache should get updated as part of this @@ -216,11 +214,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices [Fact] public void OnlyOneCacheIsCreatedForTwoDocumentsWithSameConnection() { - var connectionService = TestObjects.GetTestConnectionService(); + var connectionService = new ConnectionService(TestObjects.GetTestSqlConnectionFactory()); var autocompleteService = new AutoCompleteService(); autocompleteService.ConnectionServiceInstance = connectionService; autocompleteService.InitializeService(Microsoft.SqlTools.ServiceLayer.Hosting.ServiceHost.Instance); - autocompleteService.ConnectionFactory = TestObjects.GetTestSqlConnectionFactory(); // Open two connections ConnectParams connectionRequest1 = TestObjects.GetTestConnectionParams(); @@ -243,6 +240,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices [Fact] public void TwoCachesAreCreatedForTwoDocumentsWithDifferentConnections() { + const string testDb1 = "my_db"; + const string testDb2 = "my_other_db"; + // Result set for the query of database tables Dictionary[] data1 = { @@ -258,21 +258,21 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices }; var mockFactory = new Mock(); - mockFactory.SetupSequence(factory => factory.CreateSqlConnection(It.IsAny())) - .Returns(CreateMockDbConnection(new[] {data1})) + mockFactory.Setup(factory => factory.CreateSqlConnection(It.Is(x => x.Contains(testDb1)))) + .Returns(CreateMockDbConnection(new[] {data1})); + mockFactory.Setup(factory => factory.CreateSqlConnection(It.Is(x => x.Contains(testDb2)))) .Returns(CreateMockDbConnection(new[] {data2})); - var connectionService = TestObjects.GetTestConnectionService(); + var connectionService = new ConnectionService(mockFactory.Object); var autocompleteService = new AutoCompleteService(); autocompleteService.ConnectionServiceInstance = connectionService; autocompleteService.InitializeService(Microsoft.SqlTools.ServiceLayer.Hosting.ServiceHost.Instance); - - autocompleteService.ConnectionFactory = mockFactory.Object; // Open connections // The cache should get updated as part of this ConnectParams connectionRequest = TestObjects.GetTestConnectionParams(); connectionRequest.OwnerUri = "file:///my/first/sql/file.sql"; + connectionRequest.Connection.DatabaseName = testDb1; var connectionResult = connectionService.Connect(connectionRequest); Assert.NotEmpty(connectionResult.ConnectionId); @@ -282,7 +282,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices // Open second connection ConnectParams connectionRequest2 = TestObjects.GetTestConnectionParams(); connectionRequest2.OwnerUri = "file:///my/second/sql/file.sql"; - connectionRequest2.Connection.DatabaseName = "my_other_db"; + connectionRequest2.Connection.DatabaseName = testDb2; var connectionResult2 = connectionService.Connect(connectionRequest2); Assert.NotEmpty(connectionResult2.ConnectionId);