Skip to content

Commit ad91297

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

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 OnSetUp()
26+
{
27+
}
28+
29+
protected override void OnTearDown()
30+
{
31+
using var session = OpenSession();
32+
using var transaction = session.BeginTransaction();
33+
34+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
35+
transaction.Commit();
36+
}
37+
38+
[Test]
39+
public void LinqShouldNotLeakEntityParameters()
40+
{
41+
WeakReference sessionReference = null;
42+
WeakReference firstReference = null;
43+
WeakReference secondReference = null;
44+
45+
new System.Action(
46+
() =>
47+
{
48+
using var session = ((DebugSessionFactory) Sfi).ActualFactory.OpenSession();
49+
50+
var first = new Entity { Id = 1 };
51+
var second = new Entity { Id = 2 };
52+
53+
_ = session.Query<Entity>().FirstOrDefault(f => f == first);
54+
_ = session.Query<Entity>().FirstOrDefault(f => f == second);
55+
56+
sessionReference = new WeakReference(session, true);
57+
firstReference = new WeakReference(first, true);
58+
secondReference = new WeakReference(second, true);
59+
})();
60+
61+
GC.Collect();
62+
GC.WaitForPendingFinalizers();
63+
64+
Assert.That(sessionReference.Target, Is.Null);
65+
Assert.That(firstReference.Target, Is.Null);
66+
Assert.That(secondReference.Target, Is.Null);
67+
}
68+
69+
public class Entity
70+
{
71+
public virtual int Id { get; set; }
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)