-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathSqlConfigurationManagerReliabilityTest.cs
315 lines (281 loc) · 19.4 KB
/
SqlConfigurationManagerReliabilityTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Licensed to the .NET Foundation under one or more agreements.
// 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;
using Xunit;
using System.Data;
using System.Threading.Tasks;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public class SqlConfigurationManagerReliabilityTest
{
private static readonly SqlConnectionReliabilityTest s_connectionCRLTest = new SqlConnectionReliabilityTest();
private static readonly SqlCommandReliabilityTest s_commandCRLTest = new SqlCommandReliabilityTest();
private static readonly string s_upperCaseRetryMethodName_Fix = RetryLogicConfigHelper.RetryMethodName_Fix.ToUpper();
private static readonly string s_upperCaseRetryMethodName_Inc = RetryLogicConfigHelper.RetryMethodName_Inc.ToUpper();
private static readonly string s_upperCaseRetryMethodName_Exp = RetryLogicConfigHelper.RetryMethodName_Exp.ToUpper();
private static string TcpCnnString => DataTestUtility.TCPConnectionString;
private static string InvalidTcpCnnString => new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString)
{ InitialCatalog = SqlConnectionReliabilityTest.InvalidInitialCatalog, ConnectTimeout = 1 }.ConnectionString;
#region Internal Functions
// Test relies on error 4060 for automatic retry, which is not returned when using AAD auth
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.TcpConnectionStringDoesNotUseAadAuth))]
[InlineData(RetryLogicConfigHelper.RetryMethodName_Fix, RetryLogicConfigHelper.RetryMethodName_Inc)]
[InlineData(RetryLogicConfigHelper.RetryMethodName_Inc, RetryLogicConfigHelper.RetryMethodName_Exp)]
[InlineData(RetryLogicConfigHelper.RetryMethodName_Exp, RetryLogicConfigHelper.RetryMethodName_Fix)]
public void LoadValidInternalTypes(string method1, string method2)
{
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(method1);
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(method2,
// Doesn't accept DML statements
@"^\b(?!UPDATE|DELETE|TRUNCATE|INSERT( +INTO){0,1})\b");
// for sake of reducing the retry time in total
cnnCfg.NumberOfTries = 1;
cmdCfg.NumberOfTries = 1;
object loaderObj = RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider);
Assert.NotNull(loaderObj);
RetryLogicConfigHelper.AssessProvider(cnnProvider, cnnCfg);
RetryLogicConfigHelper.AssessProvider(cmdProvider, cmdCfg);
// check the retry in action
s_connectionCRLTest.ConnectionRetryOpenInvalidCatalogFailed(TcpCnnString, cnnProvider);
s_commandCRLTest.RetryExecuteFail(TcpCnnString, cmdProvider);
if (DataTestUtility.IsNotAzureSynapse())
{
s_commandCRLTest.RetryExecuteUnauthorizedSqlStatementDML(TcpCnnString, cmdProvider);
}
}
#endregion
#region External Functions
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[InlineData("Microsoft.Data.SqlClient.Tests.StaticCustomConfigurableRetryLogic, ExternalConfigurableRetryLogic", "GetDefaultRetry_static")]
[InlineData("Microsoft.Data.SqlClient.Tests.StructCustomConfigurableRetryLogic, ExternalConfigurableRetryLogic", "GetDefaultRetry_static")]
[InlineData("Microsoft.Data.SqlClient.Tests.StructCustomConfigurableRetryLogic, ExternalConfigurableRetryLogic", "GetDefaultRetry")]
[InlineData("Microsoft.Data.SqlClient.Tests.CustomConfigurableRetryLogic, ExternalConfigurableRetryLogic", "GetDefaultRetry_static")]
[InlineData("Microsoft.Data.SqlClient.Tests.CustomConfigurableRetryLogic, ExternalConfigurableRetryLogic", "GetDefaultRetry")]
[InlineData("Microsoft.Data.SqlClient.Tests.CustomConfigurableRetryLogic, ExternalConfigurableRetryLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "GetDefaultRetry")]
[InlineData("Microsoft.Data.SqlClient.Tests.CustomConfigurableRetryLogicEx, ExternalConfigurableRetryLogic", "GetDefaultRetry")]
public void LoadCustomMethod(string typeName, string methodName)
{
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(methodName);
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(methodName);
// for sake of reducing the retry time in total
cnnCfg.RetryLogicType = typeName;
cmdCfg.RetryLogicType = typeName;
// for sake of reducing the retry time in total
cnnCfg.NumberOfTries = 1;
cmdCfg.NumberOfTries = 3;
object loaderObj = RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider);
Assert.NotNull(loaderObj);
TestConnection(cnnProvider, cnnCfg);
TestCommandExecute(cmdProvider, cmdCfg);
TestCommandExecuteAsync(cmdProvider, cmdCfg).Wait();
}
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[InlineData("ClassLibrary.Invalid, ExternalConfigurableRetryLogic", "GetDefaultRetry_static")]
[InlineData("ClassLibrary.Invalid, ExternalConfigurableRetryLogic", "GetDefaultRetry")]
[InlineData("Microsoft.Data.SqlClient.Tests.CustomConfigurableRetryLogic, ClassLibrary_Invalid", "GetDefaultRetry_static")]
[InlineData("Microsoft.Data.SqlClient.Tests.StaticCustomConfigurableRetryLogic, ClassLibrary_Invalid", "GetDefaultRetry_static")]
[InlineData("Microsoft.Data.SqlClient.Tests.StructCustomConfigurableRetryLogic, ClassLibrary_Invalid", "GetDefaultRetry")]
// Type and method name are case sensitive.
[InlineData("Microsoft.Data.SqlClient.Tests.StaticCustomConfigurableRetryLogic, ExternalConfigurableRetryLogic", "GETDEFAULTRETRY_STATIC")]
[InlineData("Microsoft.Data.SqlClient.Tests.STRUCTCUSTOMCONFIGURABLERETRYLOGIC, ExternalConfigurableRetryLogic", "GetDefaultRetry")]
[InlineData("MICROSOFT.DATA.SQLCLIENT.TESTS.CustomConfigurableRetryLogic, ExternalConfigurableRetryLogic", "GetDefaultRetry_static")]
[InlineData("Microsoft.Data.SqlClient.Tests.CustomConfigurableRetryLogic, ExternalConfigurableRetryLogic", "getdefaultretry")]
public void LoadInvalidCustomRetryLogicType(string typeName, string methodName)
{
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(methodName);
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(methodName);
// for sake of reducing the retry time in total
cnnCfg.RetryLogicType = typeName;
cmdCfg.RetryLogicType = typeName;
// for sake of reducing the retry time in total
cnnCfg.NumberOfTries = 1;
cmdCfg.NumberOfTries = 3;
object loaderObj = RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider);
Assert.NotNull(loaderObj);
s_connectionCRLTest.DefaultOpenWithoutRetry(TcpCnnString, cnnProvider);
s_commandCRLTest.NoneRetriableExecuteFail(TcpCnnString, cmdProvider);
}
#endregion
#region Invalid Configurations
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[InlineData("InvalidMethodName")]
[InlineData("Fix")]
[InlineData(null)]
[MemberData(nameof(RetryLogicConfigHelper.GetInvalidInternalMethodNames), MemberType = typeof(RetryLogicConfigHelper))]
public void InvalidRetryMethodName(string methodName)
{
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(methodName);
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(methodName, @"Don't care!");
object loaderObj = RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider);
Assert.NotNull(loaderObj);
// none retriable logic applies.
s_connectionCRLTest.DefaultOpenWithoutRetry(TcpCnnString, cnnProvider);
s_commandCRLTest.NoneRetriableExecuteFail(TcpCnnString, cmdProvider);
}
// Test relies on error 4060 for automatic retry, which is not returned when using AAD auth
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.TcpConnectionStringDoesNotUseAadAuth))]
[InlineData("InvalidRetrylogicTypeName")]
[InlineData("")]
[InlineData(null)]
// Specifying the internal type has the same effect
[InlineData("Microsoft.Data.SqlClient.SqlConfigurableRetryFactory")]
public void InvalidRetryLogicTypeWithValidInternalMethodName(string typeName)
{
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix);
cnnCfg.RetryLogicType = typeName;
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix);
// for sake of reducing the retry time in total
cnnCfg.NumberOfTries = 1;
cmdCfg.NumberOfTries = 1;
object loaderObj = RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider);
Assert.NotNull(loaderObj);
RetryLogicConfigHelper.AssessProvider(cnnProvider, cnnCfg);
RetryLogicConfigHelper.AssessProvider(cmdProvider, cmdCfg);
// internal type used to resolve the specified method
s_connectionCRLTest.ConnectionRetryOpenInvalidCatalogFailed(TcpCnnString, cnnProvider);
s_commandCRLTest.RetryExecuteFail(TcpCnnString, cmdProvider);
}
[Theory]
[MemberData(nameof(RetryLogicConfigHelper.GetIivalidTimes), MemberType = typeof(RetryLogicConfigHelper))]
public void OutOfRangeTime(TimeSpan deltaTime, TimeSpan minTime, TimeSpan maxTime)
{
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix);
cnnCfg.DeltaTime = deltaTime;
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix, @"Don't care!");
var ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider));
Assert.Equal(typeof(System.Configuration.ConfigurationErrorsException), ex.InnerException?.GetType());
Assert.Equal(typeof(ArgumentException), ex.InnerException?.InnerException?.GetType());
cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix);
cnnCfg.MinTimeInterval = minTime;
ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider));
Assert.Equal(typeof(System.Configuration.ConfigurationErrorsException), ex.InnerException?.GetType());
Assert.Equal(typeof(ArgumentException), ex.InnerException?.InnerException?.GetType());
cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix);
cnnCfg.MaxTimeInterval = maxTime;
ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider));
Assert.Equal(typeof(System.Configuration.ConfigurationErrorsException), ex.InnerException?.GetType());
Assert.Equal(typeof(ArgumentException), ex.InnerException?.InnerException?.GetType());
}
[Theory]
[InlineData(-1)]
[InlineData(-100)]
[InlineData(61)]
[InlineData(100)]
public void InvalidNumberOfTries(int num)
{
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix);
cnnCfg.NumberOfTries = num;
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix, @"Don't care!");
var ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider));
Assert.Equal(typeof(System.Configuration.ConfigurationErrorsException), ex.InnerException?.GetType());
Assert.Equal(typeof(ArgumentException), ex.InnerException?.InnerException?.GetType());
}
[Theory]
[InlineData("invalid value")]
[InlineData("1; 2; 3")]
[InlineData("1/2/3")]
[InlineData(",1,2,3")]
[InlineData("1,2,3,")]
[InlineData("1|2|3")]
[InlineData("1+2+3")]
[InlineData("1*2*3")]
[InlineData(@"1\2\3")]
[InlineData("1&2&3")]
[InlineData("1.2.3")]
[InlineData(@"~!@#$%^&*()_+={}[]|\""':;.><?/")]
public void InvalidTransientError(string errors)
{
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix);
cnnCfg.TransientErrors = errors;
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix, @"Don't care!");
var ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out SqlRetryLogicBaseProvider cmdProvider));
Assert.Equal(typeof(System.Configuration.ConfigurationErrorsException), ex.InnerException?.GetType());
Assert.Equal(typeof(ArgumentException), ex.InnerException?.InnerException?.GetType());
}
#endregion
#region Valid Configurations
[Theory]
[InlineData("-1,1,2,3")]
[InlineData("-1, 1, 2 , 3, -2")]
[InlineData("")]
public void ValidTransientError(string errors)
{
string[] transientErrorNumbers = errors.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
RetryLogicConfigs cnnCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix);
cnnCfg.TransientErrors = errors;
RetryLogicConfigs cmdCfg = RetryLogicConfigHelper.CreateRandomConfig(RetryLogicConfigHelper.RetryMethodName_Fix, @"Don't care!");
RetryLogicConfigHelper.ReturnLoaderAndProviders(cnnCfg, cmdCfg, out SqlRetryLogicBaseProvider cnnProvider, out _);
foreach(string errorString in transientErrorNumbers)
{
int errorNumber = int.Parse(errorString.Trim());
SqlException transientException = RetryLogicConfigHelper.CreateSqlException(errorNumber);
Assert.True(cnnProvider.RetryLogic.TransientPredicate(transientException), $"Error {errorNumber} is not considered transient by the predicate.");
}
}
#endregion
#region private methods
private void TestConnection(SqlRetryLogicBaseProvider provider, RetryLogicConfigs cnfig)
{
using (SqlConnection cnn = new SqlConnection(InvalidTcpCnnString))
{
cnn.RetryLogicProvider = provider;
var ex = Assert.Throws<AggregateException>(() => cnn.Open());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
var tex = Assert.ThrowsAsync<AggregateException>(() => cnn.OpenAsync());
Assert.Equal(cnfig.NumberOfTries, tex.Result.InnerExceptions.Count);
}
}
private void TestCommandExecute(SqlRetryLogicBaseProvider provider, RetryLogicConfigs cnfig)
{
using (SqlConnection cnn = new SqlConnection(TcpCnnString))
using (SqlCommand cmd = new SqlCommand())
{
cnn.Open();
cmd.Connection = cnn;
cmd.RetryLogicProvider = provider;
cmd.CommandText = "SELECT bad command";
var ex = Assert.Throws<AggregateException>(() => cmd.ExecuteScalar());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
ex = Assert.Throws<AggregateException>(() => cmd.ExecuteReader());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
ex = Assert.Throws<AggregateException>(() => cmd.ExecuteReader(CommandBehavior.Default));
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
ex = Assert.Throws<AggregateException>(() => cmd.ExecuteNonQuery());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
if (DataTestUtility.IsNotAzureSynapse())
{
cmd.CommandText = cmd.CommandText + " FOR XML AUTO";
ex = Assert.Throws<AggregateException>(() => cmd.ExecuteXmlReader());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
}
}
}
private async Task TestCommandExecuteAsync(SqlRetryLogicBaseProvider provider, RetryLogicConfigs cnfig)
{
using (SqlConnection cnn = new SqlConnection(TcpCnnString))
using (SqlCommand cmd = new SqlCommand())
{
cnn.Open();
cmd.Connection = cnn;
cmd.RetryLogicProvider = provider;
cmd.CommandText = "SELECT bad command";
var ex = await Assert.ThrowsAsync<AggregateException>(() => cmd.ExecuteScalarAsync());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
ex = await Assert.ThrowsAsync<AggregateException>(() => cmd.ExecuteReaderAsync());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
ex = await Assert.ThrowsAsync<AggregateException>(() => cmd.ExecuteReaderAsync(CommandBehavior.Default));
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
ex = await Assert.ThrowsAsync<AggregateException>(() => cmd.ExecuteNonQueryAsync());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
if (DataTestUtility.IsNotAzureSynapse())
{
cmd.CommandText = cmd.CommandText + " FOR XML AUTO";
ex = await Assert.ThrowsAsync<AggregateException>(() => cmd.ExecuteXmlReaderAsync());
Assert.Equal(cnfig.NumberOfTries, ex.InnerExceptions.Count);
}
}
}
#endregion
}
}