Reduce unit test time by 1 minute (#607)

ObjectExplorerTests were adding 1 minute to total test time due to
unnecessarily trying and failing to connect to a non-existent server.
Mocking out this call saves 1minute, reducing test time in half
This commit is contained in:
Kevin Cunnane
2018-04-30 22:15:38 -07:00
committed by GitHub
parent dca33d768d
commit e02592460b
2 changed files with 27 additions and 3 deletions

View File

@@ -32,6 +32,17 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
int? waitForLockTimeout = null); int? waitForLockTimeout = null);
} }
public class SqlConnectionOpener
{
/// <summary>
/// Virtual method used to support mocking and testing
/// </summary>
public virtual SqlConnection OpenSqlConnection(ConnectionInfo connInfo, string featureName)
{
return ConnectionService.OpenSqlConnection(connInfo, featureName);
}
}
/// <summary> /// <summary>
/// ConnectedBindingQueue class for processing online binding requests /// ConnectedBindingQueue class for processing online binding requests
/// </summary> /// </summary>
@@ -46,6 +57,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
/// it's much cheaper to not construct these objects if not needed /// it's much cheaper to not construct these objects if not needed
/// </summary> /// </summary>
private bool needsMetadata; private bool needsMetadata;
private SqlConnectionOpener connectionOpener;
/// <summary> /// <summary>
/// Gets the current settings /// Gets the current settings
@@ -63,6 +75,13 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
public ConnectedBindingQueue(bool needsMetadata) public ConnectedBindingQueue(bool needsMetadata)
{ {
this.needsMetadata = needsMetadata; this.needsMetadata = needsMetadata;
this.connectionOpener = new SqlConnectionOpener();
}
// For testing purposes only
internal void SetConnectionOpener(SqlConnectionOpener opener)
{
this.connectionOpener = opener;
} }
/// <summary> /// <summary>
@@ -179,7 +198,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
try try
{ {
bindingContext.BindingLock.Reset(); bindingContext.BindingLock.Reset();
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo, featureName); SqlConnection sqlConn = connectionOpener.OpenSqlConnection(connInfo, featureName);
// populate the binding context to work with the SMO metadata provider // populate the binding context to work with the SMO metadata provider
bindingContext.ServerConnection = new ServerConnection(sqlConn); bindingContext.ServerConnection = new ServerConnection(sqlConn);

View File

@@ -40,6 +40,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details); ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details);
ConnectedBindingQueue connectedBindingQueue; ConnectedBindingQueue connectedBindingQueue;
Mock<SqlConnectionOpener> mockConnectionOpener;
public ObjectExplorerServiceTests() public ObjectExplorerServiceTests()
{ {
connectionServiceMock = new Mock<ConnectionService>(); connectionServiceMock = new Mock<ConnectionService>();
@@ -51,6 +52,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
connectedBindingContext.ServerConnection = new ServerConnection(new SqlConnection(fakeConnectionString)); connectedBindingContext.ServerConnection = new ServerConnection(new SqlConnection(fakeConnectionString));
connectedBindingQueue = new ConnectedBindingQueue(false); connectedBindingQueue = new ConnectedBindingQueue(false);
connectedBindingQueue.BindingContextMap.Add($"{details.ServerName}_{details.DatabaseName}_{details.UserName}_NULL", connectedBindingContext); connectedBindingQueue.BindingContextMap.Add($"{details.ServerName}_{details.DatabaseName}_{details.UserName}_NULL", connectedBindingContext);
mockConnectionOpener = new Mock<SqlConnectionOpener>();
connectedBindingQueue.SetConnectionOpener(mockConnectionOpener.Object);
service.ConnectedBindingQueue = connectedBindingQueue; service.ConnectedBindingQueue = connectedBindingQueue;
} }
@@ -400,9 +403,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
private async Task CreateSessionRequestAndVerifyServerNodeHelper(ConnectionDetails details) private async Task CreateSessionRequestAndVerifyServerNodeHelper(ConnectionDetails details)
{ {
serviceHostMock.AddEventHandling(ConnectionCompleteNotification.Type, null); serviceHostMock.AddEventHandling(ConnectionCompleteNotification.Type, null);
//SessionCreatedParameters sessionResult
// Stub out the connection to avoid a 30second timeout while attempting to connect.
// The tests don't need any connection context anyhow so this doesn't impact the scenario
mockConnectionOpener.Setup(b => b.OpenSqlConnection(It.IsAny<ConnectionInfo>(), It.IsAny<string>()))
.Throws<Exception>();
connectionServiceMock.Setup(c => c.Connect(It.IsAny<ConnectParams>())) connectionServiceMock.Setup(c => c.Connect(It.IsAny<ConnectParams>()))
.Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details))); .Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details)));
ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details); ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details);