Fixing the bug with connections on database make restore fail (#473)

* closing the connections that don't need to be open and keeping track of the connections that should stay open
This commit is contained in:
Leila Lali
2017-10-05 20:06:31 -07:00
committed by GitHub
parent 7444939335
commit f09b9f4c30
33 changed files with 1045 additions and 287 deletions

View File

@@ -7,11 +7,9 @@ using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.Extensibility;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.ObjectExplorer;
using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Contracts;
@@ -20,6 +18,7 @@ using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel;
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
using Moq;
using Xunit;
using Microsoft.SqlTools.ServiceLayer.Connection;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
{
@@ -33,10 +32,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
private ConnectionDetails defaultConnectionDetails;
private ConnectionCompleteParams defaultConnParams;
private string fakeConnectionString = "Data Source=server;Initial Catalog=database;Integrated Security=False;User Id=user";
private ServerConnection serverConnection = null;
public NodeTests()
{
defaultServerInfo = TestObjects.GetTestServerInfo();
serverConnection = new ServerConnection(new SqlConnection(fakeConnectionString));
defaultConnectionDetails = new ConnectionDetails()
{
@@ -59,15 +60,15 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
[Fact]
public void ServerNodeConstructorValidatesFields()
{
Assert.Throws<ArgumentNullException>(() => new ServerNode(null, ServiceProvider));
Assert.Throws<ArgumentNullException>(() => new ServerNode(defaultConnParams, null));
Assert.Throws<ArgumentNullException>(() => new ServerNode(null, ServiceProvider, serverConnection));
Assert.Throws<ArgumentNullException>(() => new ServerNode(defaultConnParams, null, serverConnection));
}
[Fact]
public void ServerNodeConstructorShouldSetValuesCorrectly()
{
// Given a server node with valid inputs
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider);
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider, serverConnection);
// Then expect all fields set correctly
Assert.False(node.IsAlwaysLeaf, "Server node should never be a leaf");
Assert.Equal(defaultConnectionDetails.ServerName, node.NodeValue);
@@ -99,7 +100,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
OwnerUri = defaultOwnerUri
};
// When querying label
string label = new ServerNode(connParams, ServiceProvider).Label;
string label = new ServerNode(connParams, ServiceProvider, serverConnection).Label;
// Then only server name and version shown
string expectedLabel = defaultConnectionDetails.ServerName + " (SQL Server " + defaultServerInfo.ServerVersion + ")";
Assert.Equal(expectedLabel, label);
@@ -111,7 +112,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
defaultServerInfo.IsCloud = true;
// Given a server node for a cloud DB, with master name
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider);
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider, serverConnection);
// Then expect label to not include db name
string expectedLabel = defaultConnectionDetails.ServerName + " (SQL Server " + defaultServerInfo.ServerVersion + " - "
+ defaultConnectionDetails.UserName + ")";
@@ -120,7 +121,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
// But given a server node for a cloud DB that's not master
defaultConnectionDetails.DatabaseName = "NotMaster";
defaultConnParams.ConnectionSummary.DatabaseName = defaultConnectionDetails.DatabaseName;
node = new ServerNode(defaultConnParams, ServiceProvider);
node = new ServerNode(defaultConnParams, ServiceProvider, serverConnection);
// Then expect label to include db name
expectedLabel = defaultConnectionDetails.ServerName + " (SQL Server " + defaultServerInfo.ServerVersion + " - "
@@ -132,7 +133,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public void ToNodeInfoIncludeAllFields()
{
// Given a server connection
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider);
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider, serverConnection);
// When converting to NodeInfo
NodeInfo info = node.ToNodeInfo();
// Then all fields should match
@@ -204,7 +205,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public void ServerNodeContextShouldIncludeServer()
{
// given a successful Server creation
SetupAndRegisterTestConnectionService();
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
ServerNode node = SetupServerNodeWithServer(smoServer);
@@ -223,10 +223,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public void ServerNodeContextShouldSetErrorMessageIfSqlConnectionIsNull()
{
// given a connectionInfo with no SqlConnection to use for queries
ConnectionService connService = SetupAndRegisterTestConnectionService();
connService.OwnerToConnectionMap.Remove(defaultOwnerUri);
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
Server smoServer = null;
ServerNode node = SetupServerNodeWithServer(smoServer);
// When I get the context for a ServerNode
@@ -234,17 +232,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
// Then I expect it to be in an error state
Assert.Null(context);
Assert.Equal(
string.Format(CultureInfo.CurrentCulture, SR.ServerNodeConnectionError, defaultConnectionDetails.ServerName),
node.ErrorStateMessage);
}
[Fact]
public void ServerNodeContextShouldSetErrorMessageIfConnFailureExceptionThrown()
{
// given a connectionInfo with no SqlConnection to use for queries
SetupAndRegisterTestConnectionService();
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
string expectedMsg = "ConnFailed!";
ServerNode node = SetupServerNodeWithExceptionCreator(new ConnectionFailureException(expectedMsg));
@@ -263,8 +256,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public void ServerNodeContextShouldSetErrorMessageIfExceptionThrown()
{
// given a connectionInfo with no SqlConnection to use for queries
SetupAndRegisterTestConnectionService();
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
string expectedMsg = "Failed!";
ServerNode node = SetupServerNodeWithExceptionCreator(new Exception(expectedMsg));
@@ -283,7 +274,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public void QueryContextShouldNotCallOpenOnAlreadyOpenConnection()
{
// given a server connection that will state its connection is open
SetupAndRegisterTestConnectionService();
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
Mock<SmoWrapper> wrapper = SetupSmoWrapperForIsOpenTest(smoServer, isOpen: true);
@@ -301,7 +291,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
{
Mock<SmoWrapper> wrapper = new Mock<SmoWrapper>();
int count = 0;
wrapper.Setup(c => c.CreateServer(It.IsAny<SqlConnection>()))
wrapper.Setup(c => c.CreateServer(It.IsAny<ServerConnection>()))
.Returns(() => smoServer);
wrapper.Setup(c => c.IsConnectionOpen(It.IsAny<Server>()))
.Returns(() => isOpen);
@@ -315,7 +305,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public void QueryContextShouldReopenClosedConnectionWhenGettingServer()
{
// given a server connection that will state its connection is closed
SetupAndRegisterTestConnectionService();
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
Mock<SmoWrapper> wrapper = SetupSmoWrapperForIsOpenTest(smoServer, isOpen: false);
@@ -333,7 +322,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public void QueryContextShouldReopenClosedConnectionWhenGettingParent()
{
// given a server connection that will state its connection is closed
SetupAndRegisterTestConnectionService();
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
Mock<SmoWrapper> wrapper = SetupSmoWrapperForIsOpenTest(smoServer, isOpen: false);
@@ -362,7 +350,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
private ServerNode SetupServerNodeWithServer(Server smoServer)
{
Mock<SmoWrapper> creator = new Mock<SmoWrapper>();
creator.Setup(c => c.CreateServer(It.IsAny<SqlConnection>()))
creator.Setup(c => c.CreateServer(It.IsAny<ServerConnection>()))
.Returns(() => smoServer);
creator.Setup(c => c.IsConnectionOpen(It.IsAny<Server>()))
.Returns(() => true);
@@ -373,7 +361,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
private ServerNode SetupServerNodeWithExceptionCreator(Exception ex)
{
Mock<SmoWrapper> creator = new Mock<SmoWrapper>();
creator.Setup(c => c.CreateServer(It.IsAny<SqlConnection>()))
creator.Setup(c => c.CreateServer(It.IsAny<ServerConnection>()))
.Throws(ex);
creator.Setup(c => c.IsConnectionOpen(It.IsAny<Server>()))
.Returns(() => false);
@@ -384,7 +372,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
private ServerNode SetupServerNodeWithCreator(SmoWrapper creator)
{
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider);
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider, new ServerConnection(new SqlConnection(fakeConnectionString)));
node.SmoWrapper = creator;
return node;
}

View File

@@ -16,6 +16,8 @@ using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes;
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
using Moq;
using Xunit;
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlServer.Management.Common;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
{
@@ -25,12 +27,29 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
private ObjectExplorerService service;
private Mock<ConnectionService> connectionServiceMock;
private Mock<IProtocolEndpoint> serviceHostMock;
string fakeConnectionString = "Data Source=server;Initial Catalog=database;Integrated Security=False;User Id=user";
private static ConnectionDetails details = new ConnectionDetails()
{
UserName = "user",
Password = "password",
DatabaseName = "msdb",
ServerName = "serverName"
};
ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details);
ConnectedBindingQueue connectedBindingQueue;
public ObjectExplorerServiceTests()
{
connectionServiceMock = new Mock<ConnectionService>();
serviceHostMock = new Mock<IProtocolEndpoint>();
service = CreateOEService(connectionServiceMock.Object);
connectionServiceMock.Setup(x => x.RegisterConnectedQueue(It.IsAny<string>(), It.IsAny<IConnectedBindingQueue>()));
service.InitializeService(serviceHostMock.Object);
ConnectedBindingContext connectedBindingContext = new ConnectedBindingContext();
connectedBindingContext.ServerConnection = new ServerConnection(new SqlConnection(fakeConnectionString));
connectedBindingQueue = new ConnectedBindingQueue(false);
connectedBindingQueue.BindingContextMap.Add($"{details.ServerName}_{details.DatabaseName}_{details.UserName}_NULL", connectedBindingContext);
service.ConnectedBindingQueue = connectedBindingQueue;
}
[Fact]
@@ -210,14 +229,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
private async Task<SessionCreatedParameters> CreateSession()
{
ConnectionDetails details = new ConnectionDetails()
{
UserName = "user",
Password = "password",
DatabaseName = "msdb",
ServerName = "serverName"
};
SessionCreatedParameters sessionResult = null;
serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => sessionResult = p);
CreateSessionResponse result = default(CreateSessionResponse);
@@ -226,8 +237,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
connectionServiceMock.Setup(c => c.Connect(It.IsAny<ConnectParams>()))
.Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details)));
ConnectionInfo connectionInfo = new ConnectionInfo(null, null, null);
string fakeConnectionString = "Data Source=server;Initial Catalog=database;Integrated Security=False;User Id=user";
ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details);
connectionInfo.AddConnection("Default", new SqlConnection(fakeConnectionString));
connectionServiceMock.Setup((c => c.TryFindConnection(It.IsAny<string>(), out connectionInfo))).
OutCallback((string t, out ConnectionInfo v) => v = connectionInfo)
@@ -343,7 +353,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
connectionServiceMock.Setup(c => c.Connect(It.IsAny<ConnectParams>()))
.Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details)));
ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details);
connectionInfo.AddConnection("Default", new SqlConnection(fakeConnectionString));
connectionServiceMock.Setup((c => c.TryFindConnection(It.IsAny<string>(), out connectionInfo))).
OutCallback((string t, out ConnectionInfo v) => v = connectionInfo)
.Returns(true);
// when creating a new session
// then expect the create session request to return false
await RunAndVerify<CreateSessionResponse, SessionCreatedParameters>(