-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathTransactionIsolationLevelTests.cs
More file actions
309 lines (271 loc) · 10.3 KB
/
TransactionIsolationLevelTests.cs
File metadata and controls
309 lines (271 loc) · 10.3 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
using FluentAssertions;
#if NET462
using NEventStore.Diagnostics;
#endif
using NEventStore.Persistence.AcceptanceTests.BDD;
using NEventStore.Persistence.Sql;
using NEventStore.Persistence.Sql.SqlDialects;
using NEventStore.Persistence.Sql.Tests;
using NEventStore.Serialization;
using IsolationLevel = System.Data.IsolationLevel;
#if MSTEST
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
#if NUNIT
using NUnit.Framework;
using System.Data.SqlClient;
#endif
#if XUNIT
using Xunit;
using Xunit.Should;
#endif
namespace NEventStore.Persistence.AcceptanceTests
{
#if MSTEST
[TestClass]
#endif
public class When_reusing_a_connection_from_the_connection_pool_without_a_transaction_scope :
IsolationLevelConcern
{
protected override void Because()
{
using (var conn = ConnectionFactory.Open())
using (conn.BeginTransaction(IsolationLevel.RepeatableRead))
{
}
Recorder.IsRecording = true;
// Enumerate fully to make sure the underlying DB stuff (command/reader etc.) is disposed
Persistence.GetFrom(0).ToArray();
Recorder.IsRecording = false;
}
[Fact]
public void Should_run_command_in_non_default_isolation_level()
{
Recorder.StatementsWithIsolationLevels.Select(i => i.IsolationLevel)
.Should().BeEquivalentTo(new[] {IsolationLevel.ReadCommitted});
}
}
public abstract class IsolationLevelConcern : SpecificationBase, IDisposable
{
private readonly IsolationLevelPersistenceEngineFixture _fixture;
protected IPersistStreams Persistence
{
get { return _fixture.Persistence; }
}
protected IsolationLevelRecorder Recorder
{
get { return _fixture.Recorder; }
}
protected IConnectionFactory ConnectionFactory
{
get { return _fixture.ConnectionFactory; }
}
protected override void Cleanup()
{
_fixture?.Dispose();
}
public void Dispose()
{
_fixture?.Dispose();
}
/// <summary>
/// <para>
/// This code was meant to be run right before every test in the fixture to give time
/// to do further initialization before the PersistenceEngineFixture was created.
/// Unfortunately the 3 frameworks
/// have very different ways of doing this:
/// - NUnit: TestFixtureSetUp
/// - MSTest: ClassInitialize (not inherited, will be ignored if defined on a base class)
/// - xUnit: IUseFixture + SetFixture
/// We need a way to also have some configuration before the PersistenceEngineFixture is created.
/// </para>
/// <para>
/// We'de decided to use the test constructor to do the job, it's your responsibility to guarantee
/// One time initialization (for anything that need it, if you have multiple tests on a fixture)
/// depending on the framework you are using.
/// </para>
/// <para>We can solve the also adding an optional 'config' delegate to be executed as the first line in this base constructor.</para>
/// <para>
/// quick workaround:
/// - the 'Reinitialize()' method can be called to rerun the initialization after we changed the configuration
/// in the constructor
/// </para>
/// </summary>
protected IsolationLevelConcern()
{
_fixture = new IsolationLevelPersistenceEngineFixture();
_fixture.Initialize();
}
}
public class IsolationLevelPersistenceEngineFixture
{
private readonly IsolationLevelRecorder _recorder;
private readonly IConnectionFactory _connectionFactory;
private readonly Func<IPersistStreams> _createPersistence;
private IPersistStreams _persistence;
public IsolationLevelPersistenceEngineFixture()
{
_recorder = new IsolationLevelRecorder();
#if NET462
_connectionFactory = new EnviromentConnectionFactory("MsSql", "System.Data.SqlClient");
#else
_connectionFactory = new EnviromentConnectionFactory("MsSql", System.Data.SqlClient.SqlClientFactory.Instance);
#endif
_createPersistence = () =>
new SqlPersistenceFactory(_connectionFactory,
new BinarySerializer(),
new IsolationLevelRecordingSqlDialect(_recorder)).Build();
}
public void Initialize()
{
if (_persistence?.IsDisposed == false)
{
_persistence.Drop();
_persistence.Dispose();
}
#if NET462
_persistence = new PerformanceCounterPersistenceEngine(_createPersistence(), "tests");
#else
_persistence = _createPersistence();
#endif
_persistence.Initialize();
}
public IPersistStreams Persistence
{
get { return _persistence; }
}
public IsolationLevelRecorder Recorder
{
get { return _recorder; }
}
public IConnectionFactory ConnectionFactory
{
get { return _connectionFactory; }
}
public void Dispose()
{
if (_persistence?.IsDisposed == false)
{
_persistence.Drop();
_persistence.Dispose();
}
}
}
public class StatementAndIsolationLevel
{
public string Statement { get; private set; }
public IsolationLevel IsolationLevel { get; private set; }
public StatementAndIsolationLevel(string statement, IsolationLevel isolationLevel)
{
Statement = statement;
IsolationLevel = isolationLevel;
}
}
public class IsolationLevelRecorder
{
public bool IsRecording { get; set; }
public List<StatementAndIsolationLevel> StatementsWithIsolationLevels { get; private set; }
public IsolationLevelRecorder()
{
StatementsWithIsolationLevels = new List<StatementAndIsolationLevel>();
}
public void RecordIsolationLevel(string statement, IsolationLevel isolationLevel)
{
if (IsRecording)
StatementsWithIsolationLevels.Add(new StatementAndIsolationLevel(statement, isolationLevel));
}
}
internal class IsolationLevelRecordingSqlDialect : MsSqlDialect
{
private readonly IsolationLevelRecorder _recorder;
public IsolationLevelRecordingSqlDialect(IsolationLevelRecorder recorder)
{
_recorder = recorder;
}
public override IDbStatement BuildStatement(
TransactionScope scope,
IDbConnection connection,
IDbTransaction transaction)
{
return new TransactionLevelRecordingStatement(base.BuildStatement(scope, connection, transaction), _recorder);
}
private class TransactionLevelRecordingStatement : IDbStatement
{
private readonly IDbStatement _innerStatement;
private readonly IsolationLevelRecorder _recorder;
public List<StatementAndIsolationLevel> StatementsWithIsolationLevels { get; private set; }
public TransactionLevelRecordingStatement(IDbStatement innerStatement, IsolationLevelRecorder recorder)
{
StatementsWithIsolationLevels = new List<StatementAndIsolationLevel>();
_innerStatement = innerStatement;
_recorder = recorder;
}
public void Dispose()
{
_innerStatement.Dispose();
}
private IsolationLevel GetCurrentIsolationLevel()
{
return
(IsolationLevel)
_innerStatement.ExecuteScalar(
string.Format(@"
SELECT CASE transaction_isolation_level
WHEN 0 THEN {0}
WHEN 1 THEN {1}
WHEN 2 THEN {2}
WHEN 3 THEN {3}
WHEN 4 THEN {4}
WHEN 5 THEN {5}
END AS TRANSACTION_ISOLATION_LEVEL
FROM sys.dm_exec_sessions
where session_id = @@SPID",
(int) IsolationLevel.Unspecified,
(int) IsolationLevel.ReadUncommitted,
(int) IsolationLevel.ReadCommitted,
(int) IsolationLevel.RepeatableRead,
(int) IsolationLevel.Serializable,
(int) IsolationLevel.Snapshot));
}
public void AddParameter(string name, object value, DbType? parameterType = null)
{
_innerStatement.AddParameter(name, value, parameterType);
}
public int ExecuteNonQuery(string commandText)
{
_recorder.RecordIsolationLevel(commandText, GetCurrentIsolationLevel());
return _innerStatement.ExecuteNonQuery(commandText);
}
public int ExecuteWithoutExceptions(string commandText)
{
_recorder.RecordIsolationLevel(commandText, GetCurrentIsolationLevel());
return _innerStatement.ExecuteWithoutExceptions(commandText);
}
public object ExecuteScalar(string commandText)
{
_recorder.RecordIsolationLevel(commandText, GetCurrentIsolationLevel());
return _innerStatement.ExecuteScalar(commandText);
}
public IEnumerable<IDataRecord> ExecuteWithQuery(string queryText)
{
_recorder.RecordIsolationLevel(queryText, GetCurrentIsolationLevel());
return _innerStatement.ExecuteWithQuery(queryText);
}
public IEnumerable<IDataRecord> ExecutePagedQuery(string queryText, NextPageDelegate nextpage)
{
_recorder.RecordIsolationLevel(queryText, GetCurrentIsolationLevel());
return _innerStatement.ExecutePagedQuery(queryText, nextpage);
}
public int PageSize
{
get { return _innerStatement.PageSize; }
set { _innerStatement.PageSize = value; }
}
}
}
}