Skip to content

Commit d8c8f1c

Browse files
NH-3119 - fix test not supporting optimization
It was relying on the stack trace, which is not reliable when calls can be inlined.
1 parent 4b8eab4 commit d8c8f1c

File tree

4 files changed

+72
-47
lines changed

4 files changed

+72
-47
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH3119/FixtureByCode.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@
1111
using System.IO;
1212
using System.Linq;
1313
using System.Runtime.Serialization.Formatters.Binary;
14+
using NHibernate.Bytecode;
15+
using NHibernate.Bytecode.Lightweight;
1416
using NHibernate.Cfg;
1517
using NHibernate.Cfg.MappingSchema;
16-
using NHibernate.Linq;
1718
using NHibernate.Mapping.ByCode;
19+
using NHibernate.Properties;
1820
using NUnit.Framework;
21+
using NHibernate.Linq;
1922

2023
namespace NHibernate.Test.NHSpecificTest.NH3119
2124
{
2225
using System.Threading.Tasks;
23-
/// <summary>
24-
/// Fixture using 'by code' mappings
25-
/// </summary>
26-
/// <remarks>
27-
/// This fixture is identical to <see cref="Fixture" /> except the <see cref="Entity" /> mapping is performed
28-
/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach
29-
/// if you prefer.
30-
/// </remarks>
3126
[TestFixture]
3227
public class ByCodeFixtureAsync : TestCaseMappingByCode
3328
{
@@ -47,9 +42,13 @@ protected override HbmMapping GetMappings()
4742
return mapper.CompileMappingForAllExplicitlyAddedEntities();
4843
}
4944

45+
private static IBytecodeProvider _backupByteCodeProvider;
46+
5047
protected override void OnSetUp()
5148
{
52-
if (!Cfg.Environment.UseReflectionOptimizer)
49+
_backupByteCodeProvider = Environment.BytecodeProvider;
50+
51+
if (!Environment.UseReflectionOptimizer)
5352
{
5453
Assert.Ignore("Test only works with reflection optimization enabled");
5554
}
@@ -63,10 +62,17 @@ protected override void OnSetUp()
6362
session.Flush();
6463
transaction.Commit();
6564
}
65+
66+
// Change refelection optimizer and recreate the configuration and factory
67+
Environment.BytecodeProvider = new TestBytecodeProviderImpl();
68+
Configure();
69+
RebuildSessionFactory();
6670
}
6771

6872
protected override void OnTearDown()
6973
{
74+
Environment.BytecodeProvider = _backupByteCodeProvider;
75+
7076
using (ISession session = OpenSession())
7177
using (ITransaction transaction = session.BeginTransaction())
7278
{
@@ -83,11 +89,9 @@ public async Task PocoComponentTuplizer_Instantiate_UsesReflectonOptimizerAsync(
8389
using (ISession freshSession = OpenSession())
8490
using (freshSession.BeginTransaction())
8591
{
86-
Entity entity = await (freshSession.Query<Entity>().SingleAsync());
87-
88-
string stackTrace = entity.Component.LastCtorStackTrace;
89-
90-
StringAssert.Contains("NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateInstance", stackTrace);
92+
ComponentTestReflectionOptimizer.IsCalledForComponent = false;
93+
await (freshSession.Query<Entity>().SingleAsync());
94+
Assert.That(ComponentTestReflectionOptimizer.IsCalledForComponent, Is.True);
9195
}
9296
}
9397

@@ -106,11 +110,9 @@ public async Task PocoComponentTuplizerOfDeserializedConfiguration_Instantiate_U
106110
using (ISession deserializedSession = factoryFromDeserializedConfig.OpenSession())
107111
using (deserializedSession.BeginTransaction())
108112
{
109-
Entity entity = await (deserializedSession.Query<Entity>().SingleAsync());
110-
111-
string stackTrace = entity.Component.LastCtorStackTrace;
112-
113-
StringAssert.Contains("NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateInstance", stackTrace);
113+
ComponentTestReflectionOptimizer.IsCalledForComponent = false;
114+
await (deserializedSession.Query<Entity>().SingleAsync());
115+
Assert.That(ComponentTestReflectionOptimizer.IsCalledForComponent, Is.True);
114116
}
115117
}
116118
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Diagnostics;
32

43
namespace NHibernate.Test.NHSpecificTest.NH3119
54
{
@@ -12,12 +11,6 @@ public class Entity
1211

1312
public class Component
1413
{
15-
public Component()
16-
{
17-
LastCtorStackTrace = new StackTrace().ToString();
18-
}
19-
2014
public string Value { get; set; }
21-
public string LastCtorStackTrace { get; private set; }
2215
}
23-
}
16+
}

src/NHibernate.Test/NHSpecificTest/NH3119/FixtureByCode.cs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
using System.IO;
22
using System.Linq;
33
using System.Runtime.Serialization.Formatters.Binary;
4+
using NHibernate.Bytecode;
5+
using NHibernate.Bytecode.Lightweight;
46
using NHibernate.Cfg;
57
using NHibernate.Cfg.MappingSchema;
6-
using NHibernate.Linq;
78
using NHibernate.Mapping.ByCode;
9+
using NHibernate.Properties;
810
using NUnit.Framework;
911

1012
namespace NHibernate.Test.NHSpecificTest.NH3119
1113
{
12-
/// <summary>
13-
/// Fixture using 'by code' mappings
14-
/// </summary>
15-
/// <remarks>
16-
/// This fixture is identical to <see cref="Fixture" /> except the <see cref="Entity" /> mapping is performed
17-
/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach
18-
/// if you prefer.
19-
/// </remarks>
2014
[TestFixture]
2115
public class ByCodeFixture : TestCaseMappingByCode
2216
{
@@ -36,9 +30,13 @@ protected override HbmMapping GetMappings()
3630
return mapper.CompileMappingForAllExplicitlyAddedEntities();
3731
}
3832

33+
private static IBytecodeProvider _backupByteCodeProvider;
34+
3935
protected override void OnSetUp()
4036
{
41-
if (!Cfg.Environment.UseReflectionOptimizer)
37+
_backupByteCodeProvider = Environment.BytecodeProvider;
38+
39+
if (!Environment.UseReflectionOptimizer)
4240
{
4341
Assert.Ignore("Test only works with reflection optimization enabled");
4442
}
@@ -52,10 +50,17 @@ protected override void OnSetUp()
5250
session.Flush();
5351
transaction.Commit();
5452
}
53+
54+
// Change refelection optimizer and recreate the configuration and factory
55+
Environment.BytecodeProvider = new TestBytecodeProviderImpl();
56+
Configure();
57+
RebuildSessionFactory();
5558
}
5659

5760
protected override void OnTearDown()
5861
{
62+
Environment.BytecodeProvider = _backupByteCodeProvider;
63+
5964
using (ISession session = OpenSession())
6065
using (ITransaction transaction = session.BeginTransaction())
6166
{
@@ -72,11 +77,9 @@ public void PocoComponentTuplizer_Instantiate_UsesReflectonOptimizer()
7277
using (ISession freshSession = OpenSession())
7378
using (freshSession.BeginTransaction())
7479
{
75-
Entity entity = freshSession.Query<Entity>().Single();
76-
77-
string stackTrace = entity.Component.LastCtorStackTrace;
78-
79-
StringAssert.Contains("NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateInstance", stackTrace);
80+
ComponentTestReflectionOptimizer.IsCalledForComponent = false;
81+
freshSession.Query<Entity>().Single();
82+
Assert.That(ComponentTestReflectionOptimizer.IsCalledForComponent, Is.True);
8083
}
8184
}
8285

@@ -95,12 +98,38 @@ public void PocoComponentTuplizerOfDeserializedConfiguration_Instantiate_UsesRef
9598
using (ISession deserializedSession = factoryFromDeserializedConfig.OpenSession())
9699
using (deserializedSession.BeginTransaction())
97100
{
98-
Entity entity = deserializedSession.Query<Entity>().Single();
101+
ComponentTestReflectionOptimizer.IsCalledForComponent = false;
102+
deserializedSession.Query<Entity>().Single();
103+
Assert.That(ComponentTestReflectionOptimizer.IsCalledForComponent, Is.True);
104+
}
105+
}
106+
}
99107

100-
string stackTrace = entity.Component.LastCtorStackTrace;
108+
public class TestBytecodeProviderImpl : AbstractBytecodeProvider
109+
{
110+
public override IReflectionOptimizer GetReflectionOptimizer(System.Type mappedClass, IGetter[] getters, ISetter[] setters)
111+
{
112+
return new ComponentTestReflectionOptimizer(mappedClass, getters, setters);
113+
}
114+
}
101115

102-
StringAssert.Contains("NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateInstance", stackTrace);
103-
}
116+
public class ComponentTestReflectionOptimizer : ReflectionOptimizer
117+
{
118+
private readonly bool _logCall;
119+
120+
public static bool IsCalledForComponent { get; set; }
121+
122+
public ComponentTestReflectionOptimizer(System.Type mappedType, IGetter[] getters, ISetter[] setters) :
123+
base(mappedType, getters, setters)
124+
{
125+
_logCall = mappedType == typeof(Component);
126+
}
127+
128+
public override object CreateInstance()
129+
{
130+
if (_logCall)
131+
IsCalledForComponent = true;
132+
return base.CreateInstance();
104133
}
105134
}
106135
}

src/NHibernate.Test/TestCase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void TestFixtureSetUp()
8585

8686
protected void RebuildSessionFactory()
8787
{
88+
Sfi?.Close();
8889
_sessionFactory = BuildSessionFactory();
8990
}
9091

0 commit comments

Comments
 (0)