-
Notifications
You must be signed in to change notification settings - Fork 275
/
memcached-get-set.test.js
293 lines (245 loc) · 9.28 KB
/
memcached-get-set.test.js
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
/**
* Test dependencies
*/
var assert = require('assert')
, should = require('should')
, fs = require('fs')
, common = require('./common')
, Memcached = require('../');
global.testnumbers = global.testnumbers || 0;
/**
* Expresso test suite for all `get` related
* memcached commands
*/
module.exports = {
/**
* Make sure that the string that we send to the server is correctly
* stored and retrieved. We will be storing random strings to ensure
* that we are not retrieving old data.
*/
"set and get a regular string": function(){
var memcached = new Memcached(common.servers.single)
, message = common.alphabet(256)
, testnr = ++global.testnumbers;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get("test:" + testnr, function(error, answer){
assert.ok(!error);
assert.ok(typeof answer === 'string');
answer.should.eql(message);
memcached.end(); // close connections
});
});
}
/**
* Set a stringified JSON object, and make sure we only return a string
* this should not be flagged as JSON object
*/
, "set and get a JSON.stringify string": function(){
var memcached = new Memcached(common.servers.single)
, message = JSON.stringify({numbers:common.numbers(256),alphabet:common.alphabet(256),dates:new Date(),arrays: [1,2,3, 'foo', 'bar']})
, testnr = ++global.testnumbers;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get("test:" + testnr, function(error, answer){
assert.ok(!error);
assert.ok(typeof answer === 'string');
answer.should.eql(message);
memcached.end(); // close connections
});
});
}
/**
* Make sure that Numbers are correctly send and stored on the server
* retrieval of the number based values can be tricky as the client might
* think that it was a INCR and not a SET operation.. So just to make sure..
*/
, "set and get a regular number": function(){
var memcached = new Memcached(common.servers.single)
, message = common.numbers(256)
, testnr = ++global.testnumbers;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get("test:" + testnr, function(error, answer){
assert.ok(!error);
assert.ok(typeof answer === 'string');
answer.should.eql(answer);
memcached.end(); // close connections
});
});
}
/**
* Objects should be converted to a JSON string, send to the server
* and be automagically JSON.parsed when they are retrieved.
*/
, "set and get a object": function(){
var memcached = new Memcached(common.servers.single)
, message = {
numbers: common.numbers(256)
, alphabet: common.alphabet(256)
, dates: new Date()
, arrays: [1,2,3, 'foo', 'bar']
}
, testnr = ++global.testnumbers;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get("test:" + testnr, function(error, answer){
assert.ok(!error);
assert.ok(!Array.isArray(answer) && typeof answer == 'object');
assert.ok(JSON.stringify(message) == JSON.stringify(answer));
memcached.end(); // close connections
});
});
}
/**
* Arrays should be converted to a JSON string, send to the server
* and be automagically JSON.parsed when they are retrieved.
*/
, "set and get a array": function(){
var memcached = new Memcached(common.servers.single)
, message = [
{
numbers: common.numbers(256)
, alphabet: common.alphabet(256)
, dates: new Date()
, arrays: [1,2,3, 'foo', 'bar']
}
, {
numbers: common.numbers(256)
, alphabet: common.alphabet(256)
, dates: new Date()
, arrays: [1,2,3, 'foo', 'bar']
}
]
, testnr = ++global.testnumbers;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get("test:" + testnr, function(error, answer){
assert.ok(!error);
assert.ok(Array.isArray(answer));
assert.ok(JSON.stringify(answer) == JSON.stringify(message));
memcached.end(); // close connections
});
});
}
/**
* Buffers are commonly used for binary transports So we need to make sure
* we support them properly. But please note, that we need to compare the
* strings on a "binary" level, because that is the encoding the Memcached
* client will be using, as there is no indication of what encoding the
* buffer is in.
*/
, "set and get <buffers> with a binary image": function(){
var memcached = new Memcached(common.servers.single)
, message = fs.readFileSync(__dirname + '/fixtures/hotchicks.jpg')
, testnr = ++global.testnumbers;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get("test:" + testnr, function(error, answer){
assert.ok(!error);
assert.ok(answer.toString('binary') === answer.toString('binary'));
memcached.end(); // close connections
});
});
}
/**
* Get binary of the lipsum.txt, send it over the connection and see
* if after we retrieved it, it's still the same when we compare the
* original with the memcached based version.
*
* A use case for this would be storing <buffers> with HTML data in
* memcached as a single cache pool..
*/
, "set and get <buffers> with a binary text file": function(){
var memcached = new Memcached(common.servers.single)
, message = fs.readFileSync(__dirname + '/fixtures/lipsum.txt')
, testnr = ++global.testnumbers;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get("test:" + testnr, function(error, answer){
assert.ok(!error);
assert.ok(answer.toString('utf8') === answer.toString('utf8'));
assert.ok(answer.toString('ascii') === answer.toString('ascii'));
memcached.end(); // close connections
});
});
}
/**
* Not only small strings, but also large strings should be processed
* without any issues.
*/
, "set and get large text files": function(){
var memcached = new Memcached(common.servers.single)
, message = fs.readFileSync(__dirname + '/fixtures/lipsum.txt', 'utf8')
, testnr = ++global.testnumbers;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get("test:" + testnr, function(error, answer){
assert.ok(!error);
assert.ok(typeof answer === 'string');
answer.should.eql(message);
memcached.end(); // close connections
});
});
}
/**
* A multi get on a single server is different than a multi server multi get
* as a multi server multi get will need to do a multi get over multiple servers
* yes, that's allot of multi's in one single sentence thanks for noticing
*/
, "multi get single server": function(){
var memcached = new Memcached(common.servers.single)
, message = common.alphabet(256)
, message2 = common.alphabet(256)
, testnr = ++global.testnumbers;
memcached.set("test1:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.set("test2:" + testnr, message2, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get(["test1:" + testnr, "test2:" + testnr], function(error, answer){
assert.ok(!error);
assert.ok(typeof answer === 'object');
answer["test1:" + testnr].should.eql(message);
answer["test2:" + testnr].should.eql(message2);
memcached.end(); // close connections
});
});
});
}
/**
* A multi get on a single server is different than a multi server multi get
* as a multi server multi get will need to do a multi get over multiple servers
* yes, that's allot of multi's in one single sentence thanks for noticing
*/
, "multi get multi server": function(){
var memcached = new Memcached(common.servers.multi)
, message = common.alphabet(256)
, message2 = common.alphabet(256)
, testnr = ++global.testnumbers;
memcached.set("test1:" + testnr, message, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.set("test2:" + testnr, message2, 1000, function(error, ok){
assert.ok(!error);
ok.should.be.true;
memcached.get(["test1:" + testnr,"test2:" + testnr], function(error, answer){
assert.ok(!error);
assert.ok(typeof answer === 'object');
answer["test1:" + testnr].should.eql(message);
answer["test2:" + testnr].should.eql(message2);
memcached.end(); // close connections
});
});
});
}
};