forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchableCacheSubclassFixture.cs
150 lines (136 loc) · 3.94 KB
/
BatchableCacheSubclassFixture.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.DomainModel;
using NHibernate.Loader;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;
namespace NHibernate.Test.CacheTest
{
[TestFixture(BatchFetchStyle.Dynamic)]
[TestFixture(BatchFetchStyle.Legacy)]
public class BatchableCacheSubclassFixture : TestCase
{
private readonly BatchFetchStyle _fetchStyle;
public BatchableCacheSubclassFixture(BatchFetchStyle fetchStyle)
{
_fetchStyle = fetchStyle;
}
protected override string[] Mappings
{
get
{
return new string[]
{
"FooBar.hbm.xml",
"Baz.hbm.xml",
"Qux.hbm.xml",
"Glarch.hbm.xml",
"Fum.hbm.xml",
"Fumm.hbm.xml",
"Fo.hbm.xml",
"One.hbm.xml",
"Many.hbm.xml",
"Immutable.hbm.xml",
"Fee.hbm.xml",
"Vetoer.hbm.xml",
"Holder.hbm.xml",
"Location.hbm.xml",
"Stuff.hbm.xml",
"Container.hbm.xml",
"Simple.hbm.xml"
};
}
}
protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Cfg.Environment.UseSecondLevelCache, "true");
configuration.SetProperty(Cfg.Environment.UseQueryCache, "true");
configuration.SetProperty(Cfg.Environment.CacheProvider, typeof(BatchableCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Cfg.Environment.BatchFetchStyle, _fetchStyle.ToString());
}
protected override void OnSetUp()
{
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
FooProxy flast = new Bar();
s.Save(flast);
for (int i = 0; i < 5; i++)
{
FooProxy foo = new Bar();
s.Save(foo);
flast.TheFoo = foo;
flast = flast.TheFoo;
flast.String = "foo" + (i + 1);
}
tx.Commit();
}
}
protected override void OnTearDown()
{
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
s.Delete("from NHibernate.DomainModel.Foo as foo");
tx.Commit();
}
}
[Test]
public void BatchableRootEntityTest()
{
var persister = Sfi.GetEntityPersister(typeof(Foo).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
var fooCache = (BatchableCache) persister.Cache.Cache;
persister = Sfi.GetEntityPersister(typeof(Bar).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
var barCache = (BatchableCache) persister.Cache.Cache;
Assert.That(barCache, Is.EqualTo(fooCache));
// Add Bar to cache
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var list = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List();
Assert.AreEqual(6, list.Count);
tx.Commit();
}
Assert.That(fooCache.PutCalls, Has.Count.EqualTo(6)); // Bar is not batchable
Assert.That(fooCache.PutMultipleCalls, Has.Count.EqualTo(0));
// Batch fetch by two from cache
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var enumerator =
s.CreateQuery("from foo in class NHibernate.DomainModel.Foo order by foo.String").Enumerable().GetEnumerator();
var i = 1;
while (enumerator.MoveNext())
{
BarProxy bar = (BarProxy) enumerator.Current;
if (i % 2 == 0)
{
string theString = bar.String; // Load the entity
}
i++;
}
tx.Commit();
}
Assert.That(fooCache.GetMultipleCalls, Has.Count.EqualTo(3));
// Check that each key was used only once when retriving objects from the cache
var uniqueKeys = new HashSet<string>();
foreach (var keys in fooCache.GetMultipleCalls)
{
Assert.That(keys, Has.Length.EqualTo(2));
foreach (var key in keys.OfType<CacheKey>().Select(o => (string) o.Key))
{
Assert.That(uniqueKeys, Does.Not.Contains(key));
uniqueKeys.Add(key);
}
}
}
}
}