forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheFixture.cs
160 lines (106 loc) · 4.31 KB
/
CacheFixture.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
using System;
using System.Collections.Generic;
using System.Threading;
using NHibernate.Cache;
using NHibernate.Cache.Access;
using NUnit.Framework;
namespace NHibernate.Test.CacheTest
{
[TestFixture]
public class CacheFixture: TestCase
{
[Test]
public void TestSimpleReadWriteCache()
{
DoTestCache(new HashtableCacheProvider());
}
protected CacheKey CreateCacheKey(string text)
{
return new CacheKey(text, NHibernateUtil.String, "Foo", null, null);
}
public void DoTestCache(ICacheProvider cacheProvider)
{
var cache = (CacheBase) cacheProvider.BuildCache(typeof(String).FullName, new Dictionary<string, string>());
long longBefore = Timestamper.Next();
Thread.Sleep(15);
long before = Timestamper.Next();
Thread.Sleep(15);
ICacheConcurrencyStrategy ccs = CreateCache(cache);
// cache something
CacheKey fooKey = CreateCacheKey("foo");
Assert.IsTrue(ccs.Put(fooKey, "foo", before, null, null, false));
Thread.Sleep(15);
long after = Timestamper.Next();
Assert.IsNull(ccs.Get(fooKey, longBefore));
Assert.AreEqual("foo", ccs.Get(fooKey, after));
Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, true));
Assert.IsTrue(ccs.Put(fooKey, "foo", before, null, null, false));
// update it;
ISoftLock fooLock = ccs.Lock(fooKey, null);
Assert.IsNull(ccs.Get(fooKey, after));
Assert.IsNull(ccs.Get(fooKey, longBefore));
Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, false));
Thread.Sleep(15);
long whileLocked = Timestamper.Next();
Assert.IsFalse(ccs.Put(fooKey, "foo", whileLocked, null, null, false));
Thread.Sleep(15);
ccs.Release(fooKey, fooLock);
Assert.IsNull(ccs.Get(fooKey, after));
Assert.IsNull(ccs.Get(fooKey, longBefore));
Assert.IsFalse(ccs.Put(fooKey, "bar", whileLocked, null, null, false));
Assert.IsFalse(ccs.Put(fooKey, "bar", after, null, null, false));
Thread.Sleep(15);
long longAfter = Timestamper.Next();
Assert.IsTrue(ccs.Put(fooKey, "baz", longAfter, null, null, false));
Assert.IsNull(ccs.Get(fooKey, after));
Assert.IsNull(ccs.Get(fooKey, whileLocked));
Thread.Sleep(15);
long longLongAfter = Timestamper.Next();
Assert.AreEqual("baz", ccs.Get(fooKey, longLongAfter));
// update it again, with multiple locks
ISoftLock fooLock1 = ccs.Lock(fooKey, null);
ISoftLock fooLock2 = ccs.Lock(fooKey, null);
Assert.IsNull(ccs.Get(fooKey, longLongAfter));
Thread.Sleep(15);
whileLocked = Timestamper.Next();
Assert.IsFalse(ccs.Put(fooKey, "foo", whileLocked, null, null, false));
Thread.Sleep(15);
ccs.Release(fooKey, fooLock2);
Thread.Sleep(15);
long betweenReleases = Timestamper.Next();
Assert.IsFalse(ccs.Put(fooKey, "bar", betweenReleases, null, null, false));
Assert.IsNull(ccs.Get(fooKey, betweenReleases));
Thread.Sleep(15);
ccs.Release(fooKey, fooLock1);
Assert.IsFalse(ccs.Put(fooKey, "bar", whileLocked, null, null, false));
Thread.Sleep(15);
longAfter = Timestamper.Next();
Assert.IsTrue(ccs.Put(fooKey, "baz", longAfter, null, null, false));
Assert.IsNull(ccs.Get(fooKey, whileLocked));
Thread.Sleep(15);
longLongAfter = Timestamper.Next();
Assert.AreEqual("baz", ccs.Get(fooKey, longLongAfter));
}
private void DoTestMinValueTimestampOnStrategy(CacheBase cache, ICacheConcurrencyStrategy strategy)
{
CacheKey key = CreateCacheKey("key");
strategy.Cache = cache;
strategy.Put(key, "value", long.MinValue, 0, null, false);
Assert.IsNull(strategy.Get(key, long.MinValue), "{0} strategy fails the test", strategy.GetType());
Assert.IsNull(strategy.Get(key, long.MaxValue), "{0} strategy fails the test", strategy.GetType());
}
[Test]
public void MinValueTimestamp()
{
var cache = new HashtableCacheProvider().BuildCache("region", new Dictionary<string, string>());
DoTestMinValueTimestampOnStrategy(cache, CreateCache(cache));
DoTestMinValueTimestampOnStrategy(cache, CreateCache(cache, CacheFactory.NonstrictReadWrite));
DoTestMinValueTimestampOnStrategy(cache, CreateCache(cache, CacheFactory.ReadOnly));
}
protected virtual ICacheConcurrencyStrategy CreateCache(CacheBase cache, string strategy = CacheFactory.ReadWrite)
{
return CacheFactory.CreateCache(strategy, cache, Sfi.Settings);
}
protected override string[] Mappings => Array.Empty<string>();
}
}