-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathCSRedisCachingProviderTest.cs
More file actions
354 lines (296 loc) · 13.8 KB
/
CSRedisCachingProviderTest.cs
File metadata and controls
354 lines (296 loc) · 13.8 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
using System.Threading.Tasks;
namespace EasyCaching.UnitTests
{
using System;
using EasyCaching.Core;
using EasyCaching.Core.Configurations;
using EasyCaching.CSRedis;
using EasyCaching.Serialization.Json;
using EasyCaching.Serialization.MessagePack;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Xunit;
public class CSRedisCachingProviderTest : BaseCachingProviderTest
{
public CSRedisCachingProviderTest()
{
_defaultTs = TimeSpan.FromSeconds(30);
_nameSpace = "CSRedisBase";
}
protected override IEasyCachingProvider CreateCachingProvider(Action<BaseProviderOptions> additionalSetup)
{
var services = new ServiceCollection();
services.AddEasyCaching(x =>
x.UseCSRedis(options =>
{
options.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,defaultDatabase=13,poolsize=10"
}
};
additionalSetup(options);
}).UseCSRedisLock().WithJson(EasyCachingConstValue.DefaultCSRedisName));
IServiceProvider serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetService<IEasyCachingProvider>();
}
/*[Fact]
public async Task Use_Redis6_ACL_Should_Succeed()
{
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(x =>
x.UseCSRedis(options =>
{
options.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,user=user,password=userpwd,defaultDatabase=13,poolsize=10"
}
};
}).UseCSRedisLock().WithJson(EasyCachingConstValue.DefaultCSRedisName));
IServiceProvider serviceProvider = services.BuildServiceProvider();
var provider = serviceProvider.GetService<IEasyCachingProvider>();
var key = Guid.NewGuid().ToString();
var value = "value";
await provider.SetAsync(key, value, TimeSpan.FromSeconds(20));
var getValue = await provider.GetAsync<string>(key);
Assert.Equal(value, getValue?.Value);
}*/
[Fact]
public void GetDatabase_Should_Succeed()
{
var db = _provider.Database;
Assert.NotNull(db);
Assert.IsType<EasyCachingCSRedisClient>(db);
}
[Fact]
public void GetDatabase_And_Use_Raw_Method_Should_Succeed()
{
var db = (EasyCachingCSRedisClient)_provider.Database;
var flag = db.Ping();
Assert.True(flag);
}
}
public class CSRedisCachingProviderWithNamedSerTest
{
private readonly IEasyCachingProviderFactory _providerFactory;
public CSRedisCachingProviderWithNamedSerTest()
{
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(option =>
{
option.UseCSRedis(config =>
{
config.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,defaultDatabase=2,poolsize=10"
}
};
config.SerializerName = "cs11";
}, "cs1");
option.UseCSRedis(config =>
{
config.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,defaultDatabase=3,poolsize=10"
}
};
}, "cs2");
option.WithJson("json").WithMessagePack("cs11").WithJson("cs2");
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
_providerFactory = serviceProvider.GetService<IEasyCachingProviderFactory>();
}
[Fact]
public void NamedSerializerTest()
{
var cs1 = _providerFactory.GetCachingProvider("cs1");
var cs2 = _providerFactory.GetCachingProvider("cs2");
var info1 = cs1.GetProviderInfo();
var info2 = cs2.GetProviderInfo();
Assert.Equal("cs11", info1.Serializer.Name);
Assert.Equal("cs2", info2.Serializer.Name);
}
}
public class CSRedisCachingProviderWithKeyPrefixTest
{
private readonly IEasyCachingProviderFactory _providerFactory;
public CSRedisCachingProviderWithKeyPrefixTest()
{
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(x =>
{
x.UseCSRedis(config =>
{
config.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,defaultDatabase=3,poolsize=10"
}
};
config.SerializerName = "json";
}, "NotKeyPrefix");
x.UseCSRedis(config =>
{
config.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,defaultDatabase=3,poolsize=10,prefix=foo:"
}
};
config.SerializerName = "json";
}, "WithKeyPrefix");
x.WithJson("json");
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
_providerFactory = serviceProvider.GetService<IEasyCachingProviderFactory>();
}
[Fact]
public void KeyPrefixTest()
{
var NotKeyPrefix = _providerFactory.GetCachingProvider("NotKeyPrefix");
var WithKeyPrefix = _providerFactory.GetCachingProvider("WithKeyPrefix");
WithKeyPrefix.Set("KeyPrefix", "ok", TimeSpan.FromSeconds(10));
var val1 = NotKeyPrefix.Get<string>("foo:" + "KeyPrefix");
var val2 = WithKeyPrefix.Get<string>("foo:" + "KeyPrefix");
Assert.NotEqual(val1.Value, val2.Value);
var val3 = WithKeyPrefix.Get<string>("KeyPrefix");
Assert.Equal(val1.Value, val3.Value);
}
[Fact]
public void RemoveByPrefixTest()
{
var WithKeyPrefix = _providerFactory.GetCachingProvider("WithKeyPrefix");
WithKeyPrefix.Set("KeyPrefix1", "ok", TimeSpan.FromSeconds(10));
WithKeyPrefix.Set("KeyPrefix2", "ok", TimeSpan.FromSeconds(10));
var val1 = WithKeyPrefix.Get<string>("KeyPrefix1");
var val2 = WithKeyPrefix.Get<string>("KeyPrefix2");
Assert.True(val1.HasValue);
Assert.True(val2.HasValue);
Assert.Equal(val1.Value, val2.Value);
WithKeyPrefix.RemoveByPrefix("Key");
var val3 = WithKeyPrefix.Get<string>("KeyPrefix1");
var val4 = WithKeyPrefix.Get<string>("KeyPrefix2");
Assert.False(val3.HasValue);
Assert.False(val4.HasValue);
}
[Theory]
[InlineData("WithKeyPrefix")]
[InlineData("NotKeyPrefix")]
public void RemoveByKeyPatternTest(string provider)
{
var WithKeyPrefix = _providerFactory.GetCachingProvider(provider);
WithKeyPrefix.Set("garden:pots:flowers", "ok", TimeSpan.FromSeconds(10));
WithKeyPrefix.Set("garden:pots:flowers:test", "ok", TimeSpan.FromSeconds(10));
WithKeyPrefix.Set("garden:flowerspots:test", "ok", TimeSpan.FromSeconds(10));
WithKeyPrefix.Set("boo:foo", "ok", TimeSpan.FromSeconds(10));
WithKeyPrefix.Set("boo:test:foo", "ok", TimeSpan.FromSeconds(10));
WithKeyPrefix.Set("sky:birds:bar", "ok", TimeSpan.FromSeconds(10));
WithKeyPrefix.Set("sky:birds:test:bar", "ok", TimeSpan.FromSeconds(10));
WithKeyPrefix.Set("akey", "ok", TimeSpan.FromSeconds(10));
var val1 = WithKeyPrefix.Get<string>("garden:pots:flowers");
var val2 = WithKeyPrefix.Get<string>("garden:pots:flowers:test");
var val3 = WithKeyPrefix.Get<string>("garden:flowerspots:test");
var val4 = WithKeyPrefix.Get<string>("boo:foo");
var val5 = WithKeyPrefix.Get<string>("boo:test:foo");
var val6 = WithKeyPrefix.Get<string>("sky:birds:bar");
var val7 = WithKeyPrefix.Get<string>("sky:birds:test:bar");
var val8 = WithKeyPrefix.Get<string>("akey");
Assert.True(val1.HasValue);
Assert.True(val2.HasValue);
Assert.True(val3.HasValue);
Assert.True(val4.HasValue);
Assert.True(val5.HasValue);
Assert.True(val6.HasValue);
Assert.True(val7.HasValue);
Assert.True(val8.HasValue);
// contains
WithKeyPrefix.RemoveByPattern("*:pots:*");
// postfix
WithKeyPrefix.RemoveByPattern("*foo");
// prefix
WithKeyPrefix.RemoveByPattern("sky*");
// exact
WithKeyPrefix.RemoveByPattern("akey");
var val9 = WithKeyPrefix.Get<string>("garden:pots:flowers");
var val10 = WithKeyPrefix.Get<string>("garden:pots:flowers:test");
var val11 = WithKeyPrefix.Get<string>("garden:flowerspots:test");
var val12 = WithKeyPrefix.Get<string>("boo:foo");
var val13 = WithKeyPrefix.Get<string>("boo:test:foo");
var val14 = WithKeyPrefix.Get<string>("sky:birds:bar");
var val15 = WithKeyPrefix.Get<string>("sky:birds:test:bar");
var val16 = WithKeyPrefix.Get<string>("akey");
Assert.False(val9.HasValue);
Assert.False(val10.HasValue);
Assert.True(val11.HasValue);
Assert.False(val12.HasValue);
Assert.False(val13.HasValue);
Assert.False(val14.HasValue);
Assert.False(val15.HasValue);
Assert.False(val16.HasValue);
}
[Theory]
[InlineData("WithKeyPrefix")]
[InlineData("NotKeyPrefix")]
public async Task RemoveByKeyPatternAsyncTest(string provider)
{
var WithKeyPrefix = _providerFactory.GetCachingProvider(provider);
await WithKeyPrefix.SetAsync("garden:pots:flowers", "ok", TimeSpan.FromSeconds(10));
await WithKeyPrefix.SetAsync("garden:pots:flowers:test", "ok", TimeSpan.FromSeconds(10));
await WithKeyPrefix.SetAsync("garden:flowerspots:test", "ok", TimeSpan.FromSeconds(10));
await WithKeyPrefix.SetAsync("boo:foo", "ok", TimeSpan.FromSeconds(10));
await WithKeyPrefix.SetAsync("boo:test:foo", "ok", TimeSpan.FromSeconds(10));
await WithKeyPrefix.SetAsync("sky:birds:bar", "ok", TimeSpan.FromSeconds(10));
await WithKeyPrefix.SetAsync("sky:birds:test:bar", "ok", TimeSpan.FromSeconds(10));
await WithKeyPrefix.SetAsync("akey", "ok", TimeSpan.FromSeconds(10));
var val1 = WithKeyPrefix.Get<string>("garden:pots:flowers");
var val2 = WithKeyPrefix.Get<string>("garden:pots:flowers:test");
var val3 = WithKeyPrefix.Get<string>("garden:flowerspots:test");
var val4 = WithKeyPrefix.Get<string>("boo:foo");
var val5 = WithKeyPrefix.Get<string>("boo:test:foo");
var val6 = WithKeyPrefix.Get<string>("sky:birds:bar");
var val7 = WithKeyPrefix.Get<string>("sky:birds:test:bar");
var val8 = WithKeyPrefix.Get<string>("akey");
Assert.True(val1.HasValue);
Assert.True(val2.HasValue);
Assert.True(val3.HasValue);
Assert.True(val4.HasValue);
Assert.True(val5.HasValue);
Assert.True(val6.HasValue);
Assert.True(val7.HasValue);
Assert.True(val8.HasValue);
// contains
await WithKeyPrefix.RemoveByPatternAsync("*:pots:*");
// postfix
await WithKeyPrefix.RemoveByPatternAsync("*foo");
// prefix
await WithKeyPrefix.RemoveByPatternAsync("sky*");
// exact
await WithKeyPrefix.RemoveByPatternAsync("akey");
var val9 = WithKeyPrefix.Get<string>("garden:pots:flowers");
var val10 = WithKeyPrefix.Get<string>("garden:pots:flowers:test");
var val11 = WithKeyPrefix.Get<string>("garden:flowerspots:test");
var val12 = WithKeyPrefix.Get<string>("boo:foo");
var val13 = WithKeyPrefix.Get<string>("boo:test:foo");
var val14 = WithKeyPrefix.Get<string>("sky:birds:bar");
var val15 = WithKeyPrefix.Get<string>("sky:birds:test:bar");
var val16 = WithKeyPrefix.Get<string>("akey");
Assert.False(val9.HasValue);
Assert.False(val10.HasValue);
Assert.True(val11.HasValue);
Assert.False(val12.HasValue);
Assert.False(val13.HasValue);
Assert.False(val14.HasValue);
Assert.False(val15.HasValue);
Assert.False(val16.HasValue);
}
}
}