forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheClientTestsAsyncBase.cs
More file actions
336 lines (260 loc) · 11.3 KB
/
Copy pathCacheClientTestsAsyncBase.cs
File metadata and controls
336 lines (260 loc) · 11.3 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.Text;
namespace ServiceStack.Azure.Tests.Shared
{
[TestFixture]
public abstract class CacheClientTestsAsyncBase
{
private readonly ICacheClientAsync Cache;
public abstract ICacheClientAsync CreateClient();
protected CacheClientTestsAsyncBase()
{
Cache = CreateClient();
}
[SetUp]
public async Task SetUp()
{
await Cache.FlushAllAsync();
}
[Test]
public async Task Does_flush_all()
{
await 3.TimesAsync(async i =>
await Cache.SetAsync(i.ToUrn<Item>(), new Item { Id = i, Name = "Name" + i }));
Assert.That(await Cache.GetAsync<Item>(1.ToUrn<Item>()), Is.Not.Null);
await Cache.FlushAllAsync();
Assert.That(await Cache.GetAsync<Item>(1.ToUrn<Item>()), Is.Null);
}
[Test]
public async Task Can_set_and_remove_entry()
{
var key = 1.ToUrn<Item>();
var item = await Cache.GetAsync<Item>(key);
Assert.That(item, Is.Null);
var whenNotExists = await Cache.SetAsync(key, new Item { Id = 1, Name = "Foo" });
Assert.That(whenNotExists, Is.True);
var whenExists = await Cache.SetAsync(key, new Item { Id = 1, Name = "Foo" });
Assert.That(whenExists, Is.True);
item = await Cache.GetAsync<Item>(key);
Assert.That(item, Is.Not.Null);
Assert.That(item.Name, Is.EqualTo("Foo"));
whenExists = await Cache.RemoveAsync(key);
Assert.That(whenExists, Is.True);
whenNotExists = await Cache.RemoveAsync(key);
Assert.That(whenNotExists, Is.False);
}
[Test]
public async Task Can_update_existing_entry()
{
var key = 1.ToUrn<Item>();
await Cache.SetAsync(key, new Item { Id = 1, Name = "Foo" });
await Cache.SetAsync(key, new Item { Id = 2, Name = "Updated" });
var item = await Cache.GetAsync<Item>(key);
Assert.That(item.Id, Is.EqualTo(2));
Assert.That(item.Name, Is.EqualTo("Updated"));
}
[Test]
public async Task Does_SetAll_and_GetAll()
{
var map = 3.Times(i => new Item { Id = i, Name = "Name" + i })
.ToSafeDictionary(x => x.ToUrn());
await Cache.SetAllAsync(map);
var cacheMap = await Cache.GetAllAsync<Item>(map.Keys);
Assert.That(cacheMap, Is.EquivalentTo(map));
}
[Test]
public async Task Does_not_return_expired_items()
{
var key = 1.ToUrn<Item>();
await Cache.SetAsync(key, new Item { Id = 1, Name = "Foo" }, DateTime.UtcNow.AddSeconds(-1));
Assert.That(await Cache.GetAsync<Item>(key), Is.Null);
await Cache.RemoveAsync(key);
await Cache.SetAsync(key, new Item { Id = 1, Name = "Foo" }, TimeSpan.FromMilliseconds(100));
var entry = await Cache.GetAsync<Item>(key);
Assert.That(entry, Is.Not.Null);
Thread.Sleep(200);
Assert.That(await Cache.GetAsync<Item>(key), Is.Null);
await Cache.RemoveAsync(key);
await Cache.SetAsync(key, new Item { Id = 1, Name = "Foo" }, DateTime.UtcNow.AddMilliseconds(200));
entry = await Cache.GetAsync<Item>(key);
Assert.That(entry, Is.Not.Null);
Thread.Sleep(300);
Assert.That(await Cache.GetAsync<Item>(key), Is.Null);
}
[Test]
public async Task Can_increment_and_decrement_values()
{
Assert.That(await Cache.IncrementAsync("incr:a", 2), Is.EqualTo(2));
Assert.That(await Cache.IncrementAsync("incr:a", 3), Is.EqualTo(5));
Assert.That(await Cache.DecrementAsync("decr:a", 2), Is.EqualTo(-2));
Assert.That(await Cache.DecrementAsync("decr:a", 3), Is.EqualTo(-5));
}
[Test]
public async Task Can_increment_and_reset_values()
{
Assert.That(await Cache.IncrementAsync("incr:counter", 10), Is.EqualTo(10));
await Cache.SetAsync("incr:counter", 0);
Assert.That(await Cache.IncrementAsync("incr:counter", 10), Is.EqualTo(10));
}
[Test]
public async Task Can_remove_multiple_items()
{
var map = 5.Times(i => new Item { Id = i, Name = "Name" + i })
.ToSafeDictionary(x => x.ToUrn());
await Cache.SetAllAsync(map);
await Cache.RemoveAllAsync(map.Keys);
var cacheMap = await Cache.GetAllAsync<Item>(map.Keys);
Assert.That(cacheMap.Count, Is.EqualTo(5));
Assert.That(cacheMap.Values.All(x => x == null));
}
[Test]
public async Task Can_retrieve_IAuthSession()
{
IAuthSession session = new CustomAuthSession
{
Id = "sess-1",
UserAuthId = "1",
Custom = "custom"
};
var sessionKey = SessionFeature.GetSessionKey(session.Id);
await Cache.SetAsync(sessionKey, session, SessionFeature.DefaultSessionExpiry);
var sessionCache = await Cache.GetAsync<IAuthSession>(sessionKey);
Assert.That(sessionCache, Is.Not.Null);
var typedSession = sessionCache as CustomAuthSession;
Assert.That(typedSession, Is.Not.Null);
Assert.That(typedSession.Custom, Is.EqualTo("custom"));
}
[Test]
public async Task Can_retrieve_TimeToLive_on_IAuthSession()
{
IAuthSession session = new CustomAuthSession
{
Id = "sess-1",
UserAuthId = "1",
Custom = "custom"
};
var sessionKey = SessionFeature.GetSessionKey(session.Id);
await Cache.RemoveAsync(sessionKey);
var ttl = await Cache.GetTimeToLiveAsync(sessionKey);
Assert.That(ttl, Is.Null);
await Cache.SetAsync(sessionKey, session);
ttl = await Cache.GetTimeToLiveAsync(sessionKey);
Assert.That(ttl.Value, Is.EqualTo(TimeSpan.MaxValue));
var sessionExpiry = SessionFeature.DefaultSessionExpiry;
await Cache.SetAsync(sessionKey, session, sessionExpiry);
ttl = await Cache.GetTimeToLiveAsync(sessionKey);
Assert.That(ttl.Value, Is.GreaterThan(TimeSpan.FromSeconds(0)));
Assert.That(ttl.Value, Is.LessThan(sessionExpiry).
Or.EqualTo(sessionExpiry).Within(TimeSpan.FromSeconds(1)));
}
[Test]
public async Task Can_retrieve_IAuthSession_with_global_ExcludeTypeInfo_set()
{
JsConfig.ExcludeTypeInfo = true;
IAuthSession session = new CustomAuthSession
{
Id = "sess-1",
UserAuthId = "1",
Custom = "custom"
};
var sessionKey = SessionFeature.GetSessionKey(session.Id);
await Cache.SetAsync(sessionKey, session, SessionFeature.DefaultSessionExpiry);
var sessionCache = await Cache.GetAsync<IAuthSession>(sessionKey);
Assert.That(sessionCache, Is.Not.Null);
var typedSession = sessionCache as CustomAuthSession;
Assert.That(typedSession, Is.Not.Null);
Assert.That(typedSession.Custom, Is.EqualTo("custom"));
JsConfig.Reset();
}
[Test]
public async Task Can_cache_multiple_items_in_parallel()
{
var cache = CreateClient();
var fns = 10.TimesAsync(async i =>
await cache.SetAsync("concurrent-test", "Data: {0}".Fmt(i))
);
await Task.WhenAll(fns);
var entry = await cache.GetAsync<string>("concurrent-test");
Assert.That(entry, Does.StartWith("Data: "));
}
[Test]
public async Task Can_GetKeysByPattern()
{
if (!(Cache is ICacheClientExtended))
return;
JsConfig.ExcludeTypeInfo = true;
for (int i = 0; i < 5; i++)
{
IAuthSession session = new CustomAuthSession
{
Id = "sess-" + i,
UserAuthId = i.ToString(),
Custom = "custom" + i
};
var sessionKey = SessionFeature.GetSessionKey(session.Id);
await Cache.SetAsync(sessionKey, session, SessionFeature.DefaultSessionExpiry);
await Cache.SetAsync("otherkey" + i, i);
}
var sessionPattern = IdUtils.CreateUrn<IAuthSession>("");
Assert.That(sessionPattern, Is.EqualTo("urn:iauthsession:"));
#if !NETFX
var sessionKeys = await Cache.GetKeysStartingWithAsync(sessionPattern).ToListAsync();
Assert.That(sessionKeys.Count, Is.EqualTo(5));
Assert.That(sessionKeys.All(x => x.StartsWith("urn:iauthsession:")));
var allSessions = await Cache.GetAllAsync<IAuthSession>(sessionKeys);
Assert.That(allSessions.Values.Count(x => x != null), Is.EqualTo(sessionKeys.Count));
var allKeys = (await Cache.GetAllKeysAsync().ToListAsync()).ToList();
Assert.That(allKeys.Count, Is.EqualTo(10));
#endif
JsConfig.Reset();
}
[Test]
public async Task Can_Cache_AllFields()
{
JsConfig.DateHandler = DateHandler.ISO8601;
var dto = new AllFields
{
Id = 1,
NullableId = 2,
Byte = 3,
Short = 4,
Int = 5,
Long = 6,
UShort = 7,
UInt = 8,
Float = 1.1f,
Double = 2.2d,
Decimal = 3.3m,
String = "String",
DateTime = DateTime.Now,
TimeSpan = new TimeSpan(1, 1, 1, 1, 1),
Guid = Guid.NewGuid(),
NullableTimeSpan = new TimeSpan(2, 2, 2),
NullableGuid = new Guid("4B6BB8AE-57B5-4B5B-8632-0C35AF0B3168"),
};
await Cache.SetAsync("allfields", dto);
var fromCache = await Cache.GetAsync<AllFields>("allfields");
Assert.That(fromCache.DateTime, Is.EqualTo(dto.DateTime));
Assert.That(fromCache.Equals(dto));
JsConfig.Reset();
}
[Test]
public async Task Can_RemoveAll_and_GetKeysStartingWith_with_prefix()
{
var cache = Cache.WithPrefix("prefix.");
await cache.SetAsync("test_QUERY_Deposit__Query_Deposit_10_1", "A");
await cache.SetAsync("test_QUERY_Deposit__0_1___CUSTOM", "B");
var keys = (await cache.GetKeysStartingWithAsync("test_QUERY_Deposit").ToListAsync()).ToList();
Assert.That(keys.Count, Is.EqualTo(2));
await cache.RemoveAllAsync(keys);
var newKeys = (await cache.GetKeysStartingWithAsync("test_QUERY_Deposit").ToListAsync()).ToList();
Assert.That(newKeys.Count, Is.EqualTo(0));
}
}
}