mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-28 09:35:37 -05:00
Add connection retry logic to integration tests (#1523)
* Add retry to live connection * Disable migration test * Tune the retry logic * Disable SQLCMD test
This commit is contained in:
@@ -14,9 +14,15 @@ using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||
using NUnit.Framework;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility
|
||||
{
|
||||
public class LiveConnectionException : Exception {
|
||||
public LiveConnectionException(string message)
|
||||
: base(message) { }
|
||||
}
|
||||
|
||||
public class LiveConnectionHelper
|
||||
{
|
||||
public static string GetTestSqlFile(string fileName = null)
|
||||
@@ -41,28 +47,9 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility
|
||||
|
||||
public static TestConnectionResult InitLiveConnectionInfo(string databaseName = null, string ownerUri = null)
|
||||
{
|
||||
ScriptFile scriptFile = null;
|
||||
ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName);
|
||||
if (string.IsNullOrEmpty(ownerUri))
|
||||
{
|
||||
ownerUri = GetTestSqlFile();
|
||||
scriptFile = TestServiceProvider.Instance.WorkspaceService.Workspace.GetFile(ownerUri);
|
||||
ownerUri = scriptFile.ClientUri;
|
||||
}
|
||||
var connectionService = GetLiveTestConnectionService();
|
||||
var connectionResult =
|
||||
connectionService
|
||||
.Connect(new ConnectParams
|
||||
{
|
||||
OwnerUri = ownerUri,
|
||||
Connection = connectParams.Connection
|
||||
});
|
||||
|
||||
connectionResult.Wait();
|
||||
|
||||
ConnectionInfo connInfo = null;
|
||||
connectionService.TryFindConnection(ownerUri, out connInfo);
|
||||
return new TestConnectionResult() { ConnectionInfo = connInfo, ScriptFile = scriptFile };
|
||||
var task = InitLiveConnectionInfoAsync(databaseName, ownerUri, ServiceLayer.Connection.ConnectionType.Default);
|
||||
task.Wait();
|
||||
return task.Result;
|
||||
}
|
||||
|
||||
public static async Task<TestConnectionResult> InitLiveConnectionInfoAsync(string databaseName = "master", string ownerUri = null,
|
||||
@@ -81,23 +68,48 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility
|
||||
}
|
||||
ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(serverType, databaseName);
|
||||
|
||||
var connectionService = GetLiveTestConnectionService();
|
||||
var connectionResult =
|
||||
await connectionService
|
||||
.Connect(new ConnectParams
|
||||
{
|
||||
OwnerUri = ownerUri,
|
||||
Connection = connectParams.Connection,
|
||||
Type = connectionType
|
||||
});
|
||||
if (!string.IsNullOrEmpty(connectionResult.ErrorMessage))
|
||||
// try to connect up to 3 times, sleeping in between retries
|
||||
const int RetryCount = 3;
|
||||
const int RetryDelayMs = 15000;
|
||||
for (int attempt = 0; attempt < RetryCount; ++attempt)
|
||||
{
|
||||
Console.WriteLine(connectionResult.ErrorMessage);
|
||||
var connectionService = GetLiveTestConnectionService();
|
||||
var connectionResult =
|
||||
await connectionService.Connect(new ConnectParams
|
||||
{
|
||||
OwnerUri = ownerUri,
|
||||
Connection = connectParams.Connection,
|
||||
Type = connectionType
|
||||
});
|
||||
if (!string.IsNullOrEmpty(connectionResult.ErrorMessage))
|
||||
{
|
||||
Console.WriteLine(connectionResult.ErrorMessage);
|
||||
}
|
||||
|
||||
ConnectionInfo connInfo;
|
||||
connectionService.TryFindConnection(ownerUri, out connInfo);
|
||||
|
||||
// if the connection wasn't successful then cleanup and try again (up to max retry count)
|
||||
if (connInfo == null)
|
||||
{
|
||||
connectionService.Disconnect(new DisconnectParams()
|
||||
{
|
||||
OwnerUri = ownerUri
|
||||
});
|
||||
// don't sleep on the final iterations since we won't try again
|
||||
if (attempt < RetryCount - 1)
|
||||
{
|
||||
Thread.Sleep(RetryDelayMs);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return new TestConnectionResult() { ConnectionInfo = connInfo, ScriptFile = scriptFile };
|
||||
}
|
||||
}
|
||||
|
||||
ConnectionInfo connInfo = null;
|
||||
connectionService.TryFindConnection(ownerUri, out connInfo);
|
||||
return new TestConnectionResult() { ConnectionInfo = connInfo, ScriptFile = scriptFile };
|
||||
throw new LiveConnectionException(string.Format("Could not establish a connection to {0}:{1}",
|
||||
connectParams.Connection.ServerName, connectParams.Connection.DatabaseName));
|
||||
}
|
||||
|
||||
public static ConnectionInfo InitLiveConnectionInfoForDefinition(string databaseName = null)
|
||||
@@ -106,18 +118,11 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility
|
||||
{
|
||||
ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName);
|
||||
string ownerUri = queryTempFile.FilePath;
|
||||
|
||||
InitLiveConnectionInfo(databaseName, ownerUri);
|
||||
|
||||
var connectionService = GetLiveTestConnectionService();
|
||||
var connectionResult =
|
||||
connectionService
|
||||
.Connect(new ConnectParams
|
||||
{
|
||||
OwnerUri = ownerUri,
|
||||
Connection = connectParams.Connection
|
||||
});
|
||||
|
||||
connectionResult.Wait();
|
||||
|
||||
ConnectionInfo connInfo = null;
|
||||
ConnectionInfo connInfo;
|
||||
connectionService.TryFindConnection(ownerUri, out connInfo);
|
||||
|
||||
Assert.NotNull(connInfo);
|
||||
|
||||
Reference in New Issue
Block a user