Skip to content

NH-3957 - fix 2nd level query cache bug and test cases #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.Collections.Generic;
using System.Reflection;
using NHibernate.Transform;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3957
{
[TestFixture]
public class ResultTransformerEqualityFixture
{
/// <summary>
/// Allows to simulate a hashcode collision. Issue would be unpractical to test otherwise.
/// Hashcode collision must be supported for avoiding unexpected and hard to reproduce failures.
/// </summary>
private void TweakHashcode(System.Type transformerToTweak, object hasher)
{
var hasherTargetField = transformerToTweak.GetField("Hasher", BindingFlags.Static | BindingFlags.NonPublic);
if (!_hasherBackup.ContainsKey(transformerToTweak))
_hasherBackup.Add(transformerToTweak, hasherTargetField.GetValue(null));

// Though hasher is a readonly field, this works at the time of this writing. If it starts breaking and cannot be fixed,
// ignore those tests or throw them away.
hasherTargetField.SetValue(null, hasher);
}

private Dictionary<System.Type, object> _hasherBackup = new Dictionary<System.Type, object>();

[SetUp]
public void Setup()
{
var hasherForAll = typeof(AliasToEntityMapResultTransformer)
.GetField("Hasher", BindingFlags.Static | BindingFlags.NonPublic)
.GetValue(null);
TweakHashcode(typeof(DistinctRootEntityResultTransformer), hasherForAll);
TweakHashcode(typeof(PassThroughResultTransformer), hasherForAll);
TweakHashcode(typeof(RootEntityResultTransformer), hasherForAll);
TweakHashcode(typeof(ToListResultTransformer), hasherForAll);
}

[TearDown]
public void TearDown()
{
// Restore those types hashcode. (Avoid impacting perf of other tests, avoid second level query cache
// issues if it was holding cached entries (but would mean some tests have not cleaned up properly).)
foreach(var backup in _hasherBackup)
{
TweakHashcode(backup.Key, backup.Value);
}
}

// Non reg test case
[Test]
public void AliasToEntityMapEquality()
{
var transf1 = new AliasToEntityMapResultTransformer();
var transf2 = new AliasToEntityMapResultTransformer();

Assert.IsTrue(transf1.Equals(transf2));
Assert.IsTrue(transf2.Equals(transf1));
}

[Test]
public void AliasToEntityMapAndDistinctRootEntityInequality()
{
var transf1 = new AliasToEntityMapResultTransformer();
var transf2 = new DistinctRootEntityResultTransformer();

Assert.IsFalse(transf1.Equals(transf2));
Assert.IsFalse(transf2.Equals(transf1));
}

// Non reg test case
[Test]
public void DistinctRootEntityEquality()
{
var transf1 = new DistinctRootEntityResultTransformer();
var transf2 = new DistinctRootEntityResultTransformer();

Assert.IsTrue(transf1.Equals(transf2));
Assert.IsTrue(transf2.Equals(transf1));
}

// Non reg test case
[Test]
public void PassThroughEquality()
{
var transf1 = new PassThroughResultTransformer();
var transf2 = new PassThroughResultTransformer();

Assert.IsTrue(transf1.Equals(transf2));
Assert.IsTrue(transf2.Equals(transf1));
}

[Test]
public void PassThroughAndRootEntityInequality()
{
var transf1 = new PassThroughResultTransformer();
var transf2 = new RootEntityResultTransformer();

Assert.IsFalse(transf1.Equals(transf2));
Assert.IsFalse(transf2.Equals(transf1));
}

// Non reg test case
[Test]
public void RootEntityEquality()
{
var transf1 = new RootEntityResultTransformer();
var transf2 = new RootEntityResultTransformer();

Assert.IsTrue(transf1.Equals(transf2));
Assert.IsTrue(transf2.Equals(transf1));
}

[Test]
public void RootEntityAndToListInequality()
{
var transf1 = new RootEntityResultTransformer();
var transf2 = new ToListResultTransformer();

Assert.IsFalse(transf1.Equals(transf2));
Assert.IsFalse(transf2.Equals(transf1));
}

// Non reg test case
[Test]
public void ToListEquality()
{
var transf1 = new ToListResultTransformer();
var transf2 = new ToListResultTransformer();

Assert.IsTrue(transf1.Equals(transf2));
Assert.IsTrue(transf2.Equals(transf1));
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@
<Compile Include="NHSpecificTest\NH3874\Two.cs" />
<Compile Include="NHSpecificTest\NH3909\Entity.cs" />
<Compile Include="NHSpecificTest\NH3909\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH3957\ResultTransformerEqualityFixture.cs" />
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3234\Fixture.cs" />
Expand Down
8 changes: 5 additions & 3 deletions src/NHibernate/Transform/AliasToEntityMapResultTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ public override IList TransformList(IList collection)
}


public override bool IsTransformedValueATupleElement(String[] aliases, int tupleLength)
public override bool IsTransformedValueATupleElement(string[] aliases, int tupleLength)
{
return false;
}

public override bool Equals(object obj)
{
if (obj == null)
if (obj == null || obj.GetHashCode() != Hasher.GetHashCode())
{
return false;
}
return obj.GetHashCode() == Hasher.GetHashCode();
// NH-3957: do not rely on hashcode alone.
// Must be the exact same type
return obj.GetType() == typeof(AliasToEntityMapResultTransformer);
}

public override int GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ public bool IsTransformedValueATupleElement(String[] aliases, int tupleLength)

public override bool Equals(object obj)
{
if (obj == null)
if (obj == null || obj.GetHashCode() != Hasher.GetHashCode())
{
return false;
}
return obj.GetHashCode() == Hasher.GetHashCode();
// NH-3957: do not rely on hashcode alone.
// Must be the exact same type
return obj.GetType() == typeof(DistinctRootEntityResultTransformer);
}

public override int GetHashCode()
Expand Down
6 changes: 4 additions & 2 deletions src/NHibernate/Transform/PassThroughResultTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ internal object[] UntransformToTuple(object transformed, bool isSingleResult)

public override bool Equals(object obj)
{
if (obj == null)
if (obj == null || obj.GetHashCode() != Hasher.GetHashCode())
{
return false;
}
return obj.GetHashCode() == Hasher.GetHashCode();
// NH-3957: do not rely on hashcode alone.
// Must be the exact same type
return obj.GetType() == typeof(PassThroughResultTransformer);
}

public override int GetHashCode()
Expand Down
6 changes: 4 additions & 2 deletions src/NHibernate/Transform/RootEntityResultTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ public bool[] IncludeInTransform(String[] aliases, int tupleLength)

public override bool Equals(object obj)
{
if (obj == null)
if (obj == null || obj.GetHashCode() != Hasher.GetHashCode())
{
return false;
}
return obj.GetHashCode() == Hasher.GetHashCode();
// NH-3957: do not rely on hashcode alone.
// Must be the exact same type
return obj.GetType() == typeof(RootEntityResultTransformer);
}

public override int GetHashCode()
Expand Down
6 changes: 4 additions & 2 deletions src/NHibernate/Transform/ToListResultTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public IList TransformList(IList list)

public override bool Equals(object obj)
{
if (obj == null)
if (obj == null || obj.GetHashCode() != Hasher.GetHashCode())
{
return false;
}
return obj.GetHashCode() == Hasher.GetHashCode();
// NH-3957: do not rely on hashcode alone.
// Must be the exact same type
return obj.GetType() == typeof(ToListResultTransformer);
}

public override int GetHashCode()
Expand Down