]>
git.saurik.com Git - redis.git/blob - deps/hiredis/test.c
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++;}
16 static long long usec(void) {
18 gettimeofday(&tv
,NULL
);
19 return (((long long)tv
.tv_sec
)*1000000)+tv
.tv_usec
;
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
);
33 static void test_format_commands() {
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));
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));
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));
59 size_t lens
[3] = { 3, 3, 3 };
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));
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));
75 static void test_blocking_connection() {
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
);
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. */
90 fprintf(stderr
,"Error: %s\n", c
->errstr
);
91 assert(c
->err
== REDIS_ERR_IO
&&
92 strcmp(c
->errstr
,"Broken pipe") == 0);
94 fprintf(stderr
,"Error: %s\n", c
->errstr
);
95 assert(c
->err
== REDIS_ERR_EOF
&&
96 strcmp(c
->errstr
,"Server closed the connection") == 0);
98 freeReplyObject(reply
);
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
);
108 /* Switch to DB 9 for testing, now that we know we can chat. */
109 reply
= redisCommand(c
,"SELECT 9");
110 freeReplyObject(reply
);
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");
119 printf("DB 9 is empty... test can continue\n");
121 freeReplyObject(reply
);
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
);
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
);
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)
144 test("Binary reply length is correct: ");
145 test_cond(reply
->len
== 11)
146 freeReplyObject(reply
);
148 test("Can parse nil replies: ");
149 reply
= redisCommand(c
,"GET nokey");
150 test_cond(reply
->type
== REDIS_REPLY_NIL
)
151 freeReplyObject(reply
);
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
);
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
);
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
);
187 static void test_reply_reader() {
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
);
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
);
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
);
224 static void test_throughput() {
227 redisContext
*c
= blocking_context
;
228 redisReply
**replies
;
230 test("Throughput:\n");
231 for (i
= 0; i
< 500; i
++)
232 freeReplyObject(redisCommand(c
,"LPUSH mylist foo"));
234 replies
= malloc(sizeof(redisReply
*)*1000);
236 for (i
= 0; i
< 1000; i
++) {
237 replies
[i
] = redisCommand(c
,"PING");
238 assert(replies
[i
] != NULL
&& replies
[i
]->type
== REDIS_REPLY_STATUS
);
241 for (i
= 0; i
< 1000; i
++) freeReplyObject(replies
[i
]);
243 printf("\t(1000x PING: %.2fs)\n", (t2
-t1
)/1000000.0);
245 replies
= malloc(sizeof(redisReply
*)*1000);
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);
253 for (i
= 0; i
< 1000; i
++) freeReplyObject(replies
[i
]);
255 printf("\t(1000x LRANGE with 500 elements: %.2fs)\n", (t2
-t1
)/1000000.0);
258 static void cleanup() {
259 redisContext
*c
= blocking_context
;
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
);
270 // static long __test_callback_flags = 0;
271 // static void __test_callback(redisContext *c, void *privdata) {
273 // /* Shift to detect execution order */
274 // __test_callback_flags <<= 8;
275 // __test_callback_flags |= (long)privdata;
278 // static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {
280 // /* Shift to detect execution order */
281 // __test_callback_flags <<= 8;
282 // __test_callback_flags |= (long)privdata;
283 // if (reply) freeReplyObject(reply);
286 // static redisContext *__connect_nonblock() {
287 // /* Reset callback flags */
288 // __test_callback_flags = 0;
289 // return redisConnectNonBlock("127.0.0.1", 6379, NULL);
292 // static void test_nonblocking_connection() {
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);
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);
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);
315 // test_cond(__test_callback_flags == ((2 << 8) | 4));
317 // test("redisBufferWrite against empty write buffer: ");
318 // c = __connect_nonblock();
319 // test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
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);
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);
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");
343 // /* Write output buffer */
347 // redisBufferWrite(c,&wdone);
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);
357 // test_cond(__test_callback_flags == 0x010203);
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);
369 int main(int argc
, char **argv
) {
371 if (strcmp(argv
[1],"-s") == 0)
375 signal(SIGPIPE
, SIG_IGN
);
376 test_format_commands();
377 test_blocking_connection();
379 // test_nonblocking_connection();
384 printf("ALL TESTS PASSED\n");
386 printf("*** %d TESTS FAILED ***\n", fails
);