Skip to content

Commit dfb39d7

Browse files
committed
NH-3807 - Fix tests related to WeakReference and .NET Core changes.
.NET Core extended the lifetime of some temporary variables and that affected how the tests were constructed to test WeakReference as used in WeakHashtable and WeakRefWrapper. Making sure the temporary variables are short lived in a NoInlining method is the solution.
1 parent 305c4dc commit dfb39d7

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/NHibernate.Test/UtilityTest/WeakHashtableFixture.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections;
3-
using System.IO;
4-
using System.Runtime.Serialization.Formatters.Binary;
3+
using System.Runtime.CompilerServices;
54

65
using NHibernate.Util;
76
using NUnit.Framework;
@@ -11,11 +10,30 @@ namespace NHibernate.Test.UtilityTest
1110
[TestFixture]
1211
public class WeakHashtableFixture
1312
{
14-
protected WeakHashtable Create()
13+
private static WeakHashtable Create()
1514
{
1615
return new WeakHashtable();
1716
}
1817

18+
// NoInlining to keep temporary variables' lifetime from being extended.
19+
[MethodImpl(MethodImplOptions.NoInlining)]
20+
private static WeakHashtable CreateWithTwoObjects()
21+
{
22+
var table = Create();
23+
24+
table[new object()] = new object();
25+
table[new object()] = new object();
26+
27+
return table;
28+
}
29+
30+
[MethodImpl(MethodImplOptions.NoInlining)]
31+
private static WeakRefWrapper CreateWeakRefWrapper()
32+
{
33+
object obj = new object();
34+
return new WeakRefWrapper(obj);
35+
}
36+
1937
[Test]
2038
public void Basic()
2139
{
@@ -33,10 +51,8 @@ public void Basic()
3351
[Test]
3452
public void WeakReferenceGetsFreedButHashCodeRemainsConstant()
3553
{
36-
object obj = new object();
37-
WeakRefWrapper wr = new WeakRefWrapper(obj);
54+
WeakRefWrapper wr = CreateWeakRefWrapper();
3855
int hashCode = wr.GetHashCode();
39-
obj = null;
4056

4157
GC.Collect();
4258

@@ -48,10 +64,7 @@ public void WeakReferenceGetsFreedButHashCodeRemainsConstant()
4864
[Test]
4965
public void Scavenging()
5066
{
51-
WeakHashtable table = Create();
52-
53-
table[new object()] = new object();
54-
table[new object()] = new object();
67+
WeakHashtable table = CreateWithTwoObjects();
5568

5669
GC.Collect();
5770
table.Scavenge();
@@ -62,10 +75,7 @@ public void Scavenging()
6275
[Test]
6376
public void IterationAfterGC()
6477
{
65-
WeakHashtable table = Create();
66-
67-
table[new object()] = new object();
68-
table[new object()] = new object();
78+
WeakHashtable table = CreateWithTwoObjects();
6979

7080
GC.Collect();
7181

0 commit comments

Comments
 (0)