Add more test cases for code coverage (#127)

Next round of code coverage test cases.  Please review the commit for next iteration.

* Add connection retry tests

* More test coverage

* Update diagnostics end-to-end test
This commit is contained in:
Karl Burtram
2016-10-29 12:10:02 -07:00
committed by GitHub
parent f46fc0c787
commit 6cdaa6e808
11 changed files with 581 additions and 80 deletions

View File

@@ -0,0 +1,8 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]

View File

@@ -6,12 +6,15 @@
#if LIVE_CONNECTION_TESTS
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
using Microsoft.SqlTools.ServiceLayer.Test.Utility;
using Xunit;
using static Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection.RetryPolicy;
using static Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection.RetryPolicy.TimeBasedRetryPolicy;
namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
{
@@ -21,6 +24,109 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
/// </summary>
public class ReliableConnectionTests
{
internal class TestFixedDelayPolicy : FixedDelayPolicy
{
public TestFixedDelayPolicy(
IErrorDetectionStrategy strategy,
int maxRetryCount,
TimeSpan intervalBetweenRetries)
: base(strategy,
maxRetryCount,
intervalBetweenRetries)
{
}
public bool InvokeShouldRetryImpl(RetryState retryStateObj)
{
return ShouldRetryImpl(retryStateObj);
}
}
internal class TestProgressiveRetryPolicy : ProgressiveRetryPolicy
{
public TestProgressiveRetryPolicy(
IErrorDetectionStrategy strategy,
int maxRetryCount,
TimeSpan initialInterval,
TimeSpan increment)
: base(strategy,
maxRetryCount,
initialInterval,
increment)
{
}
public bool InvokeShouldRetryImpl(RetryState retryStateObj)
{
return ShouldRetryImpl(retryStateObj);
}
}
internal class TestTimeBasedRetryPolicy : TimeBasedRetryPolicy
{
public TestTimeBasedRetryPolicy(
IErrorDetectionStrategy strategy,
TimeSpan minTotalRetryTimeLimit,
TimeSpan maxTotalRetryTimeLimit,
double totalRetryTimeLimitRate,
TimeSpan minInterval,
TimeSpan maxInterval,
double intervalFactor)
: base(
strategy,
minTotalRetryTimeLimit,
maxTotalRetryTimeLimit,
totalRetryTimeLimitRate,
minInterval,
maxInterval,
intervalFactor)
{
}
public bool InvokeShouldRetryImpl(RetryState retryStateObj)
{
return ShouldRetryImpl(retryStateObj);
}
}
[Fact]
public void FixedDelayPolicyTest()
{
TestFixedDelayPolicy policy = new TestFixedDelayPolicy(
strategy: new NetworkConnectivityErrorDetectionStrategy(),
maxRetryCount: 3,
intervalBetweenRetries: TimeSpan.FromMilliseconds(100));
bool shouldRety = policy.InvokeShouldRetryImpl(new RetryStateEx());
Assert.True(shouldRety);
}
[Fact]
public void ProgressiveRetryPolicyTest()
{
TestProgressiveRetryPolicy policy = new TestProgressiveRetryPolicy(
strategy: new NetworkConnectivityErrorDetectionStrategy(),
maxRetryCount: 3,
initialInterval: TimeSpan.FromMilliseconds(100),
increment: TimeSpan.FromMilliseconds(100));
bool shouldRety = policy.InvokeShouldRetryImpl(new RetryStateEx());
Assert.True(shouldRety);
}
[Fact]
public void TimeBasedRetryPolicyTest()
{
TestTimeBasedRetryPolicy policy = new TestTimeBasedRetryPolicy(
strategy: new NetworkConnectivityErrorDetectionStrategy(),
minTotalRetryTimeLimit: TimeSpan.FromMilliseconds(100),
maxTotalRetryTimeLimit: TimeSpan.FromMilliseconds(100),
totalRetryTimeLimitRate: 100,
minInterval: TimeSpan.FromMilliseconds(100),
maxInterval: TimeSpan.FromMilliseconds(100),
intervalFactor: 1);
bool shouldRety = policy.InvokeShouldRetryImpl(new RetryStateEx());
Assert.True(shouldRety);
}
/// <summary>
/// Environment variable that stores the name of the test server hosting the SQL Server instance.
/// </summary>
@@ -338,6 +444,90 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
Assert.NotEmpty(info.ServerVersion);
});
}
/// <summary>
/// Validate ambient static settings
/// </summary>
[Fact]
public void AmbientSettingsStaticPropertiesTest()
{
var defaultSettings = AmbientSettings.DefaultSettings;
Assert.NotNull(defaultSettings);
var masterReferenceFilePath = AmbientSettings.MasterReferenceFilePath;
var maxDataReaderDegreeOfParallelism = AmbientSettings.MaxDataReaderDegreeOfParallelism;
var tableProgressUpdateInterval = AmbientSettings.TableProgressUpdateInterval;
var traceRowCountFailure = AmbientSettings.TraceRowCountFailure;
var useOfflineDataReader = AmbientSettings.UseOfflineDataReader;
var streamBackingStoreForOfflineDataReading = AmbientSettings.StreamBackingStoreForOfflineDataReading;
var disableIndexesForDataPhase = AmbientSettings.DisableIndexesForDataPhase;
var reliableDdlEnabled = AmbientSettings.ReliableDdlEnabled;
var importModelDatabase = AmbientSettings.ImportModelDatabase;
var supportAlwaysEncrypted = AmbientSettings.SupportAlwaysEncrypted;
var alwaysEncryptedWizardMigration = AmbientSettings.AlwaysEncryptedWizardMigration;
var skipObjectTypeBlocking =AmbientSettings.SkipObjectTypeBlocking;
var doNotSerializeQueryStoreSettings = AmbientSettings.DoNotSerializeQueryStoreSettings;
var lockTimeoutMilliSeconds = AmbientSettings.LockTimeoutMilliSeconds;
var queryTimeoutSeconds = AmbientSettings.QueryTimeoutSeconds;
var longRunningQueryTimeoutSeconds = AmbientSettings.LongRunningQueryTimeoutSeconds;
var alwaysRetryOnTransientFailure = AmbientSettings.AlwaysRetryOnTransientFailure;
var connectionRetryMessageHandler = AmbientSettings.ConnectionRetryMessageHandler;
using (var settingsContext = AmbientSettings.CreateSettingsContext())
{
var settings = settingsContext.Settings;
Assert.NotNull(settings);
}
}
/// <summary>
/// Validate ambient settings populate
/// </summary>
[Fact]
public void AmbientSettingsPopulateTest()
{
var data = new AmbientSettings.AmbientData();
var masterReferenceFilePath = data.MasterReferenceFilePath;
data.MasterReferenceFilePath = masterReferenceFilePath;
var lockTimeoutMilliSeconds = data.LockTimeoutMilliSeconds;
data.LockTimeoutMilliSeconds = lockTimeoutMilliSeconds;
var queryTimeoutSeconds = data.QueryTimeoutSeconds;
data.QueryTimeoutSeconds = queryTimeoutSeconds;
var longRunningQueryTimeoutSeconds = data.LongRunningQueryTimeoutSeconds;
data.LongRunningQueryTimeoutSeconds = longRunningQueryTimeoutSeconds;
var alwaysRetryOnTransientFailure = data.AlwaysRetryOnTransientFailure;
data.AlwaysRetryOnTransientFailure = alwaysRetryOnTransientFailure;
var connectionRetryMessageHandler = data.ConnectionRetryMessageHandler;
data.ConnectionRetryMessageHandler = connectionRetryMessageHandler;
var traceRowCountFailure = data.TraceRowCountFailure;
data.TraceRowCountFailure = traceRowCountFailure;
var tableProgressUpdateInterval = data.TableProgressUpdateInterval;
data.TableProgressUpdateInterval = tableProgressUpdateInterval;
var useOfflineDataReader = data.UseOfflineDataReader;
data.UseOfflineDataReader = useOfflineDataReader;
var streamBackingStoreForOfflineDataReading = data.StreamBackingStoreForOfflineDataReading;
data.StreamBackingStoreForOfflineDataReading = streamBackingStoreForOfflineDataReading;
var disableIndexesForDataPhase = data.DisableIndexesForDataPhase;
data.DisableIndexesForDataPhase = disableIndexesForDataPhase;
var reliableDdlEnabled = data.ReliableDdlEnabled;
data.ReliableDdlEnabled = reliableDdlEnabled;
var importModelDatabase = data.ImportModelDatabase;
data.ImportModelDatabase = importModelDatabase;
var supportAlwaysEncrypted = data.SupportAlwaysEncrypted;
data.SupportAlwaysEncrypted = supportAlwaysEncrypted;
var alwaysEncryptedWizardMigration = data.AlwaysEncryptedWizardMigration;
data.AlwaysEncryptedWizardMigration = alwaysEncryptedWizardMigration;
var skipObjectTypeBlocking = data.SkipObjectTypeBlocking;
data.SkipObjectTypeBlocking = skipObjectTypeBlocking;
var doNotSerializeQueryStoreSettings = data.DoNotSerializeQueryStoreSettings;
data.DoNotSerializeQueryStoreSettings = doNotSerializeQueryStoreSettings;
Dictionary<string, object> settings = new Dictionary<string, object>();
settings.Add("LockTimeoutMilliSeconds", 10000);
data.PopulateSettings(settings);
data.TraceSettings();
}
}
}
#endif // LIVE_CONNECTION_TESTS

View File

@@ -156,7 +156,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
{
try
{
InitializeTestServices();
TestObjects.InitializeTestServices();
}
catch (System.ArgumentException)
{
@@ -175,26 +175,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
[Fact]
public void PrepopulateCommonMetadata()
{
InitializeTestServices();
ScriptFile scriptFile;
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
string sqlFilePath = GetTestSqlFile();
ScriptFile scriptFile = WorkspaceService<SqlToolsSettings>.Instance.Workspace.GetFile(sqlFilePath);
string ownerUri = scriptFile.ClientFilePath;
var connectionService = TestObjects.GetLiveTestConnectionService();
var connectionResult =
connectionService
.Connect(new ConnectParams()
{
OwnerUri = ownerUri,
Connection = TestObjects.GetIntegratedTestConnectionDetails()
});
connectionResult.Wait();
ConnectionInfo connInfo = null;
connectionService.TryFindConnection(ownerUri, out connInfo);
ScriptParseInfo scriptInfo = new ScriptParseInfo();
scriptInfo.IsConnected = true;
@@ -226,48 +209,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
#endif
private string GetTestSqlFile()
{
string filePath = Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"sqltest.sql");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
File.WriteAllText(filePath, "SELECT * FROM sys.objects\n");
return filePath;
}
private void InitializeTestServices()
{
const string hostName = "SQL Tools Service Host";
const string hostProfileId = "SQLToolsService";
Version hostVersion = new Version(1,0);
// set up the host details and profile paths
var hostDetails = new HostDetails(hostName, hostProfileId, hostVersion);
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails);
// Grab the instance of the service host
Hosting.ServiceHost serviceHost = Hosting.ServiceHost.Instance;
// Start the service
serviceHost.Start().Wait();
// Initialize the services that will be hosted here
WorkspaceService<SqlToolsSettings>.Instance.InitializeService(serviceHost);
LanguageService.Instance.InitializeService(serviceHost, sqlToolsContext);
ConnectionService.Instance.InitializeService(serviceHost);
CredentialService.Instance.InitializeService(serviceHost);
QueryExecutionService.Instance.InitializeService(serviceHost);
serviceHost.Initialize();
}
private Hosting.ServiceHost GetTestServiceHost()
{
// set up the host details and profile paths

View File

@@ -0,0 +1,87 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#if LIVE_CONNECTION_TESTS
using System.Data.Common;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.Test.Utility;
using Xunit;
namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.DataStorage
{
public class StorageDataReaderTests
{
private StorageDataReader GetTestStorageDataReader(out DbDataReader reader, string query)
{
ScriptFile scriptFile;
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
var command = connInfo.SqlConnection.CreateCommand();
command.CommandText = query;
reader = command.ExecuteReader();
return new StorageDataReader(reader);
}
/// <summary>
/// Validate GetBytesWithMaxCapacity
/// </summary>
[Fact]
public void GetBytesWithMaxCapacityTest()
{
DbDataReader reader;
var storageReader = GetTestStorageDataReader(
out reader,
"SELECT CAST([name] as TEXT) As TextName FROM sys.all_columns");
reader.Read();
Assert.False(storageReader.IsDBNull(0));
byte[] bytes = storageReader.GetBytesWithMaxCapacity(0, 100);
Assert.NotNull(bytes);
}
/// <summary>
/// Validate GetCharsWithMaxCapacity
/// </summary>
[Fact]
public void GetCharsWithMaxCapacityTest()
{
DbDataReader reader;
var storageReader = GetTestStorageDataReader(
out reader,
"SELECT name FROM sys.all_columns");
reader.Read();
Assert.False(storageReader.IsDBNull(0));
string shortName = storageReader.GetCharsWithMaxCapacity(0, 2);
Assert.True(shortName.Length == 2);
}
/// <summary>
/// Validate GetXmlWithMaxCapacity
/// </summary>
[Fact]
public void GetXmlWithMaxCapacityTest()
{
DbDataReader reader;
var storageReader = GetTestStorageDataReader(
out reader,
"SELECT CAST('<xml>Test XML context</xml>' AS XML) As XmlColumn");
reader.Read();
Assert.False(storageReader.IsDBNull(0));
string shortXml = storageReader.GetXmlWithMaxCapacity(0, 2);
Assert.True(shortXml.Length == 3);
}
}
}
#endif

View File

@@ -7,10 +7,18 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Reflection;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Credentials;
using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Microsoft.SqlTools.ServiceLayer.Test.Utility;
using Microsoft.SqlTools.ServiceLayer.Workspace;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
namespace Microsoft.SqlTools.Test.Utility
{
@@ -19,6 +27,7 @@ namespace Microsoft.SqlTools.Test.Utility
/// </summary>
public class TestObjects
{
private static bool hasInitServices = false;
public const string ScriptUri = "file://some/file.sql";
/// <summary>
@@ -109,6 +118,79 @@ namespace Microsoft.SqlTools.Test.Utility
// connect to a real server instance
return ConnectionService.Instance.ConnectionFactory;
}
public static void InitializeTestServices()
{
if (TestObjects.hasInitServices)
{
return;
}
TestObjects.hasInitServices = true;
const string hostName = "SQ Tools Test Service Host";
const string hostProfileId = "SQLToolsTestService";
Version hostVersion = new Version(1,0);
// set up the host details and profile paths
var hostDetails = new HostDetails(hostName, hostProfileId, hostVersion);
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails);
// Grab the instance of the service host
ServiceHost serviceHost = ServiceHost.Instance;
// Start the service
serviceHost.Start().Wait();
// Initialize the services that will be hosted here
WorkspaceService<SqlToolsSettings>.Instance.InitializeService(serviceHost);
LanguageService.Instance.InitializeService(serviceHost, sqlToolsContext);
ConnectionService.Instance.InitializeService(serviceHost);
CredentialService.Instance.InitializeService(serviceHost);
QueryExecutionService.Instance.InitializeService(serviceHost);
serviceHost.Initialize();
}
public static string GetTestSqlFile()
{
string filePath = Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"sqltest.sql");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
File.WriteAllText(filePath, "SELECT * FROM sys.objects\n");
return filePath;
}
public static ConnectionInfo InitLiveConnectionInfo(out ScriptFile scriptFile)
{
TestObjects.InitializeTestServices();
string sqlFilePath = GetTestSqlFile();
scriptFile = WorkspaceService<SqlToolsSettings>.Instance.Workspace.GetFile(sqlFilePath);
string ownerUri = scriptFile.ClientFilePath;
var connectionService = TestObjects.GetLiveTestConnectionService();
var connectionResult =
connectionService
.Connect(new ConnectParams()
{
OwnerUri = ownerUri,
Connection = TestObjects.GetIntegratedTestConnectionDetails()
});
connectionResult.Wait();
ConnectionInfo connInfo = null;
connectionService.TryFindConnection(ownerUri, out connInfo);
return connInfo;
}
}
/// <summary>