This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Update SqlClient manual test feature detection #34040
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
86c0018
add DataUtility functions to detect service broker and specific datab…
Wraith2 3cfa7f2
change UDT tests to use database detection fact for UDTTestDatabase
Wraith2 7e86ed5
change service broker tests to use conditional fact
Wraith2 9507ae0
add the new attribute files to the project
Wraith2 e739e68
address feedback
Wraith2 8541be2
remember to open connection before running query
Wraith2 2e7351f
address feedback
Wraith2 6a74df1
fixup IsServiceBrokerEnabled
Wraith2 2ab4fbe
remove IsServiceBrokerEnabled
Wraith2 a80793f
re-enabled tests
Wraith2 fb07bdc
remove unused variable
Wraith2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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); | ||
|
|
@@ -21,6 +25,10 @@ public static class DataTestUtility | |
| ".database.cloudapi.de", | ||
| ".database.usgovcloudapi.net", | ||
| ".database.chinacloudapi.cn"}; | ||
|
|
||
| private static bool? serviceBrokerEnabled; | ||
| private static Dictionary<string, bool> databasesAvailable; | ||
|
|
||
| static DataTestUtility() | ||
| { | ||
| NpConnStr = Environment.GetEnvironmentVariable("TEST_NP_CONN_STR"); | ||
|
|
@@ -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)) | ||
|
Wraith2 marked this conversation as resolved.
Outdated
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
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) | ||
| { | ||
|
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) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed anymore.