-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathSqlConnectionReliabilityTest.cs
218 lines (198 loc) · 11.1 KB
/
SqlConnectionReliabilityTest.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
// 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 System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public class SqlConnectionReliabilityTest
{
internal const string InvalidInitialCatalog = "InvalidInitialCatalog_for_retry";
private readonly string _exceedErrMsgPattern = RetryLogicTestHelper.s_exceedErrMsgPattern;
private readonly string _cancelErrMsgPattern = RetryLogicTestHelper.s_cancelErrMsgPattern;
#region Sync
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 2 }, MemberType = typeof(RetryLogicTestHelper), DisableDiscoveryEnumeration = true)]
public void ConnectionRetryOpenInvalidCatalogFailed(string cnnString, SqlRetryLogicBaseProvider provider)
{
int numberOfTries = provider.RetryLogic.NumberOfTries;
int cancelAfterRetries = numberOfTries + 1;
int currentRetries = 0;
provider.Retrying += (s, e) => currentRetries = e.RetryCount;
using (var cnn = CreateConnectionWithInvalidCatalog(cnnString, provider, cancelAfterRetries))
{
var ex = Assert.Throws<AggregateException>(() => cnn.Open());
Assert.Equal(numberOfTries, currentRetries + 1);
Assert.Equal(numberOfTries, ex.InnerExceptions.Count);
Assert.Contains(string.Format(_exceedErrMsgPattern, numberOfTries), ex.Message);
}
}
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 2 }, MemberType = typeof(RetryLogicTestHelper), DisableDiscoveryEnumeration = true)]
public void ConnectionCancelRetryOpenInvalidCatalog(string cnnString, SqlRetryLogicBaseProvider provider)
{
int cancelAfterRetries = provider.RetryLogic.NumberOfTries - 1;
int currentRetries = 0;
provider.Retrying += (s, e) => currentRetries = e.RetryCount;
using (var cnn = CreateConnectionWithInvalidCatalog(cnnString, provider, cancelAfterRetries))
{
var ex = Assert.Throws<AggregateException>(() => cnn.Open());
Assert.Equal(cancelAfterRetries, currentRetries);
Assert.Equal(cancelAfterRetries, ex.InnerExceptions.Count);
Assert.Contains(string.Format(_cancelErrMsgPattern, currentRetries), ex.Message);
}
}
[ActiveIssue("14590", TestPlatforms.Windows)]
// avoid creating a new database in Azure
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsNotAzureServer), nameof(DataTestUtility.IsNotAzureSynapse), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyLongRunner), parameters: new object[] { 10 }, MemberType = typeof(RetryLogicTestHelper), DisableDiscoveryEnumeration = true)]
public void CreateDatabaseWhileTryingToConnect(string cnnString, SqlRetryLogicBaseProvider provider)
{
int currentRetries = 0;
string database = DataTestUtility.GetUniqueNameForSqlServer($"RetryLogic_{provider.RetryLogic.RetryIntervalEnumerator.GetType().Name}", false);
var builder = new SqlConnectionStringBuilder(cnnString)
{
InitialCatalog = database,
ConnectTimeout = 1
};
using (var cnn1 = new SqlConnection(new SqlConnectionStringBuilder(cnnString) { ConnectTimeout = 60, Pooling = false }.ConnectionString))
{
cnn1.Open();
using (var cmd = cnn1.CreateCommand())
{
Task createDBTask = null;
try
{
provider.Retrying += (s, e) =>
{
currentRetries = e.RetryCount;
// Try to create database just after first faliure.
if (createDBTask == null || createDBTask.Status == TaskStatus.Faulted)
{
cmd.CommandText = $"IF (NOT EXISTS(SELECT 1 FROM sys.databases WHERE name = '{database}')) CREATE DATABASE [{database}];";
createDBTask = cmd.ExecuteNonQueryAsync();
}
};
using (var cnn2 = new SqlConnection(builder.ConnectionString))
{
cnn2.RetryLogicProvider = provider;
cnn2.Open();
}
}
finally
{
createDBTask?.Wait();
DataTestUtility.DropDatabase(cnn1, database);
}
}
}
Assert.True(currentRetries > 0);
}
[ActiveIssue("25147")]
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 2 }, MemberType = typeof(RetryLogicTestHelper), DisableDiscoveryEnumeration = true)]
public void ConcurrentExecution(string cnnString, SqlRetryLogicBaseProvider provider)
{
int numberOfTries = provider.RetryLogic.NumberOfTries;
int cancelAfterRetries = numberOfTries + 1;
int retriesCount = 0;
int concurrentExecution = 5;
provider.Retrying += (s, e) => Interlocked.Increment(ref retriesCount);
Parallel.For(0, concurrentExecution,
i =>
{
using (var cnn = CreateConnectionWithInvalidCatalog(cnnString, provider, cancelAfterRetries))
{
Assert.Throws<AggregateException>(() => cnn.Open());
}
});
Assert.Equal(numberOfTries * concurrentExecution, retriesCount + concurrentExecution);
retriesCount = 0;
Parallel.For(0, concurrentExecution,
i =>
{
using (var cnn = CreateConnectionWithInvalidCatalog(cnnString, provider, cancelAfterRetries))
{
Assert.ThrowsAsync<AggregateException>(() => cnn.OpenAsync()).Wait();
}
});
Assert.Equal(numberOfTries * concurrentExecution, retriesCount + concurrentExecution);
}
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(RetryLogicTestHelper.GetNoneRetriableCondition), MemberType = typeof(RetryLogicTestHelper), DisableDiscoveryEnumeration = true)]
public void DefaultOpenWithoutRetry(string connectionString, SqlRetryLogicBaseProvider cnnProvider)
{
var cnnString = new SqlConnectionStringBuilder(connectionString)
{
InitialCatalog = InvalidInitialCatalog
}.ConnectionString;
Assert.Throws<SqlException>(() => new SqlConnection(cnnString).Open());
Assert.ThrowsAsync<SqlException>(() => new SqlConnection(cnnString).OpenAsync()).Wait();
using (var cnn = new SqlConnection(cnnString))
{
cnn.RetryLogicProvider = cnnProvider;
Assert.Throws<SqlException>(() => cnn.Open());
cnn.RetryLogicProvider = cnnProvider;
Assert.ThrowsAsync<SqlException>(() => cnn.OpenAsync()).Wait();
}
}
#endregion
#region Async
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 5 }, MemberType = typeof(RetryLogicTestHelper), DisableDiscoveryEnumeration = true)]
public async void ConnectionRetryOpenAsyncInvalidCatalogFailed(string cnnString, SqlRetryLogicBaseProvider provider)
{
int numberOfTries = provider.RetryLogic.NumberOfTries;
int cancelAfterRetries = numberOfTries + 1;
int currentRetries = 0;
provider.Retrying += (s, e) => currentRetries = e.RetryCount;
using (var cnn = CreateConnectionWithInvalidCatalog(cnnString, provider, cancelAfterRetries))
{
var ex = await Assert.ThrowsAsync<AggregateException>(() => cnn.OpenAsync());
Assert.Equal(numberOfTries, currentRetries + 1);
Assert.Equal(numberOfTries, ex.InnerExceptions.Count);
Assert.Contains(string.Format(_exceedErrMsgPattern, numberOfTries), ex.Message);
}
}
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 2 }, MemberType = typeof(RetryLogicTestHelper), DisableDiscoveryEnumeration = true)]
public async void ConnectionCancelRetryOpenAsyncInvalidCatalog(string cnnString, SqlRetryLogicBaseProvider provider)
{
int numberOfTries = provider.RetryLogic.NumberOfTries;
int cancelAfterRetries = numberOfTries - 1;
int currentRetries = 0;
provider.Retrying += (s, e) => currentRetries = e.RetryCount;
using (var cnn = CreateConnectionWithInvalidCatalog(cnnString, provider, cancelAfterRetries))
{
var ex = await Assert.ThrowsAsync<AggregateException>(() => cnn.OpenAsync());
Assert.Equal(cancelAfterRetries, currentRetries);
Assert.Equal(cancelAfterRetries, ex.InnerExceptions.Count);
Assert.Contains(string.Format(_cancelErrMsgPattern, currentRetries), ex.Message);
}
}
#endregion
#region private members
private SqlConnection CreateConnectionWithInvalidCatalog(string cnnString, SqlRetryLogicBaseProvider provider, int cancelAfterRetries)
{
var builder = new SqlConnectionStringBuilder(cnnString)
{
InitialCatalog = InvalidInitialCatalog
};
SqlConnection cnn = new SqlConnection(builder.ConnectionString);
cnn.RetryLogicProvider = provider;
cnn.RetryLogicProvider.Retrying += (s, e) =>
{
Assert.Equal(e.RetryCount, e.Exceptions.Count);
Assert.NotEqual(TimeSpan.Zero, e.Delay);
if (e.RetryCount >= cancelAfterRetries)
{
e.Cancel = true;
}
};
return cnn;
}
#endregion
}
}