forked from dotnet/SqlClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactionEnlistmentTest.cs
241 lines (210 loc) · 10.4 KB
/
TransactionEnlistmentTest.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
// 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.Data;
using System.Threading.Tasks;
using System.Transactions;
using Xunit;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public class TransactionEnlistmentTest
{
// TODO Synapse: Cannot find data type 'text'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public static void TestAutoEnlistment_TxScopeComplete()
{
RunTestSet(TestCase_AutoEnlistment_TxScopeComplete);
}
// TODO Synapse: Cannot find data type 'text'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public static void TestAutoEnlistment_TxScopeNonComplete()
{
RunTestSet(TestCase_AutoEnlistment_TxScopeNonComplete);
}
// TODO Synapse: Cannot find data type 'text'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void TestManualEnlistment_Enlist()
{
RunTestSet(TestCase_ManualEnlistment_Enlist);
}
// TODO Synapse: Cannot find data type 'text'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void TestManualEnlistment_NonEnlist()
{
RunTestSet(TestCase_ManualEnlistment_NonEnlist);
}
// TODO Synapse: Cannot find data type 'text'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void TestManualEnlistment_Enlist_TxScopeComplete()
{
RunTestSet(TestCase_ManualEnlistment_Enlist_TxScopeComplete);
}
[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public static void TestEnlistmentPrepare_TxScopeComplete()
{
try
{
using TransactionScope txScope = new(TransactionScopeOption.RequiresNew, new TransactionOptions()
{
IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted,
Timeout = TransactionManager.DefaultTimeout
}, TransactionScopeAsyncFlowOption.Enabled);
using SqlConnection connection = new(DataTestUtility.TCPConnectionString);
connection.Open();
System.Transactions.Transaction.Current.EnlistDurable(EnlistmentForPrepare.s_id, new EnlistmentForPrepare(), EnlistmentOptions.None);
txScope.Complete();
Assert.False(true, "Expected exception not thrown.");
}
catch (Exception e)
{
Assert.True(e is TransactionAbortedException);
}
}
private static void TestCase_AutoEnlistment_TxScopeComplete()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
builder.Enlist = true;
ConnectionString = builder.ConnectionString;
using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
command.ExecuteNonQuery();
}
}
txScope.Complete();
}
DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
Assert.True(result.Rows.Count == 1);
Assert.True(string.Equals(result.Rows[0][0], InputCol2));
}
private static void TestCase_AutoEnlistment_TxScopeNonComplete()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
builder.Enlist = true;
ConnectionString = builder.ConnectionString;
using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
command.ExecuteNonQuery();
}
}
}
DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
Assert.True(result.Rows.Count == 0);
}
private static void TestCase_ManualEnlistment_Enlist()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
builder.Enlist = false;
ConnectionString = builder.ConnectionString;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (TransactionScope txScope = new TransactionScope())
{
connection.EnlistTransaction(System.Transactions.Transaction.Current);
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
command.ExecuteNonQuery();
}
}
}
DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
Assert.True(result.Rows.Count == 0);
}
private static void TestCase_ManualEnlistment_NonEnlist()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
builder.Enlist = false;
ConnectionString = builder.ConnectionString;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (TransactionScope txScope = new TransactionScope())
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
command.ExecuteNonQuery();
}
}
}
DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
Assert.True(result.Rows.Count == 1);
Assert.True(string.Equals(result.Rows[0][0], InputCol2));
}
private static void TestCase_ManualEnlistment_Enlist_TxScopeComplete()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
builder.Enlist = false;
ConnectionString = builder.ConnectionString;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (TransactionScope txScope = new TransactionScope())
{
connection.EnlistTransaction(System.Transactions.Transaction.Current);
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
command.ExecuteNonQuery();
}
txScope.Complete();
}
}
DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
Assert.True(result.Rows.Count == 1);
Assert.True(string.Equals(result.Rows[0][0], InputCol2));
}
class EnlistmentForPrepare : IEnlistmentNotification
{
public static readonly Guid s_id = Guid.NewGuid();
// fail during prepare, this will cause scope.Complete to throw
public void Prepare(PreparingEnlistment preparingEnlistment) => preparingEnlistment.ForceRollback();
public void Commit(Enlistment enlistment) => enlistment.Done();
public void Rollback(Enlistment enlistment) => enlistment.Done();
public void InDoubt(Enlistment enlistment) => enlistment.Done();
}
private static string TestTableName;
private static string ConnectionString;
private const int InputCol1 = 1;
private const string InputCol2 = "One";
private static void RunTestSet(Action TestCase)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString);
builder.ConnectTimeout = 5;
builder.Pooling = true;
ConnectionString = builder.ConnectionString;
RunTestFormat(TestCase);
builder.Pooling = false;
ConnectionString = builder.ConnectionString;
RunTestFormat(TestCase);
}
private static void RunTestFormat(Action testCase)
{
TestTableName = DataTestUtility.GenerateObjectName();
DataTestUtility.RunNonQuery(ConnectionString, $"create table {TestTableName} (col1 int, col2 text)");
try
{
testCase();
}
finally
{
DataTestUtility.RunNonQuery(ConnectionString, $"drop table {TestTableName}");
}
}
}
}