Skip to content

Commit 832b216

Browse files
Update to NH5.2
And some cleanup
1 parent a6b7851 commit 832b216

40 files changed

+150
-292
lines changed

NHibernate.Caches.Common.Tests/Async/CacheFixture.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public async Task TestLockUnlockAsync()
8282

8383
for (var i = 0; i < 2; i++)
8484
{
85-
await (cache.LockAsync(key, CancellationToken.None));
86-
await (cache.UnlockAsync(key, CancellationToken.None));
85+
var lockValue = await (cache.LockAsync(key, CancellationToken.None));
86+
await (cache.UnlockAsync(key, lockValue, CancellationToken.None));
8787
}
8888
}
8989

@@ -103,7 +103,7 @@ public async Task TestConcurrentLockUnlockAsync()
103103

104104
// Simulate NHibernate ReadWriteCache behavior with multiple concurrent threads
105105
// Thread 1
106-
await (cache.LockAsync(key, CancellationToken.None));
106+
var lockValue = await (cache.LockAsync(key, CancellationToken.None));
107107

108108
// Thread 2
109109
try
@@ -112,7 +112,7 @@ public async Task TestConcurrentLockUnlockAsync()
112112
}
113113
finally
114114
{
115-
await (cache.UnlockAsync(key, CancellationToken.None));
115+
await (cache.UnlockAsync(key, lockValue, CancellationToken.None));
116116
}
117117

118118
// Thread 3
@@ -122,14 +122,14 @@ public async Task TestConcurrentLockUnlockAsync()
122122
}
123123
finally
124124
{
125-
await (cache.UnlockAsync(key, CancellationToken.None));
125+
await (cache.UnlockAsync(key, lockValue, CancellationToken.None));
126126
}
127-
127+
128128
// Thread 1
129-
await (cache.UnlockAsync(key, CancellationToken.None));
129+
await (cache.UnlockAsync(key, lockValue, CancellationToken.None));
130130

131-
Assert.DoesNotThrowAsync(() => cache.LockAsync(key, CancellationToken.None), "The key should be unlocked");
132-
await (cache.UnlockAsync(key, CancellationToken.None));
131+
Assert.DoesNotThrowAsync(async () => lockValue = await (cache.LockAsync(key, CancellationToken.None)), "The key should be unlocked");
132+
await (cache.UnlockAsync(key, lockValue, CancellationToken.None));
133133

134134
await (cache.RemoveAsync(key, CancellationToken.None));
135135
}

NHibernate.Caches.Common.Tests/CacheFixture.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public void TestLockUnlock()
7676

7777
for (var i = 0; i < 2; i++)
7878
{
79-
cache.Lock(key);
80-
cache.Unlock(key);
79+
var lockValue = cache.Lock(key);
80+
cache.Unlock(key, lockValue);
8181
}
8282
}
8383

@@ -97,7 +97,7 @@ public void TestConcurrentLockUnlock()
9797

9898
// Simulate NHibernate ReadWriteCache behavior with multiple concurrent threads
9999
// Thread 1
100-
cache.Lock(key);
100+
var lockValue = cache.Lock(key);
101101

102102
// Thread 2
103103
try
@@ -106,7 +106,7 @@ public void TestConcurrentLockUnlock()
106106
}
107107
finally
108108
{
109-
cache.Unlock(key);
109+
cache.Unlock(key, lockValue);
110110
}
111111

112112
// Thread 3
@@ -116,14 +116,14 @@ public void TestConcurrentLockUnlock()
116116
}
117117
finally
118118
{
119-
cache.Unlock(key);
119+
cache.Unlock(key, lockValue);
120120
}
121-
121+
122122
// Thread 1
123-
cache.Unlock(key);
123+
cache.Unlock(key, lockValue);
124124

125-
Assert.DoesNotThrow(() => cache.Lock(key), "The key should be unlocked");
126-
cache.Unlock(key);
125+
Assert.DoesNotThrow(() => lockValue = cache.Lock(key), "The key should be unlocked");
126+
cache.Unlock(key, lockValue);
127127

128128
cache.Remove(key);
129129
}

StackExRedis/NHibernate.Caches.StackExRedis.Tests/Async/Caches/DistributedRedisCache.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111
using System;
1212
using System.Collections.Generic;
1313
using System.Linq;
14-
using System.Text;
15-
using System.Threading;
16-
using System.Threading.Tasks;
1714
using NHibernate.Cache;
1815

1916
namespace NHibernate.Caches.StackExRedis.Tests.Caches
2017
{
21-
public partial class DistributedRedisCache : ICache
18+
using System.Threading.Tasks;
19+
using System.Threading;
20+
public partial class DistributedRedisCache : CacheBase
2221
{
2322

2423
/// <inheritdoc />
25-
public Task<object> GetAsync(object key, CancellationToken cancellationToken)
24+
public override Task<object> GetAsync(object key, CancellationToken cancellationToken)
2625
{
2726
// Use a random strategy to get the value.
2827
// A real distributed cache should use a proper load balancing.
@@ -31,7 +30,7 @@ public Task<object> GetAsync(object key, CancellationToken cancellationToken)
3130
}
3231

3332
/// <inheritdoc />
34-
public async Task PutAsync(object key, object value, CancellationToken cancellationToken)
33+
public override async Task PutAsync(object key, object value, CancellationToken cancellationToken)
3534
{
3635
foreach (var strategy in _regionStrategies)
3736
{
@@ -40,7 +39,7 @@ public async Task PutAsync(object key, object value, CancellationToken cancellat
4039
}
4140

4241
/// <inheritdoc />
43-
public async Task RemoveAsync(object key, CancellationToken cancellationToken)
42+
public override async Task RemoveAsync(object key, CancellationToken cancellationToken)
4443
{
4544
foreach (var strategy in _regionStrategies)
4645
{
@@ -49,7 +48,7 @@ public async Task RemoveAsync(object key, CancellationToken cancellationToken)
4948
}
5049

5150
/// <inheritdoc />
52-
public async Task ClearAsync(CancellationToken cancellationToken)
51+
public override async Task ClearAsync(CancellationToken cancellationToken)
5352
{
5453
foreach (var strategy in _regionStrategies)
5554
{
@@ -58,7 +57,7 @@ public async Task ClearAsync(CancellationToken cancellationToken)
5857
}
5958

6059
/// <inheritdoc />
61-
public async Task LockAsync(object key, CancellationToken cancellationToken)
60+
public override async Task<object> LockAsync(object key, CancellationToken cancellationToken)
6261
{
6362
// A simple locking that requires all instances to obtain the lock
6463
// A real distributed cache should use something like the Redlock algorithm.
@@ -69,6 +68,8 @@ public async Task LockAsync(object key, CancellationToken cancellationToken)
6968
{
7069
lockValues[i] = await (_regionStrategies[i].LockAsync(key, cancellationToken));
7170
}
71+
72+
return lockValues;
7273
}
7374
catch (CacheException)
7475
{
@@ -85,14 +86,13 @@ public async Task LockAsync(object key, CancellationToken cancellationToken)
8586
}
8687

8788
/// <inheritdoc />
88-
public async Task UnlockAsync(object key, CancellationToken cancellationToken)
89+
public override async Task UnlockAsync(object key, object lockValue, CancellationToken cancellationToken)
8990
{
90-
foreach (var strategy in _regionStrategies)
91+
var lockValues = (string[]) lockValue;
92+
for (var i = 0; i < _regionStrategies.Length; i++)
9193
{
92-
// TODO: use the lockValue when upgrading to NH 5.2
93-
await (strategy.UnlockAsync(key, null, cancellationToken));
94+
await (_regionStrategies[i].UnlockAsync(key, lockValues[i], cancellationToken));
9495
}
9596
}
96-
9797
}
9898
}

StackExRedis/NHibernate.Caches.StackExRedis.Tests/Async/RedisCacheDefaultStrategyFixture.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@
99

1010

1111
using System;
12-
using System.Collections.Generic;
13-
using System.Linq;
14-
using System.Text;
1512
using System.Threading;
16-
using System.Threading.Tasks;
1713
using NHibernate.Cache;
18-
using NHibernate.Caches.Common.Tests;
1914
using NUnit.Framework;
2015

2116
namespace NHibernate.Caches.StackExRedis.Tests
2217
{
18+
using System.Threading.Tasks;
2319
public partial class RedisCacheDefaultStrategyFixture : RedisCacheFixture
2420
{
2521

StackExRedis/NHibernate.Caches.StackExRedis.Tests/Async/RedisCacheFixture.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
using System.Globalization;
1414
using System.Linq;
1515
using System.Reflection;
16-
using System.Text;
1716
using System.Threading;
18-
using System.Threading.Tasks;
1917
using System.Xml;
2018
using System.Xml.Linq;
2119
using NHibernate.Cache;
@@ -30,6 +28,7 @@
3028

3129
namespace NHibernate.Caches.StackExRedis.Tests
3230
{
31+
using System.Threading.Tasks;
3332
public abstract partial class RedisCacheFixture : CacheFixture
3433
{
3534

@@ -58,7 +57,7 @@ public async Task TestNHibernateAnyTypeSerializationAsync()
5857
entityPersister.PropertyTypes.Returns(propertyValues.Keys.ToArray());
5958

6059
var cacheKey = new CacheKey(1, NHibernateUtil.Int32, entityName, sfImpl);
61-
var cacheEntry = new CacheEntry(propertyValues.Values.ToArray(), entityPersister, false, null, sessionImpl, null);
60+
var cacheEntry = await (CacheEntry.CreateAsync(propertyValues.Values.ToArray(), entityPersister, false, null, sessionImpl, null, CancellationToken.None));
6261

6362
Assert.That(cacheEntry.DisassembledState, Has.Length.EqualTo(1));
6463
var anyObject = cacheEntry.DisassembledState[0];
@@ -126,7 +125,7 @@ public async Task TestNHibernateStandardTypesSerializationAsync()
126125
{NHibernateUtil.TrueFalse, false},
127126
{NHibernateUtil.YesNo, true},
128127
{NHibernateUtil.Class, typeof(IType)},
129-
{NHibernateUtil.ClassMetaType, entityName},
128+
{NHibernateUtil.MetaType, entityName},
130129
{NHibernateUtil.Serializable, new MyEntity {Id = 1}},
131130
{NHibernateUtil.AnsiChar, 'a'},
132131
{NHibernateUtil.XmlDoc, xmlDoc},
@@ -142,7 +141,7 @@ public async Task TestNHibernateStandardTypesSerializationAsync()
142141
entityPersister.PropertyTypes.Returns(propertyValues.Keys.ToArray());
143142

144143
var cacheKey = new CacheKey(1, NHibernateUtil.Int32, entityName, sfImpl);
145-
var cacheEntry = new CacheEntry(propertyValues.Values.ToArray(), entityPersister, false, null, sessionImpl, null);
144+
var cacheEntry = await (CacheEntry.CreateAsync(propertyValues.Values.ToArray(), entityPersister, false, null, sessionImpl, null, CancellationToken.None));
146145

147146
var cache = GetDefaultCache();
148147
await (cache.PutAsync(cacheKey, cacheEntry, CancellationToken.None));
@@ -171,7 +170,7 @@ public async Task TestNHibernateCacheEntrySerializationAsync()
171170
entityPersister.PropertyTypes.Returns(propertyValues.Keys.ToArray());
172171

173172
var cacheKey = new CacheKey(1, NHibernateUtil.Int32, entityName, sfImpl);
174-
var cacheEntry = new CacheEntry(propertyValues.Values.ToArray(), entityPersister, true, 4, sessionImpl, null);
173+
var cacheEntry = await (CacheEntry.CreateAsync(propertyValues.Values.ToArray(), entityPersister, true, 4, sessionImpl, null, CancellationToken.None));
175174

176175
var cache = GetDefaultCache();
177176
await (cache.PutAsync(cacheKey, cacheEntry, CancellationToken.None));
@@ -197,7 +196,7 @@ public async Task TestNHibernateCollectionCacheEntrySerializationAsync()
197196
collection.Disassemble(null).Returns(o => new object[] {"test"});
198197

199198
var cacheKey = new CacheKey(1, NHibernateUtil.Int32, "MyCollection", sfImpl);
200-
var cacheEntry = new CollectionCacheEntry(collection, null);
199+
var cacheEntry = await (CollectionCacheEntry.CreateAsync(collection, null, CancellationToken.None));
201200
Assert.That(cacheEntry.State, Has.Length.EqualTo(1));
202201

203202
var cache = GetDefaultCache();
@@ -216,7 +215,10 @@ public async Task TestNHibernateCacheLockSerializationAsync()
216215
{
217216
var sfImpl = Substitute.For<ISessionFactoryImplementor>();
218217
var cacheKey = new CacheKey(1, NHibernateUtil.Int32, "CacheLock", sfImpl);
219-
var cacheEntry = new CacheLock(1234, 1, 5);
218+
var cacheEntry = new CacheLock
219+
{
220+
Timeout = 1234, Id = 1, Version = 5
221+
};
220222
cacheEntry.Lock(123, 2);
221223

222224
var cache = GetDefaultCache();
@@ -240,7 +242,10 @@ public async Task TestNHibernateCachedItemSerializationAsync()
240242
{
241243
var sfImpl = Substitute.For<ISessionFactoryImplementor>();
242244
var cacheKey = new CacheKey(1, NHibernateUtil.Int32, "CachedItem", sfImpl);
243-
var cacheEntry = new CachedItem("test", 111, 5);
245+
var cacheEntry = new CachedItem
246+
{
247+
Value = "test", FreshTimestamp = 111, Version = 5
248+
};
244249
cacheEntry.Lock(123, 2);
245250

246251
var cache = GetDefaultCache();
@@ -342,7 +347,7 @@ public async Task TestNonEqualObjectsWithEqualToStringUseHashCodeAsync()
342347

343348
await (cache.PutAsync(obj1, value, CancellationToken.None));
344349
Assert.That(await (cache.GetAsync(obj1, CancellationToken.None)), Is.EqualTo(value), "Unable to retrieved cached object for key obj1");
345-
Assert.That(await (cache.GetAsync(obj2, CancellationToken.None)), Is.Null, "Unexectedly found a cache entry for key obj2 after obj1 put");
350+
Assert.That(await (cache.GetAsync(obj2, CancellationToken.None)), Is.Null, "Unexpectedly found a cache entry for key obj2 after obj1 put");
346351
await (cache.RemoveAsync(obj1, CancellationToken.None));
347352
}
348353

@@ -392,7 +397,8 @@ public async Task TestPutManyAsync()
392397

393398
var cache = (RedisCache) GetDefaultCache();
394399
// Due to async version, it may already be there.
395-
await (cache.RemoveManyAsync(keys, CancellationToken.None));
400+
foreach (var key in keys)
401+
await (cache.RemoveAsync(key, CancellationToken.None));
396402

397403
Assert.That(await (cache.GetManyAsync(keys, CancellationToken.None)), Is.EquivalentTo(new object[10]), "cache returned items we didn't add !?!");
398404

@@ -428,7 +434,8 @@ public async Task TestRemoveManyAsync()
428434
Assert.That(items, Is.EquivalentTo(values), "items just added are not there");
429435

430436
// remove it
431-
await (cache.RemoveManyAsync(keys, CancellationToken.None));
437+
foreach (var key in keys)
438+
await (cache.RemoveAsync(key, CancellationToken.None));
432439

433440
// make sure it's not there
434441
items = await (cache.GetManyAsync(keys, CancellationToken.None));
@@ -508,12 +515,5 @@ public async Task TestNullKeyGetManyAsync()
508515
var items = await (cache.GetManyAsync(null, CancellationToken.None));
509516
Assert.IsNull(items);
510517
}
511-
512-
[Test]
513-
public void TestNullKeyRemoveManyAsync()
514-
{
515-
var cache = (RedisCache) GetDefaultCache();
516-
Assert.ThrowsAsync<ArgumentNullException>(() => cache.RemoveManyAsync(null, CancellationToken.None));
517-
}
518518
}
519519
}

StackExRedis/NHibernate.Caches.StackExRedis.Tests/Async/RedisCachePerformanceFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ public Task TestLockUnlockManyOperationAsync()
9999
});
100100
}
101101

102-
private async Task PutCacheDataAsync(ICache cache, Dictionary<CacheKey, List<object>> cacheData, CancellationToken cancellationToken = default(CancellationToken))
102+
private async Task PutCacheDataAsync(CacheBase cache, Dictionary<CacheKey, List<object>> cacheData, CancellationToken cancellationToken = default(CancellationToken))
103103
{
104104
foreach (var pair in cacheData)
105105
{
106106
await (cache.PutAsync(pair.Key, pair.Value, cancellationToken));
107107
}
108108
}
109109

110-
private async Task RemoveCacheDataAsync(ICache cache, Dictionary<CacheKey, List<object>> cacheData, CancellationToken cancellationToken = default(CancellationToken))
110+
private async Task RemoveCacheDataAsync(CacheBase cache, Dictionary<CacheKey, List<object>> cacheData, CancellationToken cancellationToken = default(CancellationToken))
111111
{
112112
foreach (var pair in cacheData)
113113
{

0 commit comments

Comments
 (0)