]> git.saurik.com Git - redis.git/blob - deps/hiredis/test.c
Merge branch 'aaslave'
[redis.git] / deps / hiredis / test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <sys/time.h>
6 #include <assert.h>
7 #include <unistd.h>
8
9 #include "hiredis.h"
10
11 /* The following lines make up our testing "framework" :) */
12 static int tests = 0, fails = 0;
13 #define test(_s) { printf("#%02d ", ++tests); printf(_s); }
14 #define test_cond(_c) if(_c) printf("PASSED\n"); else {printf("FAILED\n"); fails++;}
15
16 static long long usec(void) {
17 struct timeval tv;
18 gettimeofday(&tv,NULL);
19 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
20 }
21
22 static int use_unix = 0;
23 static redisContext *blocking_context = NULL;
24 static void __connect(redisContext **target) {
25 *target = blocking_context = (use_unix ?
26 redisConnectUnix("/tmp/redis.sock") : redisConnect((char*)"127.0.0.1", 6379));
27 if (blocking_context->err) {
28 printf("Connection error: %s\n", blocking_context->errstr);
29 exit(1);
30 }
31 }
32
33 static void test_format_commands() {
34 char *cmd;
35 int len;
36
37 test("Format command without interpolation: ");
38 len = redisFormatCommand(&cmd,"SET foo bar");
39 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
40 len == 4+4+(3+2)+4+(3+2)+4+(3+2));
41 free(cmd);
42
43 test("Format command with %%s string interpolation: ");
44 len = redisFormatCommand(&cmd,"SET %s %s","foo","bar");
45 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
46 len == 4+4+(3+2)+4+(3+2)+4+(3+2));
47 free(cmd);
48
49 test("Format command with %%b string interpolation: ");
50 len = redisFormatCommand(&cmd,"SET %b %b","foo",3,"b\0r",3);
51 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nb\0r\r\n",len) == 0 &&
52 len == 4+4+(3+2)+4+(3+2)+4+(3+2));
53 free(cmd);
54
55 const char *argv[3];
56 argv[0] = "SET";
57 argv[1] = "foo";
58 argv[2] = "bar";
59 size_t lens[3] = { 3, 3, 3 };
60 int argc = 3;
61
62 test("Format command by passing argc/argv without lengths: ");
63 len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
64 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
65 len == 4+4+(3+2)+4+(3+2)+4+(3+2));
66 free(cmd);
67
68 test("Format command by passing argc/argv with lengths: ");
69 len = redisFormatCommandArgv(&cmd,argc,argv,lens);
70 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
71 len == 4+4+(3+2)+4+(3+2)+4+(3+2));
72 free(cmd);
73 }
74
75 static void test_blocking_connection() {
76 redisContext *c;
77 redisReply *reply;
78
79 __connect(&c);
80 test("Returns I/O error when the connection is lost: ");
81 reply = redisCommand(c,"QUIT");
82 test_cond(strcasecmp(reply->str,"OK") == 0 && redisCommand(c,"PING") == NULL);
83
84 /* Two conditions may happen, depending on the type of connection.
85 * When connected via TCP, the socket will not yet be aware of the closed
86 * connection and the write(2) call will succeed, but the read(2) will
87 * result in an EOF. When connected via Unix sockets, the socket will be
88 * immediately aware that it was closed and fail on the write(2) call. */
89 if (use_unix) {
90 fprintf(stderr,"Error: %s\n", c->errstr);
91 assert(c->err == REDIS_ERR_IO &&
92 strcmp(c->errstr,"Broken pipe") == 0);
93 } else {
94 fprintf(stderr,"Error: %s\n", c->errstr);
95 assert(c->err == REDIS_ERR_EOF &&
96 strcmp(c->errstr,"Server closed the connection") == 0);
97 }
98 freeReplyObject(reply);
99 redisFree(c);
100
101 __connect(&c); /* reconnect */
102 test("Is able to deliver commands: ");
103 reply = redisCommand(c,"PING");
104 test_cond(reply->type == REDIS_REPLY_STATUS &&
105 strcasecmp(reply->str,"pong") == 0)
106 freeReplyObject(reply);
107
108 /* Switch to DB 9 for testing, now that we know we can chat. */
109 reply = redisCommand(c,"SELECT 9");
110 freeReplyObject(reply);
111
112 /* Make sure the DB is emtpy */
113 reply = redisCommand(c,"DBSIZE");
114 if (reply->type != REDIS_REPLY_INTEGER ||
115 reply->integer != 0) {
116 printf("Sorry DB 9 is not empty, test can not continue\n");
117 exit(1);
118 } else {
119 printf("DB 9 is empty... test can continue\n");
120 }
121 freeReplyObject(reply);
122
123 test("Is a able to send commands verbatim: ");
124 reply = redisCommand(c,"SET foo bar");
125 test_cond (reply->type == REDIS_REPLY_STATUS &&
126 strcasecmp(reply->str,"ok") == 0)
127 freeReplyObject(reply);
128
129 test("%%s String interpolation works: ");
130 reply = redisCommand(c,"SET %s %s","foo","hello world");
131 freeReplyObject(reply);
132 reply = redisCommand(c,"GET foo");
133 test_cond(reply->type == REDIS_REPLY_STRING &&
134 strcmp(reply->str,"hello world") == 0);
135 freeReplyObject(reply);
136
137 test("%%b String interpolation works: ");
138 reply = redisCommand(c,"SET %b %b","foo",3,"hello\x00world",11);
139 freeReplyObject(reply);
140 reply = redisCommand(c,"GET foo");
141 test_cond(reply->type == REDIS_REPLY_STRING &&
142 memcmp(reply->str,"hello\x00world",11) == 0)
143
144 test("Binary reply length is correct: ");
145 test_cond(reply->len == 11)
146 freeReplyObject(reply);
147
148 test("Can parse nil replies: ");
149 reply = redisCommand(c,"GET nokey");
150 test_cond(reply->type == REDIS_REPLY_NIL)
151 freeReplyObject(reply);
152
153 /* test 7 */
154 test("Can parse integer replies: ");
155 reply = redisCommand(c,"INCR mycounter");
156 test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
157 freeReplyObject(reply);
158
159 test("Can parse multi bulk replies: ");
160 freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
161 freeReplyObject(redisCommand(c,"LPUSH mylist bar"));
162 reply = redisCommand(c,"LRANGE mylist 0 -1");
163 test_cond(reply->type == REDIS_REPLY_ARRAY &&
164 reply->elements == 2 &&
165 !memcmp(reply->element[0]->str,"bar",3) &&
166 !memcmp(reply->element[1]->str,"foo",3))
167 freeReplyObject(reply);
168
169 /* m/e with multi bulk reply *before* other reply.
170 * specifically test ordering of reply items to parse. */
171 test("Can handle nested multi bulk replies: ");
172 freeReplyObject(redisCommand(c,"MULTI"));
173 freeReplyObject(redisCommand(c,"LRANGE mylist 0 -1"));
174 freeReplyObject(redisCommand(c,"PING"));
175 reply = (redisCommand(c,"EXEC"));
176 test_cond(reply->type == REDIS_REPLY_ARRAY &&
177 reply->elements == 2 &&
178 reply->element[0]->type == REDIS_REPLY_ARRAY &&
179 reply->element[0]->elements == 2 &&
180 !memcmp(reply->element[0]->element[0]->str,"bar",3) &&
181 !memcmp(reply->element[0]->element[1]->str,"foo",3) &&
182 reply->element[1]->type == REDIS_REPLY_STATUS &&
183 strcasecmp(reply->element[1]->str,"pong") == 0);
184 freeReplyObject(reply);
185 }
186
187 static void test_reply_reader() {
188 void *reader;
189 void *reply;
190 char *err;
191 int ret;
192
193 test("Error handling in reply parser: ");
194 reader = redisReplyReaderCreate();
195 redisReplyReaderFeed(reader,(char*)"@foo\r\n",6);
196 ret = redisReplyReaderGetReply(reader,NULL);
197 err = redisReplyReaderGetError(reader);
198 test_cond(ret == REDIS_ERR &&
199 strcasecmp(err,"protocol error, got \"@\" as reply type byte") == 0);
200 redisReplyReaderFree(reader);
201
202 /* when the reply already contains multiple items, they must be free'd
203 * on an error. valgrind will bark when this doesn't happen. */
204 test("Memory cleanup in reply parser: ");
205 reader = redisReplyReaderCreate();
206 redisReplyReaderFeed(reader,(char*)"*2\r\n",4);
207 redisReplyReaderFeed(reader,(char*)"$5\r\nhello\r\n",11);
208 redisReplyReaderFeed(reader,(char*)"@foo\r\n",6);
209 ret = redisReplyReaderGetReply(reader,NULL);
210 err = redisReplyReaderGetError(reader);
211 test_cond(ret == REDIS_ERR &&
212 strcasecmp(err,"protocol error, got \"@\" as reply type byte") == 0);
213 redisReplyReaderFree(reader);
214
215 test("Works with NULL functions for reply: ");
216 reader = redisReplyReaderCreate();
217 redisReplyReaderSetReplyObjectFunctions(reader,NULL);
218 redisReplyReaderFeed(reader,(char*)"+OK\r\n",5);
219 ret = redisReplyReaderGetReply(reader,&reply);
220 test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
221 redisReplyReaderFree(reader);
222 }
223
224 static void test_throughput() {
225 int i;
226 long long t1, t2;
227 redisContext *c = blocking_context;
228 redisReply **replies;
229
230 test("Throughput:\n");
231 for (i = 0; i < 500; i++)
232 freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
233
234 replies = malloc(sizeof(redisReply*)*1000);
235 t1 = usec();
236 for (i = 0; i < 1000; i++) {
237 replies[i] = redisCommand(c,"PING");
238 assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
239 }
240 t2 = usec();
241 for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
242 free(replies);
243 printf("\t(1000x PING: %.2fs)\n", (t2-t1)/1000000.0);
244
245 replies = malloc(sizeof(redisReply*)*1000);
246 t1 = usec();
247 for (i = 0; i < 1000; i++) {
248 replies[i] = redisCommand(c,"LRANGE mylist 0 499");
249 assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
250 assert(replies[i] != NULL && replies[i]->elements == 500);
251 }
252 t2 = usec();
253 for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
254 free(replies);
255 printf("\t(1000x LRANGE with 500 elements: %.2fs)\n", (t2-t1)/1000000.0);
256 }
257
258 static void cleanup() {
259 redisContext *c = blocking_context;
260 redisReply *reply;
261
262 /* Make sure we're on DB 9 */
263 reply = redisCommand(c,"SELECT 9");
264 assert(reply != NULL); freeReplyObject(reply);
265 reply = redisCommand(c,"FLUSHDB");
266 assert(reply != NULL); freeReplyObject(reply);
267 redisFree(c);
268 }
269
270 // static long __test_callback_flags = 0;
271 // static void __test_callback(redisContext *c, void *privdata) {
272 // ((void)c);
273 // /* Shift to detect execution order */
274 // __test_callback_flags <<= 8;
275 // __test_callback_flags |= (long)privdata;
276 // }
277 //
278 // static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {
279 // ((void)c);
280 // /* Shift to detect execution order */
281 // __test_callback_flags <<= 8;
282 // __test_callback_flags |= (long)privdata;
283 // if (reply) freeReplyObject(reply);
284 // }
285 //
286 // static redisContext *__connect_nonblock() {
287 // /* Reset callback flags */
288 // __test_callback_flags = 0;
289 // return redisConnectNonBlock("127.0.0.1", 6379, NULL);
290 // }
291 //
292 // static void test_nonblocking_connection() {
293 // redisContext *c;
294 // int wdone = 0;
295 //
296 // test("Calls command callback when command is issued: ");
297 // c = __connect_nonblock();
298 // redisSetCommandCallback(c,__test_callback,(void*)1);
299 // redisCommand(c,"PING");
300 // test_cond(__test_callback_flags == 1);
301 // redisFree(c);
302 //
303 // test("Calls disconnect callback on redisDisconnect: ");
304 // c = __connect_nonblock();
305 // redisSetDisconnectCallback(c,__test_callback,(void*)2);
306 // redisDisconnect(c);
307 // test_cond(__test_callback_flags == 2);
308 // redisFree(c);
309 //
310 // test("Calls disconnect callback and free callback on redisFree: ");
311 // c = __connect_nonblock();
312 // redisSetDisconnectCallback(c,__test_callback,(void*)2);
313 // redisSetFreeCallback(c,__test_callback,(void*)4);
314 // redisFree(c);
315 // test_cond(__test_callback_flags == ((2 << 8) | 4));
316 //
317 // test("redisBufferWrite against empty write buffer: ");
318 // c = __connect_nonblock();
319 // test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
320 // redisFree(c);
321 //
322 // test("redisBufferWrite against not yet connected fd: ");
323 // c = __connect_nonblock();
324 // redisCommand(c,"PING");
325 // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
326 // strncmp(c->error,"write:",6) == 0);
327 // redisFree(c);
328 //
329 // test("redisBufferWrite against closed fd: ");
330 // c = __connect_nonblock();
331 // redisCommand(c,"PING");
332 // redisDisconnect(c);
333 // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
334 // strncmp(c->error,"write:",6) == 0);
335 // redisFree(c);
336 //
337 // test("Process callbacks in the right sequence: ");
338 // c = __connect_nonblock();
339 // redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING");
340 // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
341 // redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING");
342 //
343 // /* Write output buffer */
344 // wdone = 0;
345 // while(!wdone) {
346 // usleep(500);
347 // redisBufferWrite(c,&wdone);
348 // }
349 //
350 // /* Read until at least one callback is executed (the 3 replies will
351 // * arrive in a single packet, causing all callbacks to be executed in
352 // * a single pass). */
353 // while(__test_callback_flags == 0) {
354 // assert(redisBufferRead(c) == REDIS_OK);
355 // redisProcessCallbacks(c);
356 // }
357 // test_cond(__test_callback_flags == 0x010203);
358 // redisFree(c);
359 //
360 // test("redisDisconnect executes pending callbacks with NULL reply: ");
361 // c = __connect_nonblock();
362 // redisSetDisconnectCallback(c,__test_callback,(void*)1);
363 // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
364 // redisDisconnect(c);
365 // test_cond(__test_callback_flags == 0x0201);
366 // redisFree(c);
367 // }
368
369 int main(int argc, char **argv) {
370 if (argc > 1) {
371 if (strcmp(argv[1],"-s") == 0)
372 use_unix = 1;
373 }
374
375 signal(SIGPIPE, SIG_IGN);
376 test_format_commands();
377 test_blocking_connection();
378 test_reply_reader();
379 // test_nonblocking_connection();
380 test_throughput();
381 cleanup();
382
383 if (fails == 0) {
384 printf("ALL TESTS PASSED\n");
385 } else {
386 printf("*** %d TESTS FAILED ***\n", fails);
387 }
388 return 0;
389 }