-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
SqlServerDatabaseCreatorTest.cs
267 lines (201 loc) · 9.63 KB
/
SqlServerDatabaseCreatorTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
// ReSharper disable UnassignedGetOnlyAutoProperty
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable MemberCanBePrivate.Local
namespace Microsoft.EntityFrameworkCore.Storage.Internal;
public class SqlServerDatabaseCreatorTest
{
[ConditionalFact]
public Task Create_checks_for_existence_and_retries_if_no_proccess_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(233, async: false);
[ConditionalFact]
public Task Create_checks_for_existence_and_retries_if_timeout_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(-2, async: false);
[ConditionalFact]
public Task Create_checks_for_existence_and_retries_if_cannot_open_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(4060, async: false);
[ConditionalFact]
public Task Create_checks_for_existence_and_retries_if_cannot_attach_file_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(1832, async: false);
[ConditionalFact]
public Task Create_checks_for_existence_and_retries_if_cannot_open_file_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(5120, async: false);
[ConditionalFact]
public Task CreateAsync_checks_for_existence_and_retries_if_no_proccess_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(233, async: true);
[ConditionalFact]
public Task CreateAsync_checks_for_existence_and_retries_if_timeout_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(-2, async: true);
[ConditionalFact]
public Task CreateAsync_checks_for_existence_and_retries_if_cannot_open_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(4060, async: true);
[ConditionalFact]
public Task CreateAsync_checks_for_existence_and_retries_if_cannot_attach_file_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(1832, async: true);
[ConditionalFact]
public Task CreateAsync_checks_for_existence_and_retries_if_cannot_open_file_until_it_passes()
=> Create_checks_for_existence_and_retries_until_it_passes(5120, async: true);
private async Task Create_checks_for_existence_and_retries_until_it_passes(int errorNumber, bool async)
{
var customServices = new ServiceCollection()
.AddScoped<ISqlServerConnection, FakeSqlServerConnection>()
.AddScoped<IRelationalCommandBuilderFactory, FakeRelationalCommandBuilderFactory>()
.AddScoped<IExecutionStrategyFactory, ExecutionStrategyFactory>();
var contextServices = SqlServerTestHelpers.Instance.CreateContextServices(customServices);
var connection = (FakeSqlServerConnection)contextServices.GetRequiredService<ISqlServerConnection>();
connection.ErrorNumber = errorNumber;
connection.FailureCount = 2;
var creator = (SqlServerDatabaseCreator)contextServices.GetRequiredService<IRelationalDatabaseCreator>();
creator.RetryDelay = TimeSpan.FromMilliseconds(1);
creator.RetryTimeout = TimeSpan.FromMinutes(5);
if (async)
{
await creator.CreateAsync();
}
else
{
creator.Create();
}
Assert.Equal(2, connection.OpenCount);
}
[ConditionalFact]
public Task Create_checks_for_existence_and_ultimately_gives_up_waiting()
=> Create_checks_for_existence_and_ultimately_gives_up_waiting_test(async: false);
[ConditionalFact]
public Task CreateAsync_checks_for_existence_and_ultimately_gives_up_waiting()
=> Create_checks_for_existence_and_ultimately_gives_up_waiting_test(async: true);
private async Task Create_checks_for_existence_and_ultimately_gives_up_waiting_test(bool async)
{
var customServices = new ServiceCollection()
.AddScoped<ISqlServerConnection, FakeSqlServerConnection>()
.AddScoped<IRelationalCommandBuilderFactory, FakeRelationalCommandBuilderFactory>()
.AddScoped<IExecutionStrategyFactory, ExecutionStrategyFactory>();
var contextServices = SqlServerTestHelpers.Instance.CreateContextServices(customServices);
var connection = (FakeSqlServerConnection)contextServices.GetRequiredService<ISqlServerConnection>();
connection.ErrorNumber = 233;
connection.FailureCount = 100;
connection.FailDelay = 50;
var creator = (SqlServerDatabaseCreator)contextServices.GetRequiredService<IRelationalDatabaseCreator>();
creator.RetryDelay = TimeSpan.FromMilliseconds(5);
creator.RetryTimeout = TimeSpan.FromMilliseconds(100);
if (async)
{
await Assert.ThrowsAsync<SqlException>(() => creator.CreateAsync());
}
else
{
Assert.Throws<SqlException>(() => creator.Create());
}
}
private class FakeSqlServerConnection(IDbContextOptions options, RelationalConnectionDependencies dependencies) : SqlServerConnection(dependencies)
{
private readonly IDbContextOptions _options = options;
public int ErrorNumber { get; set; }
public int FailureCount { get; set; }
public int FailDelay { get; set; }
public int OpenCount { get; set; }
public override bool Open(bool errorsExpected = false)
{
if (++OpenCount < FailureCount)
{
Thread.Sleep(FailDelay);
throw SqlExceptionFactory.CreateSqlException(ErrorNumber);
}
return true;
}
public override async Task<bool> OpenAsync(CancellationToken cancellationToken, bool errorsExpected = false)
{
if (++OpenCount < FailureCount)
{
await Task.Delay(FailDelay, cancellationToken);
throw SqlExceptionFactory.CreateSqlException(ErrorNumber);
}
return await Task.FromResult(true);
}
public override ISqlServerConnection CreateMasterConnection()
=> new FakeSqlServerConnection(_options, Dependencies);
}
private class FakeRelationalCommandBuilderFactory : IRelationalCommandBuilderFactory
{
public IRelationalCommandBuilder Create()
=> new FakeRelationalCommandBuilder();
}
private class FakeRelationalCommandBuilder : IRelationalCommandBuilder
{
private readonly List<IRelationalParameter> _parameters = [];
public IndentedStringBuilder Instance { get; } = new();
public IReadOnlyList<IRelationalParameter> Parameters
=> _parameters;
public IRelationalCommandBuilder AddParameter(IRelationalParameter parameter)
{
_parameters.Add(parameter);
return this;
}
public IRelationalCommandBuilder RemoveParameterAt(int index)
{
_parameters.RemoveAt(index);
return this;
}
public IRelationalTypeMappingSource TypeMappingSource
=> null;
public IRelationalCommand Build()
=> new FakeRelationalCommand();
public IRelationalCommandBuilder Append(string value)
{
Instance.Append(value);
return this;
}
public IRelationalCommandBuilder AppendLine()
{
Instance.AppendLine();
return this;
}
public IRelationalCommandBuilder IncrementIndent()
{
Instance.IncrementIndent();
return this;
}
public IRelationalCommandBuilder DecrementIndent()
{
Instance.DecrementIndent();
return this;
}
public int CommandTextLength
=> Instance.Length;
}
private class FakeRelationalCommand : IRelationalCommand
{
public string CommandText { get; }
public IReadOnlyList<IRelationalParameter> Parameters { get; }
public IReadOnlyDictionary<string, object> ParameterValues
=> throw new NotImplementedException();
public int ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
=> 0;
public Task<int> ExecuteNonQueryAsync(
RelationalCommandParameterObject parameterObject,
CancellationToken cancellationToken = default)
=> Task.FromResult(0);
public RelationalDataReader ExecuteReader(RelationalCommandParameterObject parameterObject)
=> throw new NotImplementedException();
public Task<RelationalDataReader> ExecuteReaderAsync(
RelationalCommandParameterObject parameterObject,
CancellationToken cancellationToken = default)
=> throw new NotImplementedException();
public DbCommand CreateDbCommand(
RelationalCommandParameterObject parameterObject,
Guid commandId,
DbCommandMethod commandMethod)
=> throw new NotImplementedException();
public object ExecuteScalar(RelationalCommandParameterObject parameterObject)
=> throw new NotImplementedException();
public Task<object> ExecuteScalarAsync(
RelationalCommandParameterObject parameterObject,
CancellationToken cancellationToken = default)
=> throw new NotImplementedException();
public void PopulateFrom(IRelationalCommandTemplate commandTemplate)
=> throw new NotImplementedException();
}
}