Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Threading.Tasks;
Expand All @@ -13,6 +14,9 @@ public static class DataTestUtility
{
public static readonly string NpConnStr = null;
public static readonly string TcpConnStr = null;

public const string UdtTestDbName = "UdtTestDb";

private static readonly Assembly s_systemDotData = typeof(System.Data.SqlClient.SqlConnection).GetTypeInfo().Assembly;
private static readonly Type s_tdsParserStateObjectFactory = s_systemDotData?.GetType("System.Data.SqlClient.TdsParserStateObjectFactory");
private static readonly PropertyInfo s_useManagedSNI = s_tdsParserStateObjectFactory?.GetProperty("UseManagedSNI", BindingFlags.Static | BindingFlags.Public);
Expand All @@ -21,6 +25,10 @@ public static class DataTestUtility
".database.cloudapi.de",
".database.usgovcloudapi.net",
".database.chinacloudapi.cn"};

private static bool? serviceBrokerEnabled;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed anymore.

private static Dictionary<string, bool> databasesAvailable;

static DataTestUtility()
{
NpConnStr = Environment.GetEnvironmentVariable("TEST_NP_CONN_STR");
Expand All @@ -32,6 +40,66 @@ public static bool AreConnStringsSetup()
return !string.IsNullOrEmpty(NpConnStr) && !string.IsNullOrEmpty(TcpConnStr);
}

public static bool IsServiceBrokerEnabled()
{
if (!serviceBrokerEnabled.HasValue)
{
serviceBrokerEnabled = false;
if (AreConnStringsSetup())
{
try
{
var builder = new SqlConnectionStringBuilder(TcpConnStr);
string database = builder.InitialCatalog;
builder.ConnectTimeout = 2;
using (var connection = new SqlConnection(builder.ToString()))
using (var command = new SqlCommand("SELECT is_service_broker_enabled FROM sys.sys.databases WHERE name=@name", connection))
Comment thread
Wraith2 marked this conversation as resolved.
Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a bug. The name of the DB is hardcoded to @name. When I try to run the tests on my server where SB is enabled, the tests are skipped. Please fix this to pass in the correct DB name

{
command.Parameters.AddWithValue("name", database);
using (var reader = command.ExecuteReader())
{
if (reader.HasRows && reader.Read())
{
serviceBrokerEnabled = (reader.GetInt32(0) == 1);
}
}
}
}
catch (Exception)
{
serviceBrokerEnabled = false;
Comment thread
Wraith2 marked this conversation as resolved.
Outdated
}
}
}
return serviceBrokerEnabled.Value;
}

public static bool IsDatabasePresent(string name)
{
databasesAvailable = databasesAvailable ?? new Dictionary<string, bool>();
bool present = false;
if (AreConnStringsSetup() && !string.IsNullOrEmpty(name) && !databasesAvailable.TryGetValue(name, out present))
{
try
{
var builder = new SqlConnectionStringBuilder(TcpConnStr);
builder.ConnectTimeout = 2;
using (var connection = new SqlConnection(builder.ToString()))
using (var command = new SqlCommand("SELECT COUNT(*) FROM sys.databases WHERE name=@name", connection))
{
connection.Open();
command.Parameters.AddWithValue("name", name);
present = Convert.ToInt32(command.ExecuteScalar()) == 1;
}
}
catch (Exception)
{
Comment thread
Wraith2 marked this conversation as resolved.
}
databasesAvailable[name] = present;
}
return present;
}

public static bool IsUsingManagedSNI() => (bool)(s_useManagedSNI?.GetValue(null) ?? false);

// the name length will be no more then (16 + prefix.Length + escapeLeft.Length + escapeRight.Length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ public void Dispose()
Cleanup();
}

public static bool IsServiceBrokerEnabled() => DataTestUtility.IsServiceBrokerEnabled();
Comment thread
Wraith2 marked this conversation as resolved.
Outdated

private static bool AreConnectionStringsSetup() => DataTestUtility.AreConnStringsSetup();

#region StartStop_Tests

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsServiceBrokerEnabled), nameof(AreConnectionStringsSetup))]
public void Test_DoubleStart_SameConnStr()
{
Assert.True(SqlDependency.Start(_startConnectionString), "Failed to start listener.");
Expand All @@ -51,7 +55,7 @@ public void Test_DoubleStart_SameConnStr()
Assert.True(SqlDependency.Stop(_startConnectionString), "Failed to stop listener.");
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsServiceBrokerEnabled), nameof(AreConnectionStringsSetup))]
public void Test_DoubleStart_DifferentConnStr()
{
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(_startConnectionString);
Expand All @@ -76,7 +80,7 @@ public void Test_DoubleStart_DifferentConnStr()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsServiceBrokerEnabled), nameof(AreConnectionStringsSetup))]
public void Test_Start_DifferentDB()
{
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(_startConnectionString)
Expand All @@ -97,7 +101,7 @@ public void Test_Start_DifferentDB()

#region SqlDependency_Tests

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsServiceBrokerEnabled), nameof(AreConnectionStringsSetup))]
public void Test_SingleDependency_NoStart()
{
using (SqlConnection conn = new SqlConnection(_execConnectionString))
Expand All @@ -115,7 +119,7 @@ public void Test_SingleDependency_NoStart()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsServiceBrokerEnabled), nameof(AreConnectionStringsSetup))]
public void Test_SingleDependency_Stopped()
{
SqlDependency.Start(_startConnectionString);
Expand All @@ -137,7 +141,7 @@ public void Test_SingleDependency_Stopped()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsServiceBrokerEnabled), nameof(AreConnectionStringsSetup))]
public void Test_SingleDependency_AllDefaults_SqlAuth()
{
Assert.True(SqlDependency.Start(_startConnectionString), "Failed to start listener.");
Expand Down Expand Up @@ -181,7 +185,7 @@ public void Test_SingleDependency_AllDefaults_SqlAuth()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsServiceBrokerEnabled), nameof(AreConnectionStringsSetup))]
public void Test_SingleDependency_CustomQueue_SqlAuth()
{
Assert.True(SqlDependency.Start(_startConnectionString, _queueName), "Failed to start listener.");
Expand Down Expand Up @@ -225,7 +229,7 @@ public void Test_SingleDependency_CustomQueue_SqlAuth()
/// <summary>
/// SqlDependecy premature timeout
/// </summary>
[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsServiceBrokerEnabled), nameof(AreConnectionStringsSetup))]
public void Test_SingleDependency_Timeout()
{
Assert.True(SqlDependency.Start(_startConnectionString), "Failed to start listener.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.Data.SqlClient.ManualTesting.Tests
{
public class UdtBulkCopyTest
{
private string _connStr;

[CheckConnStrSetupFact]
public static bool IsUDTTestDBPresent() => DataTestUtility.IsDatabasePresent(DataTestUtility.UdtTestDbName);

private static bool AreConnectionStringsSetup() => DataTestUtility.AreConnStringsSetup();

[ConditionalFact(nameof(IsUDTTestDBPresent), nameof(AreConnectionStringsSetup))]
public void RunCopyTest()
{
_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { InitialCatalog = "UdtTestDb" }).ConnectionString;
_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { InitialCatalog = DataTestUtility.UdtTestDbName }).ConnectionString;
SqlConnection conn = new SqlConnection(_connStr);

string cities = DataTestUtility.GetUniqueNameForSqlServer("UdtBulkCopy_cities");
Expand Down
22 changes: 13 additions & 9 deletions src/System.Data.SqlClient/tests/ManualTests/SQL/UdtTest/UdtTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ public class UdtTest
{
private string _connStr;

public static bool IsUDTTestDBPresent() => DataTestUtility.IsDatabasePresent(DataTestUtility.UdtTestDbName);

private static bool AreConnectionStringsSetup() => DataTestUtility.AreConnStringsSetup();

public UdtTest()
{
_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { InitialCatalog = "UdtTestDb" }).ConnectionString;
_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { InitialCatalog = DataTestUtility.UdtTestDbName }).ConnectionString;
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsUDTTestDBPresent), nameof(AreConnectionStringsSetup))]
public void ReaderTest()
{
using (SqlConnection conn = new SqlConnection(_connStr))
Expand Down Expand Up @@ -57,7 +61,7 @@ public void ReaderTest()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsUDTTestDBPresent), nameof(AreConnectionStringsSetup))]
public void ExecuteScalarTest()
{
using (SqlConnection conn = new SqlConnection(_connStr))
Expand All @@ -74,7 +78,7 @@ public void ExecuteScalarTest()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsUDTTestDBPresent), nameof(AreConnectionStringsSetup))]
public void InputParameterTest()
{
using (SqlConnection conn = new SqlConnection(_connStr))
Expand Down Expand Up @@ -123,7 +127,7 @@ public void InputParameterTest()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsUDTTestDBPresent), nameof(AreConnectionStringsSetup))]
public void OutputParameterTest()
{
using (SqlConnection conn = new SqlConnection(_connStr))
Expand Down Expand Up @@ -158,7 +162,7 @@ public void OutputParameterTest()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsUDTTestDBPresent), nameof(AreConnectionStringsSetup))]
public void FillTest()
{
using (SqlConnection conn = new SqlConnection(_connStr))
Expand All @@ -180,7 +184,7 @@ public void FillTest()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsUDTTestDBPresent), nameof(AreConnectionStringsSetup))]
public void UpdateTest()
{
using (SqlConnection conn = new SqlConnection(_connStr))
Expand Down Expand Up @@ -216,7 +220,7 @@ public void UpdateTest()
}
}

[CheckConnStrSetupFact]
[ConditionalFact(nameof(IsUDTTestDBPresent), nameof(AreConnectionStringsSetup))]
public void NullTest()
{
using (SqlConnection conn = new SqlConnection(_connStr))
Expand Down Expand Up @@ -290,4 +294,4 @@ private void VerifyDataSet(DataSet ds, Utf8String[] expectedValues)
}
}
}
}
}
Loading