Skip to content

Commit c293184

Browse files
oskarbhazzik
authored andcommitted
QueryLock.cs: Strengthen asserts to verify actual locking has taken place.
1 parent a8369c2 commit c293184

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

src/NHibernate.Test/Linq/QueryLock.cs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Linq;
2-
using NHibernate.AdoNet;
3-
using NHibernate.Cfg;
4-
using NHibernate.Engine;
2+
using System.Transactions;
3+
using NHibernate.DomainModel.Northwind.Entities;
4+
using NHibernate.Exceptions;
55
using NHibernate.Linq;
66
using NUnit.Framework;
77

8+
89
namespace NHibernate.Test.Linq
910
{
1011
public class QueryLock : LinqTestCase
@@ -13,35 +14,69 @@ public class QueryLock : LinqTestCase
1314
[Test]
1415
public void CanSetLockLinqQueries()
1516
{
16-
var result = (from e in db.Customers
17-
where e.CompanyName == "Corp"
18-
select e).SetLockMode(LockMode.Upgrade).ToList();
17+
using (session.BeginTransaction())
18+
{
19+
var result = (from e in db.Customers
20+
select e).SetLockMode(LockMode.Upgrade).ToList();
1921

22+
Assert.That(result, Has.Count.EqualTo(91));
23+
Assert.That(session.GetCurrentLockMode(result[0]), Is.EqualTo(LockMode.Upgrade));
24+
AssertSeparateTransactionIsLockedOut(result[0].CustomerId);
25+
}
2026
}
2127

2228

2329
[Test]
2430
public void CanSetLockOnLinqPagingQuery()
2531
{
26-
var result = (from e in db.Customers
27-
where e.CompanyName == "Corp"
28-
select e).Skip(5).Take(5).SetLockMode(LockMode.Upgrade).ToList();
32+
using (session.BeginTransaction())
33+
{
34+
var result = (from e in db.Customers
35+
select e).Skip(5).Take(5).SetLockMode(LockMode.Upgrade).ToList();
36+
37+
Assert.That(result, Has.Count.EqualTo(5));
38+
Assert.That(session.GetCurrentLockMode(result[0]), Is.EqualTo(LockMode.Upgrade));
39+
AssertSeparateTransactionIsLockedOut(result[0].CustomerId);
40+
}
2941
}
3042

3143

3244
[Test]
3345
public void CanLockBeforeSkipOnLinqOrderedPageQuery()
3446
{
35-
var result = (from e in db.Customers
36-
orderby e.CompanyName
37-
select e)
38-
.SetLockMode(LockMode.Upgrade).Skip(5).Take(5).ToList();
39-
47+
using (session.BeginTransaction())
48+
{
49+
var result = (from e in db.Customers
50+
orderby e.CompanyName
51+
select e)
52+
.SetLockMode(LockMode.Upgrade).Skip(5).Take(5).ToList();
4053

54+
Assert.That(result, Has.Count.EqualTo(5));
55+
Assert.That(session.GetCurrentLockMode(result[0]), Is.EqualTo(LockMode.Upgrade));
56+
AssertSeparateTransactionIsLockedOut(result[0].CustomerId);
57+
}
4158
}
4259

4360

61+
private void AssertSeparateTransactionIsLockedOut(string customerId)
62+
{
63+
using (new TransactionScope(TransactionScopeOption.Suppress))
64+
using (var s2 = OpenSession())
65+
using (s2.BeginTransaction())
66+
{
67+
// TODO: We should try to verify that the exception actually IS a locking failure and not something unrelated.
68+
Assert.Throws<GenericADOException>(
69+
() =>
70+
{
71+
var result2 = (from e in s2.Query<Customer>()
72+
where e.CustomerId == customerId
73+
select e).SetLockMode(LockMode.UpgradeNoWait)
74+
.Timeout(5).ToList();
75+
Assert.IsNotNull(result2);
76+
}, "Expected an exception to indicate locking failure due to already locked.");
77+
}
78+
}
4479
}
45-
4680
}
4781

82+

0 commit comments

Comments
 (0)