FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
npgsql/test/Npgsql.Tests/SystemTransactionTests.cs at develop · synthesis85/npgsql · GitHub
synthesis85
/
npgsql
Public
forked from
npgsql/npgsql
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
npgsql
/
test
/
Npgsql.Tests
/
SystemTransactionTests.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
294 lines (268 loc) · 12.7 KB
Breadcrumbs
npgsql
/
test
/
Npgsql.Tests
/
SystemTransactionTests.cs
Copy path
File metadata and controls
294 lines (268 loc) · 12.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#region License
// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using
System
;
using
System
.
Data
;
using
System
.
Reflection
;
using
System
.
Transactions
;
using
Npgsql
;
using
NpgsqlTypes
;
using
NUnit
.
Framework
;
namespace
Npgsql
.
Tests
{
[
Parallelizable
(
ParallelScope
.
None
)
]
public
class
SystemTransactionTests
:
TestBase
{
public
SystemTransactionTests
(
string
backendVersion
)
:
base
(
backendVersion
)
{
}
[
Test
,
Description
(
"Single connection enlisting explicitly, committing"
)
]
public
void
ExplicitEnlist
(
)
{
using
(
var
conn
=
new
NpgsqlConnection
(
ConnectionString
)
)
using
(
var
scope
=
new
TransactionScope
(
)
)
{
conn
.
Open
(
)
;
conn
.
EnlistTransaction
(
Transaction
.
Current
)
;
Assert
.
That
(
conn
.
ExecuteNonQuery
(
@"INSERT INTO data (name) VALUES('test')"
)
,
Is
.
EqualTo
(
1
)
)
;
scope
.
Complete
(
)
;
}
AssertNoPreparedTransactions
(
)
;
using
(
var
conn
=
OpenConnection
(
)
)
Assert
.
That
(
conn
.
ExecuteScalar
(
@"SELECT COUNT(*) FROM data"
)
,
Is
.
EqualTo
(
1
)
)
;
}
[
Test
,
Description
(
"Single connection enlisting implicitly, committing"
)
]
public
void
ImplicitEnlist
(
)
{
var
connectionString
=
ConnectionString
+
";enlist=true"
;
using
(
var
scope
=
new
TransactionScope
(
)
)
{
using
(
var
conn
=
new
NpgsqlConnection
(
connectionString
)
)
{
conn
.
Open
(
)
;
Assert
.
That
(
conn
.
ExecuteNonQuery
(
@"INSERT INTO data (name) VALUES('test')"
)
,
Is
.
EqualTo
(
1
)
)
;
}
scope
.
Complete
(
)
;
}
AssertNoPreparedTransactions
(
)
;
using
(
var
conn
=
OpenConnection
(
)
)
Assert
.
That
(
conn
.
ExecuteScalar
(
@"SELECT COUNT(*) FROM data"
)
,
Is
.
EqualTo
(
1
)
)
;
}
[
Test
,
Description
(
"Single connection rollback"
)
]
public
void
Rollback
(
)
{
var
connectionString
=
ConnectionString
+
";enlist=true"
;
using
(
var
scope
=
new
TransactionScope
(
)
)
using
(
var
conn
=
new
NpgsqlConnection
(
connectionString
)
)
{
conn
.
Open
(
)
;
Assert
.
That
(
conn
.
ExecuteNonQuery
(
@"INSERT INTO data (name) VALUES('test')"
)
,
Is
.
EqualTo
(
1
)
)
;
// No commit
}
AssertNoPreparedTransactions
(
)
;
using
(
var
conn
=
OpenConnection
(
)
)
Assert
.
That
(
conn
.
ExecuteScalar
(
@"SELECT COUNT(*) FROM data"
)
,
Is
.
EqualTo
(
0
)
)
;
}
[
Test
]
[
Ignore
(
"Causing a weird timeout issue for other tests? Also clean up..."
)
]
public
void
DistributedTransactionRollback
(
)
{
int
field_serial1
;
int
field_serial2
;
var
connectionString
=
ConnectionString
+
";enlist=true"
;
using
(
var
scope
=
new
TransactionScope
(
)
)
{
//UseStringParameterWithNoNpgsqlDbType
using
(
var
connection
=
new
NpgsqlConnection
(
connectionString
)
)
{
connection
.
Open
(
)
;
var
command
=
new
NpgsqlCommand
(
"insert into data (field_text) values (:p0)"
,
connection
)
;
command
.
Parameters
.
Add
(
new
NpgsqlParameter
(
"p0"
,
"test"
)
)
;
Assert
.
AreEqual
(
command
.
Parameters
[
0
]
.
NpgsqlDbType
,
NpgsqlDbType
.
Text
)
;
Assert
.
AreEqual
(
command
.
Parameters
[
0
]
.
DbType
,
DbType
.
String
)
;
object
result
=
command
.
ExecuteNonQuery
(
)
;
Assert
.
AreEqual
(
1
,
result
)
;
field_serial1
=
(
int
)
new
NpgsqlCommand
(
"select max(field_serial) from data"
,
connection
)
.
ExecuteScalar
(
)
;
var
command2
=
new
NpgsqlCommand
(
"select field_text from data where field_serial = (select max(field_serial) from data)"
,
connection
)
;
result
=
command2
.
ExecuteScalar
(
)
;
Assert
.
AreEqual
(
"test"
,
result
)
;
}
//UseIntegerParameterWithNoNpgsqlDbType
using
(
var
connection
=
new
NpgsqlConnection
(
connectionString
)
)
{
connection
.
Open
(
)
;
var
command
=
new
NpgsqlCommand
(
"insert into data(field_int4) values (:p0)"
,
connection
)
;
command
.
Parameters
.
Add
(
new
NpgsqlParameter
(
"p0"
,
5
)
)
;
Assert
.
AreEqual
(
command
.
Parameters
[
0
]
.
NpgsqlDbType
,
NpgsqlDbType
.
Integer
)
;
Assert
.
AreEqual
(
command
.
Parameters
[
0
]
.
DbType
,
DbType
.
Int32
)
;
Object
result
=
command
.
ExecuteNonQuery
(
)
;
Assert
.
AreEqual
(
1
,
result
)
;
field_serial2
=
(
int
)
new
NpgsqlCommand
(
"select max(field_serial) from data"
,
connection
)
.
ExecuteScalar
(
)
;
var
command2
=
new
NpgsqlCommand
(
"select field_int4 from data where field_serial = (select max(field_serial) from data)"
,
connection
)
;
result
=
command2
.
ExecuteScalar
(
)
;
Assert
.
AreEqual
(
5
,
result
)
;
// using new connection here... make sure we can't see previous results even though
// it is the same distributed transaction
var
command3
=
new
NpgsqlCommand
(
"select field_text from data where field_serial = :p0"
,
connection
)
;
command3
.
Parameters
.
Add
(
new
NpgsqlParameter
(
"p0"
,
field_serial1
)
)
;
result
=
command3
.
ExecuteScalar
(
)
;
// won't see value of "test" since that's
// another connection
Assert
.
AreEqual
(
null
,
result
)
;
}
// not commiting here.
}
// This is an attempt to wait for the distributed transaction to rollback
// not guaranteed to work, but should be good enough for testing purposes.
System
.
Threading
.
Thread
.
Sleep
(
500
)
;
AssertNoPreparedTransactions
(
)
;
// ensure they no longer exist since we rolled back
AssertRowNotExist
(
"field_text"
,
field_serial1
)
;
AssertRowNotExist
(
"field_int4"
,
field_serial2
)
;
}
[
Test
]
[
Ignore
(
"Causing a weird timeout issue for other tests? Also clean up..."
)
]
public
void
TwoDistributedInSequence
(
)
{
DistributedTransactionRollback
(
)
;
DistributedTransactionRollback
(
)
;
}
[
Test
,
Description
(
"Makes sure that when a timeout occurs, the transaction is rolled backed"
)
]
[
IssueLink
(
"https://github.com/npgsql/npgsql/issues/495"
)
]
[
MonoIgnore
(
"Mono's TransactionScope doesn't enforce the timeout: https://bugzilla.xamarin.com/show_bug.cgi?id=31197"
)
]
public
void
RollbackOnTimeout
(
)
{
Assert
.
That
(
(
)
=>
{
using
(
var
scope
=
new
TransactionScope
(
TransactionScopeOption
.
Required
,
new
TimeSpan
(
0
,
0
,
1
)
)
)
{
using
(
var
conn
=
new
NpgsqlConnection
(
ConnectionString
+
";enlist=true"
)
)
{
conn
.
Open
(
)
;
var
cmd
=
new
NpgsqlCommand
(
@"INSERT INTO data (name) VALUES ('HELLO')"
,
conn
)
;
cmd
.
ExecuteNonQuery
(
)
;
// the update operation is expected to rollback
System
.
Threading
.
Thread
.
Sleep
(
2000
)
;
}
scope
.
Complete
(
)
;
}
}
,
Throws
.
Exception
.
AssignableTo
<
TransactionException
>
(
)
)
;
using
(
var
conn
=
OpenConnection
(
)
)
Assert
.
That
(
conn
.
ExecuteScalar
(
"SELECT COUNT(*) FROM data"
)
,
Is
.
EqualTo
(
0
)
)
;
}
[
Test
,
Description
(
"Not sure what this test is supposed to check..."
)
]
[
Ignore
(
"There's a real failure here"
)
]
public
void
FunctionTestTimestamptzParameterSupport
(
)
{
/*
ExecuteNonQuery(@"INSERT INTO data (name) VALUES ('X')");
ExecuteNonQuery(@"CREATE OR REPLACE FUNCTION testtimestamptzparameter(timestamptz) returns refcursor as
$BODY$
declare ref refcursor;
begin
open ref for select * from data;
return ref;
end
$BODY$
language 'plpgsql' volatile called on null input security invoker;");
var connectionString = ConnectionString + ";enlist=true";
using (var scope = new TransactionScope())
{
using (var connection = new NpgsqlConnection(connectionString))
{
connection.Open();
var command = new NpgsqlCommand("testtimestamptzparameter", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new NpgsqlParameter("p1", NpgsqlDbType.TimestampTZ));
//NpgsqlDataReader dr = command.ExecuteReader();
//Int32 count = 0;
//while (dr.Read())
//count++;
var da = new NpgsqlDataAdapter(command);
var dt = new DataTable();
da.Fill(dt);
Assert.AreEqual(1, dt.Rows.Count);
}
}
*/
}
private
void
AssertNoPreparedTransactions
(
)
{
using
(
var
conn
=
OpenConnection
(
)
)
using
(
var
cmd
=
new
NpgsqlCommand
(
"select count(*) from pg_prepared_xacts where database = :database"
,
conn
)
)
{
cmd
.
Parameters
.
Add
(
new
NpgsqlParameter
(
"database"
,
conn
.
Database
)
)
;
Assert
.
That
(
cmd
.
ExecuteScalar
(
)
,
Is
.
EqualTo
(
0
)
)
;
}
}
private
void
AssertRowNotExist
(
string
columnName
,
int
field_serial
)
{
using
(
var
conn
=
OpenConnection
(
)
)
using
(
var
cmd
=
new
NpgsqlCommand
(
"select "
+
columnName
+
" from data where field_serial = :p0"
,
conn
)
)
{
cmd
.
Parameters
.
Add
(
new
NpgsqlParameter
(
"p0"
,
field_serial
)
)
;
Assert
.
That
(
cmd
.
ExecuteScalar
(
)
,
Is
.
Null
)
;
}
}
[
Test
]
public
void
ReuseConnection
(
)
{
var
connectionString
=
ConnectionString
+
";enlist=true"
;
using
(
var
scope
=
new
TransactionScope
(
)
)
{
using
(
var
conn
=
new
NpgsqlConnection
(
connectionString
)
)
{
conn
.
Open
(
)
;
conn
.
Close
(
)
;
conn
.
Open
(
)
;
conn
.
Close
(
)
;
}
}
}
#region Setup
[
SetUp
]
public
void
SetUp
(
)
{
using
(
var
s
=
new
TransactionScope
(
TransactionScopeOption
.
RequiresNew
)
)
{
try
{
Transaction
.
Current
.
EnlistPromotableSinglePhase
(
new
FakePromotableSinglePhaseNotification
(
)
)
;
}
catch
(
NotImplementedException
)
{
Assert
.
Ignore
(
"Promotable single phase transactions aren't supported (mono < 3.0.0?)"
)
;
}
}
using
(
var
conn
=
OpenConnection
(
)
)
{
conn
.
ExecuteNonQuery
(
"DROP TABLE IF EXISTS data"
)
;
conn
.
ExecuteNonQuery
(
"CREATE TABLE data (name TEXT)"
)
;
}
}
class
FakePromotableSinglePhaseNotification
:
IPromotableSinglePhaseNotification
{
public
byte
[
]
Promote
(
)
{
return
null
;
}
public
void
Initialize
(
)
{
}
public
void
SinglePhaseCommit
(
SinglePhaseEnlistment
singlePhaseEnlistment
)
{
}
public
void
Rollback
(
SinglePhaseEnlistment
singlePhaseEnlistment
)
{
}
}
#endregion
}
}
Back
|
FazBrowse Home
|
New Git URL