Skip to content

Fix session factory deserialization with Prevalence Cache #2196

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
Aug 21, 2019
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
84 changes: 84 additions & 0 deletions src/NHibernate.Test/Async/CacheTest/Caches/SerializingCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Collections;
using NHibernate.Cache;

namespace NHibernate.Test.CacheTest.Caches
{
using System.Threading.Tasks;
using System.Threading;
public partial class SerializingCache : CacheBase
{

public override Task<object> GetAsync(object key, CancellationToken cancellationToken)
{
try
{
return Task.FromResult<object>(_hashtable[key]);
}
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

public override Task PutAsync(object key, object value, CancellationToken cancellationToken)
{
try
{
_hashtable[key] = value;
return Task.CompletedTask;
}
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

public override Task RemoveAsync(object key, CancellationToken cancellationToken)
{
try
{
_hashtable.Remove(key);
return Task.CompletedTask;
}
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

public override Task ClearAsync(CancellationToken cancellationToken)
{
try
{
_hashtable.Clear();
return Task.CompletedTask;
}
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

public override Task<object> LockAsync(object key, CancellationToken cancellationToken)
{
// local cache, no need to actually lock.
return Task.FromResult<object>(null);
}

public override Task UnlockAsync(object key, object lockValue, CancellationToken cancellationToken)
{
return Task.CompletedTask;
// local cache, no need to actually lock.
}
}
}
93 changes: 93 additions & 0 deletions src/NHibernate.Test/Async/CacheTest/SerializingCacheFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Linq;
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.CacheTest
{
using System.Threading.Tasks;
[TestFixture]
public class SerializingCacheFixtureAsync : TestCase
{
protected override string[] Mappings => new[]
{
"CacheTest.ReadOnly.hbm.xml"
};

protected override string MappingsAssembly => "NHibernate.Test";

protected override string CacheConcurrencyStrategy => null;

protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.UseSecondLevelCache, "true");
configuration.SetProperty(Environment.UseQueryCache, "true");
configuration.SetProperty(Environment.CacheProvider, typeof(SerializingCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Environment.SessionFactoryName, "SerializingCacheFactory");
}

protected override void OnSetUp()
{
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var totalItems = 6;
for (var i = 1; i <= totalItems; i++)
{
var parent = new ReadOnly
{
Name = $"Name{i}"
};
for (var j = 1; j <= totalItems; j++)
{
var child = new ReadOnlyItem
{
Parent = parent
};
parent.Items.Add(child);
}
s.Save(parent);
}
tx.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from ReadOnlyItem").ExecuteUpdate();
s.CreateQuery("delete from ReadOnly").ExecuteUpdate();
tx.Commit();
}
}

[Test]
public async Task CachedQueryTestAsync()
{
// Put things in cache
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var items = await (s.Query<ReadOnly>().WithOptions(o => o.SetCacheable(true)).ToListAsync());
Assert.That(items, Has.Count.GreaterThan(0));
await (tx.CommitAsync());
}

RebuildSessionFactory();
}
}
}
60 changes: 60 additions & 0 deletions src/NHibernate.Test/CacheTest/Caches/SerializingCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections;
using NHibernate.Cache;

namespace NHibernate.Test.CacheTest.Caches
{
public partial class SerializingCache : CacheBase
{
private readonly IDictionary _hashtable;

public SerializingCache(string regionName, IDictionary data)
{
RegionName = regionName;
_hashtable = data;
}

public override object Get(object key)
{
return _hashtable[key];
}

public override void Put(object key, object value)
{
_hashtable[key] = value;
}

public override void Remove(object key)
{
_hashtable.Remove(key);
}

public override void Clear()
{
_hashtable.Clear();
}

public override void Destroy()
{
}

public override object Lock(object key)
{
// local cache, no need to actually lock.
return null;
}

public override void Unlock(object key, object lockValue)
{
// local cache, no need to actually lock.
}

public override long NextTimestamp()
{
return Timestamper.Next();
}

public override int Timeout => Timestamper.OneMs * 60000;

public override string RegionName { get; }
}
}
67 changes: 67 additions & 0 deletions src/NHibernate.Test/CacheTest/Caches/SerializingCacheProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Cache;

namespace NHibernate.Test.CacheTest.Caches
{
public class SerializingCacheProvider : ICacheProvider
{
#region ICacheProvider Members

// Since 5.2
[Obsolete]
ICache ICacheProvider.BuildCache(string regionName, IDictionary<string, string> properties)
{
return BuildCache(regionName, properties);
}

public CacheBase BuildCache(string regionName, IDictionary<string, string> properties)
{
if (!CacheData.TryGetValue(regionName ?? string.Empty, out var data))
{
data = new Hashtable();
CacheData.Add(regionName ?? string.Empty, data);
}

return new SerializingCache(regionName, data);
}

public long NextTimestamp()
{
return Timestamper.Next();
}

public void Start(IDictionary<string, string> properties)
{
var serializer = new BinaryFormatter();
foreach (var cache in SerializedCacheData)
{
using (var stream = new MemoryStream(cache.Value))
{
CacheData.Add(cache.Key, (IDictionary) serializer.Deserialize(stream));
}
}
}

public void Stop()
{
var serializer = new BinaryFormatter();
foreach (var cache in CacheData)
{
using (var stream = new MemoryStream())
{
serializer.Serialize(stream, cache.Value);
SerializedCacheData[cache.Key] = stream.ToArray();
}
}
}

#endregion

private readonly Dictionary<string, IDictionary> CacheData = new Dictionary<string, IDictionary>();
private static readonly Dictionary<string, byte[]> SerializedCacheData = new Dictionary<string, byte[]>();
}
}
82 changes: 82 additions & 0 deletions src/NHibernate.Test/CacheTest/SerializingCacheFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.CacheTest
{
[TestFixture]
public class SerializingCacheFixture : TestCase
{
protected override string[] Mappings => new[]
{
"CacheTest.ReadOnly.hbm.xml"
};

protected override string MappingsAssembly => "NHibernate.Test";

protected override string CacheConcurrencyStrategy => null;

protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.UseSecondLevelCache, "true");
configuration.SetProperty(Environment.UseQueryCache, "true");
configuration.SetProperty(Environment.CacheProvider, typeof(SerializingCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Environment.SessionFactoryName, "SerializingCacheFactory");
}

protected override void OnSetUp()
{
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var totalItems = 6;
for (var i = 1; i <= totalItems; i++)
{
var parent = new ReadOnly
{
Name = $"Name{i}"
};
for (var j = 1; j <= totalItems; j++)
{
var child = new ReadOnlyItem
{
Parent = parent
};
parent.Items.Add(child);
}
s.Save(parent);
}
tx.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from ReadOnlyItem").ExecuteUpdate();
s.CreateQuery("delete from ReadOnly").ExecuteUpdate();
tx.Commit();
}
}

[Test]
public void CachedQueryTest()
{
// Put things in cache
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var items = s.Query<ReadOnly>().WithOptions(o => o.SetCacheable(true)).ToList();
Assert.That(items, Has.Count.GreaterThan(0));
tx.Commit();
}

RebuildSessionFactory();
}
}
}
Loading