forked from enyim/EnyimMemcached
-
Notifications
You must be signed in to change notification settings - Fork 7
/
MemcachedClientTest.cs
executable file
·402 lines (323 loc) · 11.3 KB
/
MemcachedClientTest.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
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
using System;
using System.Net;
using System.Threading;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using NUnit.Framework;
using System.Collections.Generic;
using System.Text;
namespace MemcachedTest
{
[TestFixture]
public abstract class MemcachedClientTest
{
private static readonly Enyim.Caching.ILog log = Enyim.Caching.LogManager.GetLogger(typeof(MemcachedClientTest));
public const string TestObjectKey = "Hello_World";
protected abstract MemcachedClient GetClient();
[TestFixtureSetUp]
public void Setup()
{
#if log4net
log4net.Config.XmlConfigurator.Configure();
#endif
}
[global::System.Serializable]
public class TestData
{
public TestData() { }
public string FieldA;
public string FieldB;
public int FieldC;
public bool FieldD;
}
/// <summary>
///A test for Store (StoreMode, string, byte[], int, int)
///</summary>
[TestCase]
public void StoreObjectTest()
{
TestData td = new TestData();
td.FieldA = "Hello";
td.FieldB = "World";
td.FieldC = 19810619;
td.FieldD = true;
using (MemcachedClient client = GetClient())
{
Assert.IsTrue(client.Store(StoreMode.Set, TestObjectKey, td));
}
}
[TestCase]
public void GetObjectTest()
{
TestData td = new TestData();
td.FieldA = "Hello";
td.FieldB = "World";
td.FieldC = 19810619;
td.FieldD = true;
using (MemcachedClient client = GetClient())
{
Assert.IsTrue(client.Store(StoreMode.Set, TestObjectKey, td), "Initialization failed.");
TestData td2 = client.Get<TestData>(TestObjectKey);
Assert.IsNotNull(td2, "Get returned null.");
Assert.AreEqual(td2.FieldA, "Hello", "Object was corrupted.");
Assert.AreEqual(td2.FieldB, "World", "Object was corrupted.");
Assert.AreEqual(td2.FieldC, 19810619, "Object was corrupted.");
Assert.AreEqual(td2.FieldD, true, "Object was corrupted.");
}
}
[TestCase]
public void DeleteObjectTest()
{
using (MemcachedClient client = GetClient())
{
TestData td = new TestData();
Assert.IsTrue(client.Store(StoreMode.Set, TestObjectKey, td), "Initialization failed.");
Assert.IsTrue(client.Remove(TestObjectKey), "Remove failed.");
Assert.IsNull(client.Get(TestObjectKey), "Remove failed.");
}
}
[TestCase]
public void StoreStringTest()
{
using (MemcachedClient client = GetClient())
{
Assert.IsTrue(client.Store(StoreMode.Set, "TestString", "Hello world!"), "StoreString failed.");
Assert.AreEqual("Hello world!", client.Get<string>("TestString"));
}
}
[TestCase]
public void StoreNullTest()
{
using (MemcachedClient client = GetClient())
{
Assert.IsTrue(client.Store(StoreMode.Set, "TestNull", null), "StoreNull failed.");
object retval;
Assert.IsTrue(client.TryGet("TestNull", out retval), "Failed to retrieve TestNull");
Assert.IsNull(retval, "Retval should be null");
}
}
[TestCase]
public void StoreLongTest()
{
using (MemcachedClient client = GetClient())
{
Assert.IsTrue(client.Store(StoreMode.Set, "TestLong", 65432123456L), "StoreLong failed.");
Assert.AreEqual(65432123456L, client.Get<long>("TestLong"));
}
}
[TestCase]
public void StoreArrayTest()
{
byte[] bigBuffer = new byte[200 * 1024];
for (int i = 0; i < bigBuffer.Length / 256; i++)
{
for (int j = 0; j < 256; j++)
{
bigBuffer[i * 256 + j] = (byte)j;
}
}
using (MemcachedClient client = GetClient())
{
Assert.IsTrue(client.Store(StoreMode.Set, "BigBuffer", bigBuffer), "StoreArray failed");
byte[] bigBuffer2 = client.Get<byte[]>("BigBuffer");
for (int i = 0; i < bigBuffer.Length / 256; i++)
{
for (int j = 0; j < 256; j++)
{
if (bigBuffer2[i * 256 + j] != (byte)j)
{
Assert.AreEqual(j, bigBuffer[i * 256 + j], "Data should be {0} but its {1}");
break;
}
}
}
}
}
[TestCase]
public void ExpirationTestTimeSpan()
{
using (MemcachedClient client = GetClient())
{
Assert.IsTrue(client.Store(StoreMode.Set, "ExpirationTest:TimeSpan", "ExpirationTest:TimeSpan", new TimeSpan(0, 0, 5)), "Expires:Timespan failed");
Assert.AreEqual("ExpirationTest:TimeSpan", client.Get("ExpirationTest:TimeSpan"), "Expires:Timespan store failed");
Thread.Sleep(8000);
Assert.IsNull(client.Get("ExpirationTest:TimeSpan"), "ExpirationTest:TimeSpan item did not expire");
}
}
[TestCase]
public void ExpirationTestDateTime()
{
using (MemcachedClient client = GetClient())
{
DateTime expiresAt = DateTime.Now.AddSeconds(5);
Assert.IsTrue(client.Store(StoreMode.Set, "Expires:DateTime", "Expires:DateTime", expiresAt), "Expires:DateTime failed");
Assert.AreEqual("Expires:DateTime", client.Get("Expires:DateTime"), "Expires:DateTime store failed");
Thread.Sleep(8000);
Assert.IsNull(client.Get("Expires:DateTime"), "Expires:DateTime item did not expire");
}
}
[TestCase]
public void AddSetReplaceTest()
{
using (MemcachedClient client = GetClient())
{
log.Debug("Cache should be empty.");
Assert.IsTrue(client.Store(StoreMode.Set, "VALUE", "1"), "Initialization failed");
log.Debug("Setting VALUE to 1.");
Assert.AreEqual("1", client.Get("VALUE"), "Store failed");
log.Debug("Adding VALUE; this should return false.");
Assert.IsFalse(client.Store(StoreMode.Add, "VALUE", "2"), "Add should have failed");
log.Debug("Checking if VALUE is still '1'.");
Assert.AreEqual("1", client.Get("VALUE"), "Item should not have been Added");
log.Debug("Replacing VALUE; this should return true.");
Assert.IsTrue(client.Store(StoreMode.Replace, "VALUE", "4"), "Replace failed");
log.Debug("Checking if VALUE is '4' so it got replaced.");
Assert.AreEqual("4", client.Get("VALUE"), "Item should have been replaced");
log.Debug("Removing VALUE.");
Assert.IsTrue(client.Remove("VALUE"), "Remove failed");
log.Debug("Replacing VALUE; this should return false.");
Assert.IsFalse(client.Store(StoreMode.Replace, "VALUE", "8"), "Replace should not have succeeded");
log.Debug("Checking if VALUE is 'null' so it was not replaced.");
Assert.IsNull(client.Get("VALUE"), "Item should not have been Replaced");
log.Debug("Adding VALUE; this should return true.");
Assert.IsTrue(client.Store(StoreMode.Add, "VALUE", "16"), "Item should have been Added");
log.Debug("Checking if VALUE is '16' so it was added.");
Assert.AreEqual("16", client.Get("VALUE"), "Add failed");
log.Debug("Passed AddSetReplaceTest.");
}
}
class NonSerializableObject
{
public string Value;
}
[TestCase]
public void NonSerializableTest()
{
using (MemcachedClient client = GetClient())
{
Assert.IsFalse(client.Store(StoreMode.Set, "VALUE", new NonSerializableObject()), "Storing a non serializable object should have failed");
}
}
private string[] keyParts = { "multi", "get", "test", "key", "parts", "test", "values" };
protected string MakeRandomKey(int partCount)
{
var sb = new StringBuilder();
var rnd = new Random();
for (var i = 0; i < partCount; i++)
{
sb.Append(keyParts[rnd.Next(keyParts.Length)]).Append(":");
}
sb.Length--;
return sb.ToString();
}
[TestCase]
public virtual void MultiGetTest()
{
var prefix = new Random().Next(300) + ":";
// note, this test will fail, if memcached version is < 1.2.4
using (var client = GetClient())
{
var keys = new List<string>();
for (int i = 0; i < 1000; i++)
{
string k = prefix + "_Hello_Multi_Get_" + i;
keys.Add(k);
Assert.IsTrue(client.Store(StoreMode.Set, k, i), "Store of " + k + " failed");
}
//Thread.Sleep(5000);
//for (var i = 0; i < 100; i++)
//{
// Assert.AreEqual(client.Get(keys[i]), i, "Store of " + keys[i] + " failed");
//}
IDictionary<string, object> retvals = client.Get(keys);
object value;
for (int i = 0; i < keys.Count; i++)
{
string key = keys[i];
if (!retvals.TryGetValue(key, out value))
Console.WriteLine("missing key: " + key);
}
Assert.AreEqual(keys.Count, retvals.Count, "MultiGet should have returned " + keys.Count + " items.");
for (int i = 0; i < keys.Count; i++)
{
string key = keys[i];
Assert.IsTrue(retvals.TryGetValue(key, out value), "missing key: " + key);
Assert.AreEqual(value, i, "Invalid value returned: " + value);
}
}
}
[TestCase]
public virtual void MultiGetWithCasTest()
{
var prefix = new Random().Next(300) + ":";
// note, this test will fail, if memcached version is < 1.2.4
using (var client = GetClient())
{
var keys = new List<string>();
for (int i = 0; i < 1000; i++)
{
string k = prefix + "_Cas_Multi_Get_" + i;
keys.Add(k);
Assert.IsTrue(client.Store(StoreMode.Set, k, i), "Store of " + k + " failed");
}
var retvals = client.GetWithCas(keys);
CasResult<object> value;
for (int i = 0; i < keys.Count; i++)
{
string key = keys[i];
if (!retvals.TryGetValue(key, out value))
Console.WriteLine("missing key: " + key);
}
Assert.AreEqual(keys.Count, retvals.Count, "MultiGet should have returned " + keys.Count + " items.");
for (int i = 0; i < keys.Count; i++)
{
string key = keys[i];
Assert.IsTrue(retvals.TryGetValue(key, out value), "missing key: " + key);
Assert.AreEqual(value.Result, i, "Invalid value returned: " + value);
Assert.AreNotEqual(value.Cas, 0, key + " key has no cas");
}
}
}
[TestCase]
public void FlushTest()
{
using (MemcachedClient client = GetClient())
{
Assert.IsTrue(client.Store(StoreMode.Set, "qwer", "1"), "Initialization failed");
Assert.IsTrue(client.Store(StoreMode.Set, "tyui", "1"), "Initialization failed");
Assert.IsTrue(client.Store(StoreMode.Set, "polk", "1"), "Initialization failed");
Assert.IsTrue(client.Store(StoreMode.Set, "mnbv", "1"), "Initialization failed");
Assert.IsTrue(client.Store(StoreMode.Set, "zxcv", "1"), "Initialization failed");
Assert.IsTrue(client.Store(StoreMode.Set, "gfsd", "1"), "Initialization failed");
Assert.AreEqual("1", client.Get("mnbv"), "Setup for FlushAll() failed");
client.FlushAll();
Assert.IsNull(client.Get("qwer"), "FlushAll() failed.");
Assert.IsNull(client.Get("tyui"), "FlushAll() failed.");
Assert.IsNull(client.Get("polk"), "FlushAll() failed.");
Assert.IsNull(client.Get("mnbv"), "FlushAll() failed.");
Assert.IsNull(client.Get("zxcv"), "FlushAll() failed.");
Assert.IsNull(client.Get("gfsd"), "FlushAll() failed.");
}
}
}
}
#region [ License information ]
/* ************************************************************
*
* Copyright (c) 2010 Attila Kiskó, enyim.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion