Skip to content

Commit b0d5a1c

Browse files
committed
Unit Tests for all methods
1 parent 343ba8d commit b0d5a1c

File tree

5 files changed

+248
-66
lines changed

5 files changed

+248
-66
lines changed

examples/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var bin = new binary.Binary();
77
var key = "node-memcached-test";
88
var data = "test-data";
99

10-
var encoder = new Buffer(24 + key.length + data.length + 8 + 24 + key.length + 24 + 24 + (24 + key.length)*3);
10+
var encoder = new Buffer(24 + key.length + data.length + 8 + 24 + key.length + (24 + key.length)*5);
1111

1212
var pos = 0;
1313
var size = bin.pack([

examples/testclient.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var memc = require("../lib/client");
33

44
var client = new memc.Connection();
55

6-
client.chunked = false;
7-
client.encoding = memc.constants.encodings.UTF8;
6+
client.parser.chunked = false;
7+
client.parser.encoding = memc.constants.encodings.UTF8;
88

99
var key = "node-memcached-test";
1010
var value = "hello";

examples/unit.js

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
var sys = require("sys");
2+
var memc = require("../lib/client");
3+
4+
var client = new memc.Connection();
5+
6+
client.parser.chunked = false;
7+
client.parser.encoding = memc.constants.encodings.UTF8;
8+
9+
var key = "node-memcached-test";
10+
var value = "hello";
11+
12+
/*
13+
{"method": "connect", "params":"port, host, cb"}
14+
{"method": "get", "params":"key, cb"}
15+
{"method": "getq", "params":"key, cb"}
16+
{"method": "getk", "params":"key, cb"}
17+
{"method": "getkq", "params":"key, cb"}
18+
{"method": "set", "params":"key, value, flags, expiration, cb"}
19+
{"method": "setq", "params":"key, value, flags, expiration, cb"}
20+
{"method": "add", "params":"key, value, flags, expiration, cb"}
21+
{"method": "addq", "params":"key, value, flags, expiration, cb"}
22+
{"method": "replace", "params":"key, value, flags, expiration, cb"}
23+
{"method": "replaceq", "params":"key, value, flags, expiration, cb"}
24+
{"method": "inc", "params":"key, delta, initial, expiration, cb"}
25+
{"method": "dec", "params":"key, delta, initial, expiration, cb"}
26+
{"method": "incq", "params":"key, delta, initial, expiration, cb"}
27+
{"method": "decq", "params":"key, delta, initial, expiration, cb"}
28+
{"method": "delete", "params":"key, cb"}
29+
{"method": "deleteq", "params":"key, cb"}
30+
{"method": "append", "params":"key, value, cb"}
31+
{"method": "prepend", "params":"key, value, cb"}
32+
{"method": "appendq", "params":"key, value, cb"}
33+
{"method": "prependq", "params":"key, value, cb"}
34+
{"method": "quit", "params":"cb"}
35+
{"method": "quitq", "params":"cb"}
36+
{"method": "version", "params":"cb"}
37+
{"method": "stat", "params":"key, cb"}
38+
{"method": "noop", "params":"cb"}
39+
{"method": "flush", "params":"expiration, cb"}
40+
{"method": "flushq", "params":"expiration, cb"}
41+
*/
42+
43+
client.connect("/tmp/memcached.sock", null, function() {
44+
sys.puts("connected");
45+
client.flushq(null, function(message) {
46+
// we should only get a callback if there is an error
47+
sys.puts("FLUSHQ: " + (message.header.status == memc.constants.status.NO_ERROR?"OK":"FAIL"));
48+
});
49+
client.flush(null, function(message) {
50+
sys.puts("FLUSH: " + (message.header.status == memc.constants.status.NO_ERROR?"OK":"FAIL"));
51+
});
52+
client.version(function(message) {
53+
sys.puts("VERSION: " + message.body);
54+
});
55+
client.noop(function(message) {
56+
sys.puts("NOOP: " + (message.header.status == memc.constants.status.NO_ERROR?"OK":"FAIL"));
57+
});
58+
client.set(key, value, 0x01, 3600, function(message) {
59+
if(message.header.status == memc.constants.status.NO_ERROR) {
60+
sys.puts("SET: OK");
61+
client.getq(key, function(message) {
62+
sys.puts("GETQ: " + message.body);
63+
});
64+
client.getkq(key, function(message) {
65+
sys.puts("GETKQ: " + message.key + ":" + message.body);
66+
});
67+
client.getk(key, function(message) {
68+
sys.puts("GETK: " + message.key + ":" + message.body);
69+
});
70+
client.setq("arse", "bum", 0x01, 3600, function(message) {
71+
// we should only get a callback if there is an error
72+
sys.puts("SETQ: " + message.header.status);
73+
});
74+
client.getq("arse", function(message) {
75+
sys.puts("GETQ: " + message.body);
76+
});
77+
client.add("arse", "bum", 0x01, 3600, function(message) {
78+
sys.puts("ADD: " + message.header.status);
79+
});
80+
client.add("arse1", "bum", 0x01, 3600, function(message) {
81+
sys.puts("ADD: " + message.header.status);
82+
});
83+
client.addq("arse1", "bum", 0x01, 3600, function(message) {
84+
// we should only get a callback if there is an error
85+
sys.puts("ADDQ: " + message.header.status);
86+
});
87+
client.replace("arse1", "bum", 0x01, 3600, function(message) {
88+
sys.puts("REPLACE: " + message.header.status);
89+
});
90+
client.replace("arse123", "bum", 0x01, 3600, function(message) {
91+
sys.puts("REPLACE: " + message.header.status);
92+
});
93+
client.replaceq("arse123", "bum", 0x01, 3600, function(message) {
94+
sys.puts("REPLACEQ: " + message.header.status);
95+
});
96+
client.replaceq("arse1", "wee", 0x01, 3600, function(message) {
97+
sys.puts("REPLACEQ: " + message.header.status);
98+
});
99+
client.incq("tester1", 1, 1, 3600, function(message) {
100+
sys.puts("INCQ: " + message.header.status);
101+
sys.puts(message.body);
102+
});
103+
client.incq("tester1", 2, 0, 3600, function(message) {
104+
sys.puts("INCQ: " + message.header.status);
105+
sys.puts(JSON.stringify(message));
106+
});
107+
client.inc("tester1", 3, 0, 3600, function(message) {
108+
sys.puts("INC: " + message.header.status);
109+
sys.puts(JSON.stringify(message));
110+
});
111+
client.decq("tester1", 1, 0, 3600, function(message) {
112+
sys.puts("DECQ: " + message.header.status);
113+
sys.puts(JSON.stringify(message));
114+
});
115+
client.dec("tester1", 2, 0, 3600, function(message) {
116+
sys.puts("DEC: " + message.header.status);
117+
sys.puts(JSON.stringify(message));
118+
});
119+
client.deleteq("tester109", function(message) {
120+
sys.puts("DELETEQ: " + message.header.status);
121+
});
122+
client.deleteq("tester1", function(message) {
123+
sys.puts("DELETEQ: " + message.header.status);
124+
});
125+
client.delete("tester1", function(message) {
126+
sys.puts("DELETE: " + message.header.status);
127+
});
128+
client.appendq("arse", "1", function(message) {
129+
sys.puts("APPENDQ: " + message.header.status);
130+
});
131+
client.prependq("arse", "1", function(message) {
132+
sys.puts("PREPENDQ: " + message.header.status);
133+
});
134+
client.append("arse", "2", function(message) {
135+
sys.puts("APPEND: " + message.header.status);
136+
});
137+
client.prepend("arse", "2", function(message) {
138+
sys.puts("PREPEND: " + message.header.status);
139+
});
140+
client.get("arse", function(message) {
141+
sys.puts("GET: " + message.body);
142+
});
143+
client.get(key, function(message) {
144+
sys.puts("GET: " + message.body);
145+
var stat = {};
146+
client.stat(null, function(message) {
147+
if(message.header.bodylen > 0) {
148+
stat[message.key] = message.body;
149+
}
150+
else {
151+
sys.puts("STAT:\n" + JSON.stringify(stat, null, "\t"));
152+
client.delete(key, function(message) {
153+
sys.puts("DELETE: " + (message.header.status == memc.constants.status.NO_ERROR?"OK":"FAIL"));
154+
client.quit(function(message) {
155+
sys.puts("QUIT: " + (message.header.status == memc.constants.status.NO_ERROR?"OK":"FAIL"));
156+
});
157+
});
158+
}
159+
});
160+
});
161+
}
162+
else {
163+
sys.puts("SET: Error = " + message.header.status);
164+
}
165+
});
166+
});
167+
168+
client.on("error", function(err) {
169+
sys.puts("client error\n" + JSON.stringify(err, null, "\t"));
170+
});
171+
172+
client.on("close", function() {
173+
sys.puts("client closed");
174+
});
175+

0 commit comments

Comments
 (0)