]> git.saurik.com Git - redis.git/blob - deps/hiredis/test.c
config option to select if when replication link with master a slave should or not...
[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 char *err;
190 int ret;
191
192 test("Error handling in reply parser: ");
193 reader = redisReplyReaderCreate(NULL);
194 redisReplyReaderFeed(reader,(char*)"@foo\r\n",6);
195 ret = redisReplyReaderGetReply(reader,NULL);
196 err = redisReplyReaderGetError(reader);
197 test_cond(ret == REDIS_ERR &&
198 strcasecmp(err,"protocol error, got \"@\" as reply type byte") == 0);
199 redisReplyReaderFree(reader);
200
201 /* when the reply already contains multiple items, they must be free'd
202 * on an error. valgrind will bark when this doesn't happen. */
203 test("Memory cleanup in reply parser: ");
204 reader = redisReplyReaderCreate(NULL);
205 redisReplyReaderFeed(reader,(char*)"*2\r\n",4);
206 redisReplyReaderFeed(reader,(char*)"$5\r\nhello\r\n",11);
207 redisReplyReaderFeed(reader,(char*)"@foo\r\n",6);
208 ret = redisReplyReaderGetReply(reader,NULL);
209 err = redisReplyReaderGetError(reader);
210 test_cond(ret == REDIS_ERR &&
211 strcasecmp(err,"protocol error, got \"@\" as reply type byte") == 0);
212 redisReplyReaderFree(reader);
213 }
214
215 static void test_throughput() {
216 int i;
217 long long t1, t2;
218 redisContext *c = blocking_context;
219 redisReply **replies;
220
221 test("Throughput:\n");
222 for (i = 0; i < 500; i++)
223 freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
224
225 replies = malloc(sizeof(redisReply*)*1000);
226 t1 = usec();
227 for (i = 0; i < 1000; i++) {
228 replies[i] = redisCommand(c,"PING");
229 assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
230 }
231 t2 = usec();
232 for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
233 free(replies);
234 printf("\t(1000x PING: %.2fs)\n", (t2-t1)/1000000.0);
235
236 replies = malloc(sizeof(redisReply*)*1000);
237 t1 = usec();
238 for (i = 0; i < 1000; i++) {
239 replies[i] = redisCommand(c,"LRANGE mylist 0 499");
240 assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
241 assert(replies[i] != NULL && replies[i]->elements == 500);
242 }
243 t2 = usec();
244 for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
245 free(replies);
246 printf("\t(1000x LRANGE with 500 elements: %.2fs)\n", (t2-t1)/1000000.0);
247 }
248
249 static void cleanup() {
250 redisContext *c = blocking_context;
251 redisReply *reply;
252
253 /* Make sure we're on DB 9 */
254 reply = redisCommand(c,"SELECT 9");
255 assert(reply != NULL); freeReplyObject(reply);
256 reply = redisCommand(c,"FLUSHDB");
257 assert(reply != NULL); freeReplyObject(reply);
258 redisFree(c);
259 }
260
261 // static long __test_callback_flags = 0;
262 // static void __test_callback(redisContext *c, void *privdata) {
263 // ((void)c);
264 // /* Shift to detect execution order */
265 // __test_callback_flags <<= 8;
266 // __test_callback_flags |= (long)privdata;
267 // }
268 //
269 // static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {
270 // ((void)c);
271 // /* Shift to detect execution order */
272 // __test_callback_flags <<= 8;
273 // __test_callback_flags |= (long)privdata;
274 // if (reply) freeReplyObject(reply);
275 // }
276 //
277 // static redisContext *__connect_nonblock() {
278 // /* Reset callback flags */
279 // __test_callback_flags = 0;
280 // return redisConnectNonBlock("127.0.0.1", 6379, NULL);
281 // }
282 //
283 // static void test_nonblocking_connection() {
284 // redisContext *c;
285 // int wdone = 0;
286 //
287 // test("Calls command callback when command is issued: ");
288 // c = __connect_nonblock();
289 // redisSetCommandCallback(c,__test_callback,(void*)1);
290 // redisCommand(c,"PING");
291 // test_cond(__test_callback_flags == 1);
292 // redisFree(c);
293 //
294 // test("Calls disconnect callback on redisDisconnect: ");
295 // c = __connect_nonblock();
296 // redisSetDisconnectCallback(c,__test_callback,(void*)2);
297 // redisDisconnect(c);
298 // test_cond(__test_callback_flags == 2);
299 // redisFree(c);
300 //
301 // test("Calls disconnect callback and free callback on redisFree: ");
302 // c = __connect_nonblock();
303 // redisSetDisconnectCallback(c,__test_callback,(void*)2);
304 // redisSetFreeCallback(c,__test_callback,(void*)4);
305 // redisFree(c);
306 // test_cond(__test_callback_flags == ((2 << 8) | 4));
307 //
308 // test("redisBufferWrite against empty write buffer: ");
309 // c = __connect_nonblock();
310 // test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
311 // redisFree(c);
312 //
313 // test("redisBufferWrite against not yet connected fd: ");
314 // c = __connect_nonblock();
315 // redisCommand(c,"PING");
316 // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
317 // strncmp(c->error,"write:",6) == 0);
318 // redisFree(c);
319 //
320 // test("redisBufferWrite against closed fd: ");
321 // c = __connect_nonblock();
322 // redisCommand(c,"PING");
323 // redisDisconnect(c);
324 // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
325 // strncmp(c->error,"write:",6) == 0);
326 // redisFree(c);
327 //
328 // test("Process callbacks in the right sequence: ");
329 // c = __connect_nonblock();
330 // redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING");
331 // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
332 // redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING");
333 //
334 // /* Write output buffer */
335 // wdone = 0;
336 // while(!wdone) {
337 // usleep(500);
338 // redisBufferWrite(c,&wdone);
339 // }
340 //
341 // /* Read until at least one callback is executed (the 3 replies will
342 // * arrive in a single packet, causing all callbacks to be executed in
343 // * a single pass). */
344 // while(__test_callback_flags == 0) {
345 // assert(redisBufferRead(c) == REDIS_OK);
346 // redisProcessCallbacks(c);
347 // }
348 // test_cond(__test_callback_flags == 0x010203);
349 // redisFree(c);
350 //
351 // test("redisDisconnect executes pending callbacks with NULL reply: ");
352 // c = __connect_nonblock();
353 // redisSetDisconnectCallback(c,__test_callback,(void*)1);
354 // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
355 // redisDisconnect(c);
356 // test_cond(__test_callback_flags == 0x0201);
357 // redisFree(c);
358 // }
359
360 int main(int argc, char **argv) {
361 if (argc > 1) {
362 if (strcmp(argv[1],"-s") == 0)
363 use_unix = 1;
364 }
365
366 signal(SIGPIPE, SIG_IGN);
367 test_format_commands();
368 test_blocking_connection();
369 test_reply_reader();
370 // test_nonblocking_connection();
371 test_throughput();
372 cleanup();
373
374 if (fails == 0) {
375 printf("ALL TESTS PASSED\n");
376 } else {
377 printf("*** %d TESTS FAILED ***\n", fails);
378 }
379 return 0;
380 }