forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheClientTestsBase.cs
More file actions
504 lines (409 loc) · 16.3 KB
/
Copy pathCacheClientTestsBase.cs
File metadata and controls
504 lines (409 loc) · 16.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.OrmLite;
using ServiceStack.Text;
namespace ServiceStack.Server.Tests.Shared
{
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
protected bool Equals(Item other)
{
return Id == other.Id && string.Equals(Name, other.Name);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Item)obj);
}
public override int GetHashCode()
{
unchecked
{
return (Id * 397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}
}
public class AllFields
{
public int Id { get; set; }
public int? NullableId { get; set; }
public byte Byte { get; set; }
public short Short { get; set; }
public int Int { get; set; }
public long Long { get; set; }
public ushort UShort { get; set; }
public uint UInt { get; set; }
public ulong ULong { get; set; }
public float Float { get; set; }
public double Double { get; set; }
public decimal Decimal { get; set; }
public string String { get; set; }
public DateTime DateTime { get; set; }
public TimeSpan TimeSpan { get; set; }
public Guid Guid { get; set; }
public DateTime? NullableDateTime { get; set; }
public TimeSpan? NullableTimeSpan { get; set; }
public Guid? NullableGuid { get; set; }
protected bool Equals(AllFields other)
{
return Id == other.Id &&
NullableId == other.NullableId &&
Byte == other.Byte &&
Short == other.Short &&
Int == other.Int &&
Long == other.Long &&
UShort == other.UShort &&
UInt == other.UInt &&
ULong == other.ULong &&
Float.Equals(other.Float) &&
Double.Equals(other.Double) &&
Decimal == other.Decimal &&
string.Equals(String, other.String) &&
DateTime.Equals(other.DateTime) &&
TimeSpan.Equals(other.TimeSpan) &&
Guid.Equals(other.Guid) &&
NullableDateTime.Equals(other.NullableDateTime) &&
NullableTimeSpan.Equals(other.NullableTimeSpan) &&
NullableGuid.Equals(other.NullableGuid);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((AllFields)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = Id;
hashCode = (hashCode * 397) ^ NullableId.GetHashCode();
hashCode = (hashCode * 397) ^ Byte.GetHashCode();
hashCode = (hashCode * 397) ^ Short.GetHashCode();
hashCode = (hashCode * 397) ^ Int;
hashCode = (hashCode * 397) ^ Long.GetHashCode();
hashCode = (hashCode * 397) ^ UShort.GetHashCode();
hashCode = (hashCode * 397) ^ (int)UInt;
hashCode = (hashCode * 397) ^ ULong.GetHashCode();
hashCode = (hashCode * 397) ^ Float.GetHashCode();
hashCode = (hashCode * 397) ^ Double.GetHashCode();
hashCode = (hashCode * 397) ^ Decimal.GetHashCode();
hashCode = (hashCode * 397) ^ (String != null ? String.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ DateTime.GetHashCode();
hashCode = (hashCode * 397) ^ TimeSpan.GetHashCode();
hashCode = (hashCode * 397) ^ Guid.GetHashCode();
hashCode = (hashCode * 397) ^ NullableDateTime.GetHashCode();
hashCode = (hashCode * 397) ^ NullableTimeSpan.GetHashCode();
hashCode = (hashCode * 397) ^ NullableGuid.GetHashCode();
return hashCode;
}
}
}
public class CustomAuthSession : AuthUserSession
{
[DataMember]
public string Custom { get; set; }
}
[TestFixture]
public abstract class CacheClientTestsBase
{
private readonly ICacheClient Cache;
public abstract ICacheClient CreateClient();
protected CacheClientTestsBase()
{
Cache = CreateClient();
}
[OneTimeTearDown]
public void TestFixtureTearDown()
{
Cache.Dispose();
}
[SetUp]
public void SetUp()
{
Cache.FlushAll();
}
[Test]
public void Does_flush_all()
{
3.Times(i =>
Cache.Set(i.ToUrn<Item>(), new Item { Id = i, Name = "Name" + i }));
Assert.That(Cache.Get<Item>(1.ToUrn<Item>()), Is.Not.Null);
Cache.FlushAll();
Assert.That(Cache.Get<Item>(1.ToUrn<Item>()), Is.Null);
}
[Test]
public void Can_set_and_remove_entry()
{
var key = 1.ToUrn<Item>();
var item = Cache.Get<Item>(key);
Assert.That(item, Is.Null);
var whenNotExists = Cache.Set(key, new Item { Id = 1, Name = "Foo" });
Assert.That(whenNotExists, Is.True);
var whenExists = Cache.Set(key, new Item { Id = 1, Name = "Foo" });
Assert.That(whenExists, Is.True);
item = Cache.Get<Item>(key);
Assert.That(item, Is.Not.Null);
Assert.That(item.Name, Is.EqualTo("Foo"));
whenExists = Cache.Remove(key);
Assert.That(whenExists, Is.True);
whenNotExists = Cache.Remove(key);
Assert.That(whenNotExists, Is.False);
}
[Test]
public void Can_update_existing_entry()
{
var key = 1.ToUrn<Item>();
Cache.Set(key, new Item { Id = 1, Name = "Foo" });
Cache.Set(key, new Item { Id = 2, Name = "Updated" });
var item = Cache.Get<Item>(key);
Assert.That(item.Id, Is.EqualTo(2));
Assert.That(item.Name, Is.EqualTo("Updated"));
}
[Test]
public void Does_SetAll_and_GetAll()
{
var map = 3.Times(i => new Item { Id = i, Name = "Name" + i })
.ToSafeDictionary(x => x.ToUrn());
Cache.SetAll(map);
var cacheMap = Cache.GetAll<Item>(map.Keys);
Assert.That(cacheMap, Is.EquivalentTo(map));
}
[Test]
public void Does_not_return_expired_items()
{
var key = 1.ToUrn<Item>();
Cache.Set(key, new Item { Id = 1, Name = "Foo" }, DateTime.UtcNow.AddSeconds(-1));
Assert.That(Cache.Get<Item>(key), Is.Null);
Cache.Remove(key);
Cache.Set(key, new Item { Id = 1, Name = "Foo" }, TimeSpan.FromMilliseconds(100));
var entry = Cache.Get<Item>(key);
Assert.That(entry, Is.Not.Null);
Thread.Sleep(200);
Assert.That(Cache.Get<Item>(key), Is.Null);
Cache.Remove(key);
Cache.Set(key, new Item { Id = 1, Name = "Foo" }, DateTime.UtcNow.AddMilliseconds(200));
entry = Cache.Get<Item>(key);
Assert.That(entry, Is.Not.Null);
Thread.Sleep(300);
Assert.That(Cache.Get<Item>(key), Is.Null);
}
[Test]
public void Expired_item_returns_correct_GetTimeToLive()
{
var ormliteCache = Cache as OrmLiteCacheClient;
var key = "int:key";
var value = Cache.GetOrCreate(key, TimeSpan.FromMilliseconds(100), () => 1);
var ttl = Cache.GetTimeToLive(key);
if (ormliteCache != null)
{
using (var db = ormliteCache.DbFactory.OpenDbConnection())
{
var row = db.SingleById<CacheEntry>(key);
Assert.That(row, Is.Not.Null);
Assert.That(row.ExpiryDate, Is.Not.Null);
}
}
Assert.That(value, Is.EqualTo(1));
Assert.That(ttl.Value.TotalMilliseconds, Is.GreaterThan(0));
Thread.Sleep(200);
value = Cache.Get<int>(key);
ttl = Cache.GetTimeToLive(key);
Assert.That(value, Is.EqualTo(0));
Assert.That(ttl, Is.Null);
if (ormliteCache != null)
{
using (var db = ormliteCache.DbFactory.OpenDbConnection())
{
var row = db.SingleById<CacheEntry>(key);
Assert.That(row, Is.Null);
}
}
}
[Test]
public void Can_increment_and_decrement_values()
{
Assert.That(Cache.Increment("incr:a", 2), Is.EqualTo(2));
Assert.That(Cache.Increment("incr:a", 3), Is.EqualTo(5));
Assert.That(Cache.Decrement("decr:a", 2), Is.EqualTo(-2));
Assert.That(Cache.Decrement("decr:a", 3), Is.EqualTo(-5));
}
[Test]
public void Can_increment_and_reset_values()
{
Assert.That(Cache.Increment("incr:counter", 10), Is.EqualTo(10));
Cache.Set("incr:counter", 0);
Assert.That(Cache.Increment("incr:counter", 10), Is.EqualTo(10));
}
[Test]
public void Can_remove_multiple_items()
{
var map = 5.Times(i => new Item { Id = i, Name = "Name" + i })
.ToSafeDictionary(x => x.ToUrn());
Cache.SetAll(map);
Cache.RemoveAll(map.Keys);
var cacheMap = Cache.GetAll<Item>(map.Keys);
Assert.That(cacheMap.Count, Is.EqualTo(5));
Assert.That(cacheMap.Values.All(x => x == null));
}
[Test]
public void Can_retrieve_IAuthSession()
{
IAuthSession session = new CustomAuthSession
{
Id = "sess-1",
UserAuthId = "1",
Custom = "custom"
};
var sessionKey = SessionFeature.GetSessionKey(session.Id);
Cache.Set(sessionKey, session, SessionFeature.DefaultSessionExpiry);
var sessionCache = Cache.Get<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 void Can_retrieve_TimeToLive_on_IAuthSession()
{
IAuthSession session = new CustomAuthSession
{
Id = "sess-1",
UserAuthId = "1",
Custom = "custom"
};
var sessionKey = SessionFeature.GetSessionKey(session.Id);
Cache.Remove(sessionKey);
var ttl = Cache.GetTimeToLive(sessionKey);
Assert.That(ttl, Is.Null);
Cache.Set(sessionKey, session);
ttl = Cache.GetTimeToLive(sessionKey);
Assert.That(ttl.Value, Is.EqualTo(TimeSpan.MaxValue));
var sessionExpiry = SessionFeature.DefaultSessionExpiry;
Cache.Set(sessionKey, session, sessionExpiry);
ttl = Cache.GetTimeToLive(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 void 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);
Cache.Set(sessionKey, session, SessionFeature.DefaultSessionExpiry);
var sessionCache = Cache.Get<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 void Can_cache_multiple_items_in_parallel()
{
var cache = CreateClient();
var fns = 10.Times(i => (Action)(() =>
{
cache.Set("concurrent-test", "Data: {0}".Fmt(i));
}));
Parallel.Invoke(fns.ToArray());
var entry = cache.Get<string>("concurrent-test");
Assert.That(entry, Does.StartWith("Data: "));
}
[Test]
public void Can_set_get_and_remove_ISession()
{
var sessionA = new SessionFactory(CreateClient()).CreateSession("a");
var sessionB = new SessionFactory(CreateClient()).CreateSession("b");
3.Times(i =>
{
sessionA.Set("key" + i, "value" + i);
sessionB.Set("key" + i, "value" + i);
});
var value1 = sessionA.Get<String>("key1");
Assert.That(value1, Is.EqualTo("value1"));
sessionA.RemoveAll();
value1 = sessionA.Get<String>("key1");
Assert.That(value1, Is.Null);
value1 = sessionB.Get<String>("key1");
Assert.That(value1, Is.EqualTo("value1"));
}
[Test]
public void Can_GetKeysByPattern()
{
if (!(Cache is ICacheClientExtended))
return;
JsConfig.ExcludeTypeInfo = true;
5.Times(i =>
{
IAuthSession session = new CustomAuthSession
{
Id = "sess-" + i,
UserAuthId = i.ToString(),
Custom = "custom" + i
};
var sessionKey = SessionFeature.GetSessionKey(session.Id);
Cache.Set(sessionKey, session, SessionFeature.DefaultSessionExpiry);
Cache.Set("otherkey" + i, i);
});
var sessionPattern = IdUtils.CreateUrn<IAuthSession>("");
Assert.That(sessionPattern, Is.EqualTo("urn:iauthsession:"));
var sessionKeys = Cache.GetKeysStartingWith(sessionPattern).ToList();
Assert.That(sessionKeys.Count, Is.EqualTo(5));
Assert.That(sessionKeys.All(x => x.StartsWith("urn:iauthsession:")));
var allSessions = Cache.GetAll<IAuthSession>(sessionKeys);
Assert.That(allSessions.Values.Count(x => x != null), Is.EqualTo(sessionKeys.Count));
var allKeys = Cache.GetAllKeys().ToList();
Assert.That(allKeys.Count, Is.EqualTo(10));
JsConfig.Reset();
}
[Test]
public void 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"),
};
Cache.Set("allfields", dto);
var fromCache = Cache.Get<AllFields>("allfields");
Assert.That(fromCache.DateTime, Is.EqualTo(dto.DateTime));
Assert.That(fromCache.Equals(dto));
JsConfig.Reset();
}
}
}