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
/
test-memcache.js
182 lines (152 loc) · 3.51 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
/*
tests for expresso
*/
var sys = require('sys');
var memcache = require('memcache');
mc = new memcache.Client();
mc.on('error', function(e){
if (e.errno == 111){
exports['startup test'] = function(assert){
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){
assert.ok(false, "Unexpected error during connection: "+sys.inspect(e));
}
});
mc.connect();
mc.addHandler(function() {
// test nonexistent key is null
exports['test null value'] = function(assert, beforeExit) {
var n = 0;
mc.get('no such key', function(r) {
assert.equal(null, r);
n++;
});
beforeExit(function() {
assert.equal(1, n);
});
};
// test set, get and expires
exports['test set, get, and expires'] = function(assert, beforeExit) {
var n = 0;
// set key
mc.set('set1', 'asdf1', function() {
n++;
mc.get('set1', function(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);
});
};
// test set and delete
exports['test set del'] = function(assert, beforeExit) {
var n = 0;
// set key
mc.set('set2', 'asdf2', function() {
n++;
mc.get('set2', function(r) {
// assert key is found
assert.equal('asdf2', r);
n++;
// delete key
mc.del('set2', function() {
mc.get('set2', function(r) {
// assert key is null
assert.equal(null, r);
n++;
});
});
});
}, 0);
beforeExit(function() {
assert.equal(3, n);
});
};
// test connecting and disconnecting
exports['con disco'] = function(assert, beforeExit) {
var n = 0;
var mc2 = new memcache.Client();
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(assert, beforeExit){
var n = 0;
mc.set('inc_bad', 'HELLO', function(response){
assert.equal(response, 'STORED');
n++;
mc.increment('inc_bad', 2, function(ok, err){
n++;
assert.equal(ok, null);
assert.match(err, /^CLIENT_ERROR/);
});
mc.decrement('inc_bad', 3, function(ok, err){
n++;
assert.equal(ok, null);
assert.match(err, /^CLIENT_ERROR/);
});
mc.increment('inc_bad', null, function(ok, err){
n++;
assert.equal(ok, null);
assert.match(err, /^CLIENT_ERROR/);
});
mc.decrement('inc_bad', null, function(ok, err){
n++;
assert.equal(ok, null);
assert.match(err, /^CLIENT_ERROR/);
});
});
mc.set('inc_good', '5', function(response){
assert.equal(response, 'STORED');
n++;
mc.increment('inc_good', 2, function(response){
n++;
assert.equal(response, 7);
mc.increment('inc_good', function(response){
n++;
assert.equal(response, 8);
mc.decrement('inc_good', function(response){
n++;
assert.equal(response, 7);
mc.decrement('inc_good', 4, function(response){
n++;
assert.equal(response, 3);
});
});
});
});
});
beforeExit(function(){
assert.equal(10, n);
});
};
exports['version'] = function(assert, beforeExit){
mc.version(function(success, error){
assert.equal(error, null);
assert.length(success, 5);
});
}
});