Skip to content

Commit 7b04696

Browse files
committed
Stronger typing of invalidation methods.
Fixed assertions in unit test.
1 parent 5878986 commit 7b04696

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/NHibernate.Test/SecondLevelCacheTest/InvalidationTests.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
64
using NHibernate.Cache;
75
using NHibernate.Cfg;
86
using NHibernate.Impl;
97
using NHibernate.Test.SecondLevelCacheTests;
108
using NSubstitute;
119
using NUnit.Framework;
12-
using Environment = System.Environment;
1310

1411
namespace NHibernate.Test.SecondLevelCacheTest
1512
{
@@ -23,8 +20,8 @@ public class InvalidationTests : TestCase
2320
protected override void Configure(Configuration configuration)
2421
{
2522
base.Configure(configuration);
26-
configuration.Properties[Cfg.Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName;
27-
configuration.Properties[Cfg.Environment.UseQueryCache] = "true";
23+
configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName;
24+
configuration.Properties[Environment.UseQueryCache] = "true";
2825
}
2926

3027
[Test]
@@ -35,7 +32,18 @@ public void InvalidatesEntities()
3532
x => x.UpdateTimestampsCache,
3633
cache);
3734

38-
var items = new List<Item>();
35+
//"Received" assertions can not be used since the collection is reused and cleared between calls.
36+
//The received args are cloned and stored
37+
var preInvalidations = new List<IReadOnlyCollection<string>>();
38+
var invalidations = new List<IReadOnlyCollection<string>>();
39+
40+
cache
41+
.When(x=>x.PreInvalidate(Arg.Any<IReadOnlyCollection<string>>()))
42+
.Do(x=>preInvalidations.Add(((IReadOnlyCollection<string>) x[0]).ToList()));
43+
cache
44+
.When(x => x.Invalidate(Arg.Any<IReadOnlyCollection<string>>()))
45+
.Do(x => invalidations.Add(((IReadOnlyCollection<string>) x[0]).ToList()));
46+
3947
using (ISession session = OpenSession())
4048
{
4149
using (ITransaction tx = session.BeginTransaction())
@@ -73,8 +81,17 @@ public void InvalidatesEntities()
7381
}
7482

7583
//Should receive one preinvalidation and one invalidation per commit
76-
cache.Received(3).PreInvalidate(Arg.Is<IReadOnlyCollection<object>>(x => x.Count == 1 && (string) x.First() == "Item"));
77-
cache.Received(3).Invalidate(Arg.Is<IReadOnlyCollection<object>>(x => x.Count == 1 && (string) x.First() == "Item"));
84+
Assert.That(preInvalidations.Count,Is.EqualTo(3));
85+
Assert.That(preInvalidations.All(x => x.Count == 1 && x.First() == "Item"), Is.True);
86+
87+
Assert.That(invalidations.Count, Is.EqualTo(3));
88+
Assert.That(invalidations.All(x => x.Count == 1 && x.First() == "Item"), Is.True);
89+
90+
}
91+
92+
private bool IsRight(HashSet<string> x)
93+
{
94+
return x.Count == 1 && x.First() == "Item";
7895
}
7996

8097
public void CleanUp()

src/NHibernate/Cache/UpdateTimestampsCache.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Runtime.CompilerServices;
45

56
using NHibernate.Cfg;
@@ -33,13 +34,15 @@ public UpdateTimestampsCache(Settings settings, IDictionary<string, string> prop
3334
updateTimestamps = settings.CacheProvider.BuildCache(regionName, props);
3435
}
3536

37+
//TODO: Make obsolete
3638
public void PreInvalidate(object[] spaces)
3739
{
38-
PreInvalidate((IReadOnlyList<object>) spaces);
40+
//Only for backwards compatibility.
41+
PreInvalidate(spaces.OfType<string>().ToList());
3942
}
4043

4144
[MethodImpl(MethodImplOptions.Synchronized)]
42-
public virtual void PreInvalidate(IReadOnlyCollection<object> spaces)
45+
public virtual void PreInvalidate(IReadOnlyCollection<string> spaces)
4346
{
4447
//TODO: to handle concurrent writes correctly, this should return a Lock to the client
4548
long ts = updateTimestamps.NextTimestamp() + updateTimestamps.Timeout;
@@ -51,14 +54,15 @@ public virtual void PreInvalidate(IReadOnlyCollection<object> spaces)
5154
//TODO: return new Lock(ts);
5255
}
5356

54-
/// <summary></summary>
57+
//TODO: Make obsolete
5558
public void Invalidate(object[] spaces)
5659
{
57-
Invalidate((IReadOnlyList<object>) spaces);
60+
//Only for backwards compatibility.
61+
Invalidate(spaces.OfType<string>().ToList());
5862
}
5963

6064
[MethodImpl(MethodImplOptions.Synchronized)]
61-
public virtual void Invalidate(IReadOnlyCollection<object> spaces)
65+
public virtual void Invalidate(IReadOnlyCollection<string> spaces)
6266
{
6367
//TODO: to handle concurrent writes correctly, the client should pass in a Lock
6468
long ts = updateTimestamps.NextTimestamp();

0 commit comments

Comments
 (0)