forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionSessionLeakTest.cs
101 lines (86 loc) · 3.19 KB
/
ExpressionSessionLeakTest.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
using System;
using System.Linq;
using NHibernate.DomainModel.Northwind.Entities;
using NHibernate.Linq;
using NUnit.Framework;
namespace NHibernate.Test.Linq
{
[TestFixture]
public class ExpressionSessionLeakTest : LinqTestCase
{
[Test]
public void SessionGetsCollected()
{
var reference = DoLinqInSeparateSession();
GC.Collect();
Assert.That(reference.IsAlive, Is.False);
}
private WeakReference DoLinqInSeparateSession()
{
// Using SessionFactory of another session dodge TestCase session tracking, which is needed for having session garbage collected
// Otherwise DebugSessionFactory session tracking should be changed to use WeakReference too.
using (var leakTest = session.SessionFactory.OpenSession())
{
// It appears linq expressions will (or might) contain a reference to the
// IQueryable. At time of writing, linq expressions are held within NhLinqExpression,
// which in turn will be held in the query plan cache. Since the IQueryable will
// be an NhQueryable, which holds a reference to the SessionImpl instance,
// we will be leaking session instances.
var query = leakTest.Query<Customer>().FirstOrDefault(t => t.CustomerId != "");
return new WeakReference(leakTest, false);
}
}
[Theory]
public void SessionIsNotNullOrResurrected(bool? disposeSession)
{
// Must do in a separated method, local variables seem not collected otherwise.
var provider = GetProviderFromSeparateSession(disposeSession);
if (provider == null)
Assert.Ignore("Another query provider than NHibernate default one is used");
// Force collection of no more strongly referenced objects.
// Do not wait for pending finalizers
GC.Collect();
try
{
var s = provider.Session;
Assert.Fail($"Getting provider Session property did not failed. Obtained {(s == null ? "null" : s.GetType().Name)}.");
}
catch (Exception e)
{
Assert.That(e, Is.TypeOf<InvalidOperationException>().And.Message.Contains("garbage coll"));
}
}
[Theory]
public void QueryFailsProperlyOnDereferencedSession(bool? disposeSession)
{
// Must do in a separated method, local variables seem not collected otherwise.
var query = GetQueryFromSeparateSession(disposeSession);
// Force collection of no more strongly referenced objects.
// Do not wait for pending finalizers
GC.Collect();
Assert.That(() => query.FirstOrDefault(), Throws.InvalidOperationException.And.Message.Contains("garbage coll"));
}
IQueryable<Customer> GetQueryFromSeparateSession(bool? disposeSession)
{
// Using SessionFactory of another session dodge DebugSessionFactory session tracking, which is needed for having session garbage collected.
// Otherwise DebugSessionFactory session tracking should be changed to use WeakReference too.
var s = session.SessionFactory.OpenSession();
try
{
return s.Query<Customer>();
}
finally
{
if (disposeSession == true)
s.Dispose();
else if (disposeSession == false)
s.Close();
}
}
DefaultQueryProvider GetProviderFromSeparateSession(bool? disposeSession)
{
var queryable = GetQueryFromSeparateSession(disposeSession);
return queryable.Provider as DefaultQueryProvider;
}
}
}