This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 876
/
RedisTypedClientTests.cs
164 lines (132 loc) · 4.63 KB
/
RedisTypedClientTests.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
using ServiceStack.Redis.Generic;
namespace ServiceStack.Redis.Tests.Generic
{
[TestFixture, Category("Integration")]
public class RedisTypedClientTests
: RedisClientTestsBase
{
public class CacheRecord
{
public CacheRecord()
{
this.Children = new List<CacheRecordChild>();
}
public string Id { get; set; }
public List<CacheRecordChild> Children { get; set; }
}
public class CacheRecordChild
{
public string Id { get; set; }
public string Data { get; set; }
}
protected IRedisTypedClient<CacheRecord> RedisTyped;
[SetUp]
public override void OnBeforeEachTest()
{
base.OnBeforeEachTest();
Redis?.Dispose();
Redis = new RedisClient(TestConfig.SingleHost)
{
NamespacePrefix = "RedisTypedClientTests:"
};
RedisTyped = Redis.As<CacheRecord>();
}
[TearDown]
public virtual void TearDown()
{
Redis.SearchKeys(Redis.NamespacePrefix + "*").ForEach(t => Redis.Del(t));
}
[Test]
public void Can_Store_with_Prefix()
{
var expected = new CacheRecord() { Id = "123" };
RedisTyped.Store(expected);
var current = Redis.Get<CacheRecord>("RedisTypedClientTests:urn:cacherecord:123");
Assert.AreEqual(expected.Id, current.Id);
}
[Test]
public void Can_Expire()
{
var cachedRecord = new CacheRecord
{
Id = "key",
Children = {
new CacheRecordChild { Id = "childKey", Data = "data" }
}
};
RedisTyped.Store(cachedRecord);
RedisTyped.ExpireIn("key", TimeSpan.FromSeconds(1));
Assert.That(RedisTyped.GetById("key"), Is.Not.Null);
Thread.Sleep(2000);
Assert.That(RedisTyped.GetById("key"), Is.Null);
}
[Ignore("Changes in system clock can break test")]
[Test]
public void Can_ExpireAt()
{
var cachedRecord = new CacheRecord
{
Id = "key",
Children = {
new CacheRecordChild { Id = "childKey", Data = "data" }
}
};
RedisTyped.Store(cachedRecord);
var in2Secs = DateTime.UtcNow.AddSeconds(2);
RedisTyped.ExpireAt("key", in2Secs);
Assert.That(RedisTyped.GetById("key"), Is.Not.Null);
Thread.Sleep(3000);
Assert.That(RedisTyped.GetById("key"), Is.Null);
}
[Test]
public void Can_Delete_All_Items()
{
var cachedRecord = new CacheRecord
{
Id = "key",
Children = {
new CacheRecordChild { Id = "childKey", Data = "data" }
}
};
RedisTyped.Store(cachedRecord);
Assert.That(RedisTyped.GetById("key"), Is.Not.Null);
RedisTyped.DeleteAll();
Assert.That(RedisTyped.GetById("key"), Is.Null);
}
[Test]
public void Can_Delete_All_Items_multiple_batches()
{
// Clear previous usage
Redis.Delete(Redis.GetTypeIdsSetKey(typeof(CacheRecord)));
var cachedRecord = new CacheRecord
{
Id = "key",
Children = {
new CacheRecordChild { Id = "childKey", Data = "data" }
}
};
var exists = Redis.Exists(Redis.GetTypeIdsSetKey(typeof(CacheRecord)));
Assert.That(exists, Is.EqualTo(0));
RedisTyped.Store(cachedRecord);
exists = Redis.Exists(Redis.GetTypeIdsSetKey(typeof(CacheRecord)));
Assert.That(exists, Is.EqualTo(1));
RedisConfig.CommandKeysBatchSize = 5;
for (int i = 0; i < 50; i++)
{
cachedRecord.Id = "key" + i;
RedisTyped.Store(cachedRecord);
}
Assert.That(RedisTyped.GetById("key"), Is.Not.Null);
RedisTyped.DeleteAll();
exists = Redis.Exists(Redis.GetTypeIdsSetKey(typeof(CacheRecord)));
Assert.That(exists, Is.EqualTo(0));
Assert.That(RedisTyped.GetById("key"), Is.Null);
RedisConfig.Reset();
}
}
}