forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildCacheFixture.cs
198 lines (180 loc) · 6.35 KB
/
BuildCacheFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Engine;
using NHibernate.Util;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;
namespace NHibernate.Test.CacheTest
{
[TestFixture]
public class BuildCacheFixture : TestCase
{
protected override string MappingsAssembly => "NHibernate.Test";
protected override string[] Mappings => new[] { "CacheTest.EntitiesInSameRegion.hbm.xml" };
// Disable the TestCase cache overrides.
protected override string CacheConcurrencyStrategy => null;
protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.UseQueryCache, "true");
configuration.SetProperty(Environment.CacheProvider, typeof(LockedCacheProvider).AssemblyQualifiedName);
}
[Theory]
public void CommonRegionHasOneUniqueCacheAndExpectedConcurrency(bool withPrefix)
{
const string prefix = "Prefix";
const string region = "Common";
var fullRegion = (withPrefix ? prefix + "." : "") + region;
ISessionFactoryImplementor sfi = null;
if (withPrefix)
cfg.SetProperty(Environment.CacheRegionPrefix, prefix);
try
{
sfi = withPrefix ? BuildSessionFactory() : Sfi;
var commonRegionCache = sfi.GetSecondLevelCacheRegion(fullRegion);
var entityAName = typeof(EntityA).FullName;
var entityAConcurrencyCache = sfi.GetEntityPersister(entityAName).Cache;
var entityACache = entityAConcurrencyCache.Cache;
var entityBName = typeof(EntityB).FullName;
var entityBConcurrencyCache = sfi.GetEntityPersister(entityBName).Cache;
var entityBCache = entityBConcurrencyCache.Cache;
var relatedAConcurrencyCache =
sfi.GetCollectionPersister(StringHelper.Qualify(entityAName, nameof(EntityA.Related))).Cache;
var relatedACache = relatedAConcurrencyCache.Cache;
var relatedBConcurrencyCache =
sfi.GetCollectionPersister(StringHelper.Qualify(entityBName, nameof(EntityB.Related))).Cache;
var relatedBCache = relatedBConcurrencyCache.Cache;
var queryCache = sfi.GetQueryCache(region).Cache;
Assert.Multiple(
() =>
{
Assert.That(commonRegionCache.RegionName, Is.EqualTo(fullRegion), "Unexpected region name for common region");
Assert.That(entityACache.RegionName, Is.EqualTo(fullRegion), "Unexpected region name for EntityA");
Assert.That(entityBCache.RegionName, Is.EqualTo(fullRegion), "Unexpected region name for EntityB");
Assert.That(relatedACache.RegionName, Is.EqualTo(fullRegion), "Unexpected region name for RelatedA");
Assert.That(relatedBCache.RegionName, Is.EqualTo(fullRegion), "Unexpected region name for RelatedB");
Assert.That(queryCache.RegionName, Is.EqualTo(fullRegion), "Unexpected region name for query cache");
});
Assert.Multiple(
() =>
{
Assert.That(entityAConcurrencyCache, Is.InstanceOf<ReadWriteCache>(), "Unexpected concurrency for EntityA");
Assert.That(relatedAConcurrencyCache, Is.InstanceOf<NonstrictReadWriteCache>(), "Unexpected concurrency for RelatedA");
Assert.That(entityBConcurrencyCache, Is.InstanceOf<ReadOnlyCache>(), "Unexpected concurrency for EntityB");
Assert.That(relatedBConcurrencyCache, Is.InstanceOf<ReadWriteCache>(), "Unexpected concurrency for RelatedB");
Assert.That(entityACache, Is.SameAs(commonRegionCache), "Unexpected cache for EntityA");
Assert.That(entityBCache, Is.SameAs(commonRegionCache), "Unexpected cache for EntityB");
Assert.That(relatedACache, Is.SameAs(commonRegionCache), "Unexpected cache for RelatedA");
Assert.That(relatedBCache, Is.SameAs(commonRegionCache), "Unexpected cache for RelatedB");
Assert.That(queryCache, Is.SameAs(commonRegionCache), "Unexpected cache for query cache");
});
}
finally
{
if (withPrefix)
{
cfg.Properties.Remove(Environment.CacheRegionPrefix);
sfi?.Dispose();
}
}
}
[Test]
public void RetrievedQueryCacheMatchesGloballyStoredOne()
{
var region = "RetrievedQueryCacheMatchesGloballyStoredOne";
LockedCache.Semaphore = new SemaphoreSlim(0);
LockedCache.CreationCount = 0;
try
{
var failures = new ConcurrentBag<Exception>();
var thread1 = new Thread(
() =>
{
try
{
Sfi.GetQueryCache(region);
}
catch (Exception e)
{
failures.Add(e);
}
});
var thread2 = new Thread(
() =>
{
try
{
Sfi.GetQueryCache(region);
}
catch (Exception e)
{
failures.Add(e);
}
});
thread1.Start();
thread2.Start();
// Give some time to threads for reaching the wait, having all of them ready to do most of their job concurrently.
Thread.Sleep(100);
// Let only one finish its job, it should be the one being stored in query cache dictionary.
LockedCache.Semaphore.Release(1);
// Give some time to released thread to finish its job.
Thread.Sleep(100);
// Release other thread
LockedCache.Semaphore.Release(10);
thread1.Join();
thread2.Join();
Assert.That(failures, Is.Empty, $"{failures.Count} thread(s) failed.");
}
finally
{
LockedCache.Semaphore.Dispose();
LockedCache.Semaphore = null;
}
var queryCache = Sfi.GetQueryCache(region).Cache;
var globalCache = Sfi.GetSecondLevelCacheRegion(region);
Assert.That(globalCache, Is.SameAs(queryCache));
Assert.That(LockedCache.CreationCount, Is.EqualTo(1));
}
}
public class LockedCache : HashtableCache
{
public static SemaphoreSlim Semaphore { get; set; }
private static int _creationCount;
public static int CreationCount
{
get => _creationCount;
set => _creationCount = value;
}
public LockedCache(string regionName) : base(regionName)
{
Semaphore?.Wait();
Interlocked.Increment(ref _creationCount);
}
}
public class LockedCacheProvider : ICacheProvider
{
// 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)
{
return new LockedCache(regionName);
}
public long NextTimestamp()
{
return Timestamper.Next();
}
public void Start(IDictionary<string, string> properties)
{
}
public void Stop()
{
}
}
}