This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathtest-memcache.js
238 lines (200 loc) · 4.83 KB
/
test-memcache.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
/*
tests for expresso
*/
var util = require('util'),
memcache = require('memcache'),
assert = require('assert'),
port = 11211;
mc = new memcache.Client(port);
mc.on('error', function(e){
if (e.errno == 111){
exports['startup test'] = function(){
assert.ok(false, "You need to have a memcache server running on localhost:11211 for these tests to run");
}
return;
}
exports['startup test'] = function(){
assert.ok(false, "Unexpected error during connection: "+util.inspect(e));
}
});
mc.connect();
mc.addHandler(function() {
// test nonexistent key is null
exports['test null value'] = function(beforeExit) {
var n = 0;
mc.get('no such key', function(err, r) {
assert.equal(null, r);
n++;
});
beforeExit(function() {
assert.equal(1, n);
});
};
// test set, get and expires
exports['test set, get, and expires'] = function(beforeExit) {
var n = 0;
// set key
mc.set('set1', 'asdf1', function() {
n++;
mc.get('set1', function(err, r) {
// assert key is found
assert.equal('asdf1', r);
n++;
// assert key expires after 1s
setTimeout(function() {
mc.get('set1', function(r) {
mc.close();
assert.equal(null, r);
n++;
});
}, 1000);
});
}, 1);
beforeExit(function() {
assert.equal(3, n);
});
};
exports['test set get with integer value'] = function(beforeExit) {
mc.set('testKey', 123, function() {
mc.get('testKey', function(err, r) {
assert.equal(123,r);
});
});
};
// test set and delete
exports['test set del'] = function(beforeExit) {
var n = 0;
// set key
mc.set('set2', 'asdf2', function() {
n++;
mc.get('set2', function(err, r) {
// assert key is found
assert.equal('asdf2', r);
n++;
// delete key
mc.delete('set2', function() {
mc.get('set2', function(err, r) {
// assert key is null
assert.equal(null, r);
n++;
});
});
});
}, 0);
beforeExit(function() {
assert.equal(3, n);
});
};
// test utf8 handling
exports['utf8'] = function(beforeExit) {
mc.set('key1', 'привет', function() {
mc.get('key1', function(err, r) {
assert.equal('привет', r);
});
});
};
// test connecting and disconnecting
exports['con disco'] = function(beforeExit) {
var n = 0;
var mc2 = new memcache.Client(port);
mc2.on('connect', function(){
n++;
mc2.close();
});
mc2.on('close', function(){
n++;
});
mc2.connect();
beforeExit(function() {
assert.equal(2, n);
});
};
// increment / decrement
exports['inc dec'] = function(beforeExit){
var n = 0;
mc.set('inc_bad', 'HELLO', function(err, response){
assert.equal(response, 'STORED');
n++;
mc.increment('inc_bad', 2, function(err, ok){
n++;
assert.match(err, /^CLIENT_ERROR/);
assert.equal(ok, null);
});
mc.decrement('inc_bad', 3, function(err, ok){
n++;
assert.match(err, /^CLIENT_ERROR/);
assert.equal(ok, null);
});
mc.increment('inc_bad', null, function(err, ok){
n++;
assert.match(err, /^CLIENT_ERROR/);
assert.equal(ok, null);
});
mc.decrement('inc_bad', null, function(err, ok){
n++;
assert.match(err, /^CLIENT_ERROR/);
assert.equal(ok, null);
});
});
mc.set('inc_good', '5', function(err, response){
assert.equal(response, 'STORED');
n++;
mc.increment('inc_good', 2, function(err, response){
n++;
assert.equal(response, 7);
mc.increment('inc_good', function(err, response){
n++;
assert.equal(response, 8);
mc.decrement('inc_good', function(err, response){
n++;
assert.equal(response, 7);
mc.decrement('inc_good', 4, function(err, response){
n++;
assert.equal(response, 3);
});
});
});
});
});
beforeExit(function(){
assert.equal(10, n);
});
};
exports['version'] = function(beforeExit){
var n = 0;
mc.version(function(error, success){
n++;
assert.equal(error, null);
assert.length(success, 5);
});
beforeExit(function(){
assert.equal(1, n);
});
};
exports['stats'] = function(beforeExit){
var n = 0;
mc.stats(function(error, success){
n++;
assert.ok(success.pid, "server has a pid");
});
mc.stats('settings', function(error, success){
n++;
assert.ok(success.maxconns);
});
mc.stats('items', function(error, success){ n++; assert.ok(num_keys(success)); });
mc.stats('sizes', function(error, success){ n++; assert.ok(num_keys(success)); });
mc.stats('slabs', function(error, success){ n++; assert.ok(num_keys(success)); });
mc.stats('notreal', function(error, success){
n++;
assert.equal(error, 'ERROR');
});
beforeExit(function(){
assert.equal(6, n);
});
};
});
function num_keys(a){
var i=0;
for (var k in a) i++;
return i;
}