Skip to content

Commit f1be2e2

Browse files
griffitdfredericDelaporte
authored andcommitted
NH-2176 - Add test case for consecutive transaction scopes.
1 parent 4d99b2b commit f1be2e2

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Transactions;
13+
using NUnit.Framework;
14+
15+
namespace NHibernate.Test.NHSpecificTest.NH2176
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
protected override void OnSetUp()
22+
{
23+
base.OnSetUp();
24+
using (var s = OpenSession())
25+
using (var tx = s.BeginTransaction())
26+
{
27+
var steve = new Person {Name = "Steve"};
28+
var peter = new Person {Name = "Peter"};
29+
var simon = new Person {Name = "Simon"};
30+
var paul = new Person {Name = "Paul"};
31+
var john = new Person {Name = "John"};
32+
var eric = new Person {Name = "Eric"};
33+
34+
s.Save(steve);
35+
s.Save(peter);
36+
s.Save(simon);
37+
s.Save(paul);
38+
s.Save(john);
39+
s.Save(eric);
40+
41+
tx.Commit();
42+
}
43+
}
44+
45+
protected override void OnTearDown()
46+
{
47+
base.OnTearDown();
48+
using (var s = OpenSession())
49+
using (var tx = s.BeginTransaction())
50+
{
51+
s.Delete("from Person");
52+
tx.Commit();
53+
}
54+
}
55+
56+
// Whilst this bug seems specific to Oracle I think it is valid to run the
57+
// test against all database types.
58+
[Test]
59+
public async Task MultipleConsecutiveTransactionScopesCanBeUsedInsideASingleSessionAsync()
60+
{
61+
using (var s = OpenSession())
62+
{
63+
// usually fails after just a few loops in oracle
64+
// this can be run for 10000 loops in sql server without problem
65+
for (var i = 0; i < 100; ++i)
66+
{
67+
Console.WriteLine(i.ToString());
68+
69+
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
70+
{
71+
var criteria = s.CreateCriteria<Person>();
72+
var people = await (criteria.ListAsync<Person>());
73+
74+
Assert.That(people.Count, Is.EqualTo(6));
75+
76+
scope.Complete();
77+
}
78+
79+
// The exeption is caused by a race condition between two threads.
80+
// This can be demonstracted by uncommenting the following line which
81+
// causes the test to run without an exception.
82+
//System.Threading.Thread.Sleep(1000);
83+
}
84+
}
85+
}
86+
}
87+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Transactions;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH2176
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
protected override void OnSetUp()
11+
{
12+
base.OnSetUp();
13+
using (var s = OpenSession())
14+
using (var tx = s.BeginTransaction())
15+
{
16+
var steve = new Person {Name = "Steve"};
17+
var peter = new Person {Name = "Peter"};
18+
var simon = new Person {Name = "Simon"};
19+
var paul = new Person {Name = "Paul"};
20+
var john = new Person {Name = "John"};
21+
var eric = new Person {Name = "Eric"};
22+
23+
s.Save(steve);
24+
s.Save(peter);
25+
s.Save(simon);
26+
s.Save(paul);
27+
s.Save(john);
28+
s.Save(eric);
29+
30+
tx.Commit();
31+
}
32+
}
33+
34+
protected override void OnTearDown()
35+
{
36+
base.OnTearDown();
37+
using (var s = OpenSession())
38+
using (var tx = s.BeginTransaction())
39+
{
40+
s.Delete("from Person");
41+
tx.Commit();
42+
}
43+
}
44+
45+
// Whilst this bug seems specific to Oracle I think it is valid to run the
46+
// test against all database types.
47+
[Test]
48+
public void MultipleConsecutiveTransactionScopesCanBeUsedInsideASingleSession()
49+
{
50+
using (var s = OpenSession())
51+
{
52+
// usually fails after just a few loops in oracle
53+
// this can be run for 10000 loops in sql server without problem
54+
for (var i = 0; i < 100; ++i)
55+
{
56+
Console.WriteLine(i.ToString());
57+
58+
using (var scope = new TransactionScope())
59+
{
60+
var criteria = s.CreateCriteria<Person>();
61+
var people = criteria.List<Person>();
62+
63+
Assert.That(people.Count, Is.EqualTo(6));
64+
65+
scope.Complete();
66+
}
67+
68+
// The exeption is caused by a race condition between two threads.
69+
// This can be demonstracted by uncommenting the following line which
70+
// causes the test to run without an exception.
71+
//System.Threading.Thread.Sleep(1000);
72+
}
73+
}
74+
}
75+
}
76+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.NHSpecificTest.NH2176"
4+
assembly="NHibernate.Test"
5+
>
6+
<class name="Person" table="NH2176_Person">
7+
8+
<id name="Id" column="PersonId">
9+
<generator class="identity"/>
10+
</id>
11+
12+
<property name="Name" column="Name" />
13+
14+
</class>
15+
</hibernate-mapping>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH2176
2+
{
3+
public class Person
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
}
8+
}

0 commit comments

Comments
 (0)