|
| 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>(rc => |
| 16 | + { |
| 17 | + rc.Table("Entity"); |
| 18 | + rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); |
| 19 | + }); |
| 20 | + |
| 21 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 22 | + } |
| 23 | + |
| 24 | + protected override void OnSetUp() |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + protected override void OnTearDown() |
| 29 | + { |
| 30 | + using var session = OpenSession(); |
| 31 | + using var transaction = session.BeginTransaction(); |
| 32 | + |
| 33 | + session.CreateQuery("delete from System.Object").ExecuteUpdate(); |
| 34 | + transaction.Commit(); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public void LinqLeaksMemory() |
| 39 | + { |
| 40 | + WeakReference sessionReference = null; |
| 41 | + WeakReference firstReference = null; |
| 42 | + WeakReference secondReference = null; |
| 43 | + |
| 44 | + new System.Action( |
| 45 | + () => |
| 46 | + { |
| 47 | + using var session = ((DebugSessionFactory) Sfi).ActualFactory.OpenSession(); |
| 48 | + |
| 49 | + var first = new Entity { Id = 1 }; |
| 50 | + var second = new Entity { Id = 2 }; |
| 51 | + |
| 52 | + _ = session.Query<Entity>().FirstOrDefault(f => f == first); |
| 53 | + _ = session.Query<Entity>().FirstOrDefault(f => f == second); |
| 54 | + |
| 55 | + sessionReference = new WeakReference(session, true); |
| 56 | + firstReference = new WeakReference(first, true); |
| 57 | + secondReference = new WeakReference(second, true); |
| 58 | + })(); |
| 59 | + |
| 60 | + GC.Collect(); |
| 61 | + GC.WaitForPendingFinalizers(); |
| 62 | + |
| 63 | + Assert.That(sessionReference.Target, Is.Null); |
| 64 | + Assert.That(firstReference.Target, Is.Null); |
| 65 | + Assert.That(secondReference.Target, Is.Null); |
| 66 | + } |
| 67 | + |
| 68 | + public class Entity |
| 69 | + { |
| 70 | + public virtual int Id { get; set; } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments