|
4 | 4 |
|
5 | 5 | using System;
|
6 | 6 | using System.Reflection;
|
| 7 | +using Xunit; |
7 | 8 |
|
8 | 9 | namespace Microsoft.Data.SqlClient.ManualTesting.Tests.SystemDataInternals
|
9 | 10 | {
|
10 | 11 | internal static class TdsParserStateObjectHelper
|
11 | 12 | {
|
12 |
| - private static readonly Assembly s_systemDotData = typeof(Microsoft.Data.SqlClient.SqlConnection).GetTypeInfo().Assembly; |
13 |
| - private static readonly Type s_tdsParserStateObject = s_systemDotData.GetType("Microsoft.Data.SqlClient.TdsParserStateObject"); |
14 |
| - private static readonly FieldInfo s_forceAllPends = s_tdsParserStateObject.GetField("s_forceAllPends", BindingFlags.Static | BindingFlags.NonPublic); |
15 |
| - private static readonly FieldInfo s_skipSendAttention = s_tdsParserStateObject.GetField("s_skipSendAttention", BindingFlags.Static | BindingFlags.NonPublic); |
16 |
| - private static readonly FieldInfo s_forceSyncOverAsyncAfterFirstPend = s_tdsParserStateObject.GetField("s_forceSyncOverAsyncAfterFirstPend", BindingFlags.Static | BindingFlags.NonPublic); |
17 |
| - private static readonly FieldInfo s_failAsyncPends = s_tdsParserStateObject.GetField("s_failAsyncPends", BindingFlags.Static | BindingFlags.NonPublic); |
18 |
| - private static readonly FieldInfo s_forcePendingReadsToWaitForUser = s_tdsParserStateObject.GetField("s_forcePendingReadsToWaitForUser", BindingFlags.Static | BindingFlags.NonPublic); |
19 |
| - private static readonly Type s_tdsParserStateObjectManaged = s_systemDotData.GetType("Microsoft.Data.SqlClient.SNI.TdsParserStateObjectManaged"); |
20 |
| - private static readonly FieldInfo s_tdsParserStateObjectManagedSessionHandle = s_tdsParserStateObjectManaged.GetField("_sessionHandle", BindingFlags.Instance | BindingFlags.NonPublic); |
| 13 | + private static readonly FieldInfo s_forceAllPends; |
| 14 | + private static readonly FieldInfo s_skipSendAttention; |
| 15 | + private static readonly FieldInfo s_forceSyncOverAsyncAfterFirstPend; |
| 16 | + private static readonly FieldInfo s_failAsyncPends; |
| 17 | + private static readonly FieldInfo s_forcePendingReadsToWaitForUser; |
| 18 | + private static readonly Type s_tdsParserStateObjectManaged; |
| 19 | + private static readonly FieldInfo s_tdsParserStateObjectManagedSessionHandle; |
| 20 | + |
| 21 | + static TdsParserStateObjectHelper() |
| 22 | + { |
| 23 | + Assembly assembly = typeof(Microsoft.Data.SqlClient.SqlConnection).GetTypeInfo().Assembly; |
| 24 | + Assert.True(assembly is not null, nameof(assembly)); |
| 25 | + |
| 26 | + Type tdsParserStateObject = assembly.GetType("Microsoft.Data.SqlClient.TdsParserStateObject"); |
| 27 | + Assert.True(tdsParserStateObject is not null, nameof(tdsParserStateObject)); |
| 28 | + |
| 29 | + s_forceAllPends = tdsParserStateObject.GetField("s_forceAllPends", BindingFlags.Static | BindingFlags.NonPublic); |
| 30 | + Assert.True(s_forceAllPends is not null, nameof(s_forceAllPends)); |
| 31 | + |
| 32 | + s_skipSendAttention = tdsParserStateObject.GetField("s_skipSendAttention", BindingFlags.Static | BindingFlags.NonPublic); |
| 33 | + Assert.True(s_skipSendAttention is not null, nameof(s_skipSendAttention)); |
| 34 | + |
| 35 | + s_forceSyncOverAsyncAfterFirstPend = tdsParserStateObject.GetField("s_forceSyncOverAsyncAfterFirstPend", BindingFlags.Static | BindingFlags.NonPublic); |
| 36 | + Assert.True(s_forceSyncOverAsyncAfterFirstPend is not null, nameof(s_forceSyncOverAsyncAfterFirstPend)); |
| 37 | + |
| 38 | + s_failAsyncPends = tdsParserStateObject.GetField("s_failAsyncPends", BindingFlags.Static | BindingFlags.NonPublic); |
| 39 | + Assert.True(s_failAsyncPends is not null, nameof(s_failAsyncPends)); |
| 40 | + |
| 41 | + s_forcePendingReadsToWaitForUser = tdsParserStateObject.GetField("s_forcePendingReadsToWaitForUser", BindingFlags.Static | BindingFlags.NonPublic); |
| 42 | + Assert.True(s_forcePendingReadsToWaitForUser is not null, nameof(s_forcePendingReadsToWaitForUser)); |
| 43 | + |
| 44 | + // These managed SNI handles are allowed to be null, since they |
| 45 | + // won't exist in .NET Framework builds. |
| 46 | + s_tdsParserStateObjectManaged = |
| 47 | + assembly.GetType("Microsoft.Data.SqlClient.SNI.TdsParserStateObjectManaged"); |
| 48 | + s_tdsParserStateObjectManagedSessionHandle = null; |
| 49 | + if (s_tdsParserStateObjectManaged is not null) |
| 50 | + { |
| 51 | + s_tdsParserStateObjectManagedSessionHandle = |
| 52 | + s_tdsParserStateObjectManaged.GetField( |
| 53 | + "_sessionHandle", |
| 54 | + BindingFlags.Instance | BindingFlags.NonPublic); |
| 55 | + // If we have the managed SNI type, we must have the session |
| 56 | + // handle field. |
| 57 | + Assert.True( |
| 58 | + s_tdsParserStateObjectManagedSessionHandle is not null, |
| 59 | + nameof(s_tdsParserStateObjectManagedSessionHandle)); |
| 60 | + } |
| 61 | + } |
21 | 62 |
|
22 | 63 | internal static bool ForceAllPends
|
23 | 64 | {
|
@@ -49,17 +90,20 @@ internal static bool FailAsyncPends
|
49 | 90 | set { s_failAsyncPends.SetValue(null, value); }
|
50 | 91 | }
|
51 | 92 |
|
52 |
| - private static void VerifyObjectIsTdsParserStateObject(object stateObject) |
| 93 | + internal static object GetSessionHandle(object stateObject) |
53 | 94 | {
|
54 | 95 | if (stateObject == null)
|
| 96 | + { |
55 | 97 | throw new ArgumentNullException(nameof(stateObject));
|
56 |
| - if (!s_tdsParserStateObjectManaged.IsInstanceOfType(stateObject)) |
| 98 | + } |
| 99 | + if (s_tdsParserStateObjectManaged is null) |
| 100 | + { |
| 101 | + throw new InvalidOperationException("This method is not supported on this platform."); |
| 102 | + } |
| 103 | + if (! s_tdsParserStateObjectManaged.IsInstanceOfType(stateObject)) |
| 104 | + { |
57 | 105 | throw new ArgumentException("Object provided was not a TdsParserStateObjectManaged", nameof(stateObject));
|
58 |
| - } |
59 |
| - |
60 |
| - internal static object GetSessionHandle(object stateObject) |
61 |
| - { |
62 |
| - VerifyObjectIsTdsParserStateObject(stateObject); |
| 106 | + } |
63 | 107 | return s_tdsParserStateObjectManagedSessionHandle.GetValue(stateObject);
|
64 | 108 | }
|
65 | 109 | }
|
|
0 commit comments