Skip to content

Commit 963a037

Browse files
committed
Add test for #3030
1 parent d903bb2 commit 963a037

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Linq;
3+
using NHibernate.Cfg.MappingSchema;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.GH3030
8+
{
9+
[TestFixture]
10+
public class ByCodeFixture : TestCaseMappingByCode
11+
{
12+
protected override HbmMapping GetMappings()
13+
{
14+
var mapper = new ModelMapper();
15+
mapper.Class<Entity>(
16+
rc =>
17+
{
18+
rc.Table("Entity");
19+
rc.Id(x => x.Id, m => m.Generator(Generators.Assigned));
20+
});
21+
22+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
23+
}
24+
25+
protected override void OnTearDown()
26+
{
27+
using (var session = OpenSession())
28+
using (var transaction = session.BeginTransaction())
29+
{
30+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
31+
transaction.Commit();
32+
}
33+
}
34+
35+
[Test]
36+
public void LinqShouldNotLeakEntityParameters()
37+
{
38+
WeakReference sessionReference = null;
39+
WeakReference firstReference = null;
40+
WeakReference secondReference = null;
41+
42+
new System.Action(
43+
() =>
44+
{
45+
using (var session = ((DebugSessionFactory) Sfi).ActualFactory.OpenSession())
46+
{
47+
var first = new Entity { Id = 1 };
48+
var second = new Entity { Id = 2 };
49+
50+
_ = session.Query<Entity>().FirstOrDefault(f => f == first);
51+
_ = session.Query<Entity>().FirstOrDefault(f => f == second);
52+
53+
sessionReference = new WeakReference(session, true);
54+
firstReference = new WeakReference(first, true);
55+
secondReference = new WeakReference(second, true);
56+
}
57+
})();
58+
59+
GC.Collect();
60+
GC.WaitForPendingFinalizers();
61+
62+
Assert.That(sessionReference.Target, Is.Null);
63+
Assert.That(firstReference.Target, Is.Null);
64+
Assert.That(secondReference.Target, Is.Null);
65+
}
66+
67+
public class Entity
68+
{
69+
public virtual int Id { get; set; }
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)