-
Notifications
You must be signed in to change notification settings - Fork 934
Fix ProxyFactory cache #1454
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
Fix ProxyFactory cache #1454
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
using System.Collections.Concurrent; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using NHibernate.Proxy; | ||
using NHibernate.Proxy.DynamicProxy; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.NH3954 | ||
{ | ||
[TestFixture, Explicit("Demonstrates bug impact on cache, but which tests will fail is a bit unpredictable")] | ||
[TestFixture, Explicit("Demonstrates bug impact on cache, but which tests will fail is a bit unpredictable"), Obsolete] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to mark it as obsolete as |
||
public class ProxyCacheFixture | ||
{ | ||
private ProxyCache _cache; | ||
private ConcurrentDictionary<ProxyCacheEntry, System.Type> _internalCache; | ||
private ConcurrentDictionary<ProxyCacheEntry, TypeInfo> _internalCache; | ||
hazzik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private int _hashCode1; | ||
private int _hashCode2; | ||
|
||
private static readonly FieldInfo InternalCacheField = | ||
typeof(ProxyCache).GetField("cache", BindingFlags.Static | BindingFlags.NonPublic); | ||
typeof(ProxyFactory).GetField("_cache", BindingFlags.Static | BindingFlags.NonPublic); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The field has moved to |
||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_cache = new ProxyCache(); | ||
|
||
_internalCache = (ConcurrentDictionary<ProxyCacheEntry, System.Type>)InternalCacheField.GetValue(null); | ||
_internalCache = (ConcurrentDictionary<ProxyCacheEntry, TypeInfo>)InternalCacheField.GetValue(null); | ||
|
||
_cache.StoreProxyType(typeof(Entity1FakeProxy).GetTypeInfo(), typeof(Entity1)); | ||
_cache.StoreProxyType(typeof(Entity2FakeProxy).GetTypeInfo(), typeof(Entity2), typeof(INHibernateProxy)); | ||
_cache.StoreProxyType(typeof(Entity3FakeProxy).GetTypeInfo(), typeof(Entity3)); | ||
_cache.StoreProxyType(typeof(Entity4FakeProxy).GetTypeInfo(), typeof(Entity4), typeof(IProxy)); | ||
hazzik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_cache.StoreProxyType(typeof(Entity5FakeProxy).GetTypeInfo(), typeof(Entity5), typeof(INHibernateProxy), typeof(IProxy)); | ||
_cache.StoreProxyType(typeof(Entity4FakeProxy).GetTypeInfo(), typeof(Entity4), typeof(ISerializable)); | ||
_cache.StoreProxyType(typeof(Entity5FakeProxy).GetTypeInfo(), typeof(Entity5), typeof(INHibernateProxy), typeof(ISerializable)); | ||
|
||
// Artificially inject other entries with same hashcodes | ||
_hashCode1 = new ProxyCacheEntry(typeof(Entity1), null).GetHashCode(); | ||
|
@@ -37,14 +39,14 @@ public void SetUp() | |
|
||
_hashCode2 = new ProxyCacheEntry(typeof(Entity2), new[] { typeof(INHibernateProxy) }).GetHashCode(); | ||
Inject(new ProxyCacheEntry(typeof(Entity2), null), _hashCode2, typeof(Entity2FakeProxy2)); | ||
Inject(new ProxyCacheEntry(typeof(Entity4), new[] { typeof(IProxy) }), _hashCode2, typeof(Entity4FakeProxy2)); | ||
Inject(new ProxyCacheEntry(typeof(Entity5), new[] { typeof(INHibernateProxy), typeof(IProxy) }), _hashCode2, typeof(Entity5FakeProxy2)); | ||
Inject(new ProxyCacheEntry(typeof(Entity4), new[] { typeof(ISerializable) }), _hashCode2, typeof(Entity4FakeProxy2)); | ||
Inject(new ProxyCacheEntry(typeof(Entity5), new[] { typeof(INHibernateProxy), typeof(ISerializable) }), _hashCode2, typeof(Entity5FakeProxy2)); | ||
} | ||
|
||
private void Inject(ProxyCacheEntry entryToTweak, int hashcode, System.Type result) | ||
{ | ||
TweakEntry(entryToTweak, hashcode); | ||
_internalCache[entryToTweak] = result; | ||
_internalCache[entryToTweak] = result.GetTypeInfo(); | ||
} | ||
|
||
private static readonly FieldInfo HashCodeField = | ||
|
@@ -112,14 +114,14 @@ public void ProxyCacheEntity3FakeProxy2() | |
[Test] | ||
public void ProxyCacheEntity4FakeProxy() | ||
{ | ||
var result = _cache.GetProxyType(typeof(Entity4), typeof(IProxy)); | ||
var result = _cache.GetProxyType(typeof(Entity4), typeof(ISerializable)); | ||
Assert.AreEqual(typeof(Entity4FakeProxy), result); | ||
} | ||
|
||
[Test] | ||
public void ProxyCacheEntity4FakeProxy2() | ||
{ | ||
var entry = new ProxyCacheEntry(typeof(Entity4), new[] { typeof(IProxy) }); | ||
var entry = new ProxyCacheEntry(typeof(Entity4), new[] { typeof(ISerializable) }); | ||
TweakEntry(entry, _hashCode2); | ||
var result = _internalCache[entry]; | ||
Assert.AreEqual(typeof(Entity4FakeProxy2), result); | ||
|
@@ -128,15 +130,15 @@ public void ProxyCacheEntity4FakeProxy2() | |
[Test] | ||
public void ProxyCacheEntity5FakeProxy() | ||
{ | ||
var result = _cache.GetProxyType(typeof(Entity5), typeof(IProxy), typeof(INHibernateProxy)); | ||
var result = _cache.GetProxyType(typeof(Entity5), typeof(ISerializable), typeof(INHibernateProxy)); | ||
Assert.AreEqual(typeof(Entity5FakeProxy), result); | ||
} | ||
|
||
[Test] | ||
public void ProxyCacheEntity5FakeProxy2() | ||
{ | ||
// Interfaces order inverted on purpose: must be supported. | ||
var entry = new ProxyCacheEntry(typeof(Entity5), new[] { typeof(IProxy), typeof(INHibernateProxy) }); | ||
var entry = new ProxyCacheEntry(typeof(Entity5), new[] { typeof(ISerializable), typeof(INHibernateProxy) }); | ||
TweakEntry(entry, _hashCode2); | ||
var result = _internalCache[entry]; | ||
Assert.AreEqual(typeof(Entity5FakeProxy2), result); | ||
|
@@ -149,7 +151,7 @@ public void ProxyCacheNone() | |
// (Otherwise the test may starts failing unexpectedly sometimes, as the original bug ...) | ||
// This one was not added in anyway. | ||
TypeInfo result; | ||
Assert.IsFalse(_cache.TryGetProxyType(typeof(Entity2), new[] { typeof(IProxy) }, out result)); | ||
Assert.IsFalse(_cache.TryGetProxyType(typeof(Entity2), new[] { typeof(ISerializable) }, out result)); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.