2010-05-18 18:12:03 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-05-25 12:00:41 +00:00
|
|
|
#include <strings.h>
|
2010-09-19 13:18:51 +00:00
|
|
|
#include <sys/time.h>
|
2010-05-18 18:12:03 +00:00
|
|
|
|
|
|
|
#include "hiredis.h"
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
/* The following lines make up our testing "framework" :) */
|
|
|
|
#define test(_s) { printf("#%02d ", ++tests); printf(_s); }
|
2010-05-18 18:12:03 +00:00
|
|
|
#define test_cond(_c) if(_c) printf("PASSED\n"); else {printf("FAILED\n"); fails++;}
|
|
|
|
|
2010-09-19 18:43:43 +00:00
|
|
|
static long long usec(void) {
|
2010-09-19 13:18:51 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
2010-10-06 19:10:09 +00:00
|
|
|
static void __connect(redisContext **target) {
|
|
|
|
redisContext *c;
|
|
|
|
c = *target = redisConnect((char*)"127.0.0.1", 6379, NULL);
|
|
|
|
if (c->error != NULL) {
|
2010-10-06 19:23:03 +00:00
|
|
|
printf("Connection error: %s\n", c->error);
|
2010-09-19 17:01:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-18 18:12:03 +00:00
|
|
|
int main(void) {
|
2010-09-25 13:09:13 +00:00
|
|
|
int i, ret, tests = 0, fails = 0;
|
2010-09-19 13:18:51 +00:00
|
|
|
long long t1, t2;
|
2010-09-24 16:48:07 +00:00
|
|
|
redisContext *c;
|
2010-09-25 13:33:27 +00:00
|
|
|
redisReply *reply, **replies;
|
2010-09-20 12:05:23 +00:00
|
|
|
void *reader;
|
2010-09-25 13:09:13 +00:00
|
|
|
char *err;
|
2010-05-18 18:12:03 +00:00
|
|
|
|
2010-09-25 13:33:27 +00:00
|
|
|
__connect(&c);
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Returns I/O error when the connection is lost: ");
|
2010-09-25 13:09:13 +00:00
|
|
|
test_cond(redisCommand(c,"QUIT") == NULL &&
|
|
|
|
strcmp(c->error,"Server closed the connection") == 0);
|
2010-09-25 13:33:27 +00:00
|
|
|
redisFree(c);
|
2010-05-18 18:12:03 +00:00
|
|
|
|
2010-09-25 13:33:27 +00:00
|
|
|
__connect(&c); /* reconnect */
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Is able to deliver commands: ");
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"PING");
|
2010-05-18 18:12:03 +00:00
|
|
|
test_cond(reply->type == REDIS_REPLY_STRING &&
|
|
|
|
strcasecmp(reply->reply,"pong") == 0)
|
2010-09-19 18:43:57 +00:00
|
|
|
freeReplyObject(reply);
|
2010-05-18 18:12:03 +00:00
|
|
|
|
|
|
|
/* Switch to DB 9 for testing, now that we know we can chat. */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"SELECT 9");
|
2010-05-18 18:12:03 +00:00
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
/* Make sure the DB is emtpy */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"DBSIZE");
|
2010-05-18 18:12:03 +00:00
|
|
|
if (reply->type != REDIS_REPLY_INTEGER ||
|
|
|
|
reply->integer != 0) {
|
|
|
|
printf("Sorry DB 9 is not empty, test can not continue\n");
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("DB 9 is empty... test can continue\n");
|
|
|
|
}
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Is a able to send commands verbatim: ");
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"SET foo bar");
|
2010-05-18 18:12:03 +00:00
|
|
|
test_cond (reply->type == REDIS_REPLY_STRING &&
|
|
|
|
strcasecmp(reply->reply,"ok") == 0)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
test("%%s String interpolation works: ");
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"SET %s %s","foo","hello world");
|
2010-05-18 18:12:03 +00:00
|
|
|
freeReplyObject(reply);
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"GET foo");
|
2010-05-18 18:12:03 +00:00
|
|
|
test_cond(reply->type == REDIS_REPLY_STRING &&
|
|
|
|
strcmp(reply->reply,"hello world") == 0);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
test("%%b String interpolation works: ");
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"SET %b %b","foo",3,"hello\x00world",11);
|
2010-05-18 18:12:03 +00:00
|
|
|
freeReplyObject(reply);
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"GET foo");
|
2010-05-18 18:12:03 +00:00
|
|
|
test_cond(reply->type == REDIS_REPLY_STRING &&
|
|
|
|
memcmp(reply->reply,"hello\x00world",11) == 0)
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Binary reply length is correct: ");
|
2010-05-18 18:12:03 +00:00
|
|
|
test_cond(sdslen(reply->reply) == 11)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Can parse nil replies: ");
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"GET nokey");
|
2010-05-18 18:12:03 +00:00
|
|
|
test_cond(reply->type == REDIS_REPLY_NIL)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
/* test 7 */
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Can parse integer replies: ");
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"INCR mycounter");
|
2010-05-18 18:12:03 +00:00
|
|
|
test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Can parse multi bulk replies: ");
|
2010-09-24 16:48:07 +00:00
|
|
|
freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
|
|
|
|
freeReplyObject(redisCommand(c,"LPUSH mylist bar"));
|
|
|
|
reply = redisCommand(c,"LRANGE mylist 0 -1");
|
2010-05-18 18:12:03 +00:00
|
|
|
test_cond(reply->type == REDIS_REPLY_ARRAY &&
|
|
|
|
reply->elements == 2 &&
|
|
|
|
!memcmp(reply->element[0]->reply,"bar",3) &&
|
|
|
|
!memcmp(reply->element[1]->reply,"foo",3))
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
/* m/e with multi bulk reply *before* other reply.
|
2010-09-19 16:47:05 +00:00
|
|
|
* specifically test ordering of reply items to parse. */
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Can handle nested multi bulk replies: ");
|
2010-09-24 16:48:07 +00:00
|
|
|
freeReplyObject(redisCommand(c,"MULTI"));
|
|
|
|
freeReplyObject(redisCommand(c,"LRANGE mylist 0 -1"));
|
|
|
|
freeReplyObject(redisCommand(c,"PING"));
|
|
|
|
reply = (redisCommand(c,"EXEC"));
|
2010-09-19 16:47:05 +00:00
|
|
|
test_cond(reply->type == REDIS_REPLY_ARRAY &&
|
|
|
|
reply->elements == 2 &&
|
|
|
|
reply->element[0]->type == REDIS_REPLY_ARRAY &&
|
|
|
|
reply->element[0]->elements == 2 &&
|
|
|
|
!memcmp(reply->element[0]->element[0]->reply,"bar",3) &&
|
|
|
|
!memcmp(reply->element[0]->element[1]->reply,"foo",3) &&
|
|
|
|
reply->element[1]->type == REDIS_REPLY_STRING &&
|
|
|
|
strcasecmp(reply->element[1]->reply,"pong") == 0);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-20 12:05:23 +00:00
|
|
|
test("Error handling in reply parser: ");
|
2010-09-21 09:39:18 +00:00
|
|
|
reader = redisReplyReaderCreate(NULL);
|
|
|
|
redisReplyReaderFeed(reader,(char*)"@foo\r\n",6);
|
2010-09-25 13:09:13 +00:00
|
|
|
ret = redisReplyReaderGetReply(reader,(void*)&reply);
|
|
|
|
err = redisReplyReaderGetError(reader);
|
|
|
|
test_cond(ret == REDIS_ERR &&
|
|
|
|
strcasecmp(err,"protocol error, got \"@\" as reply type byte") == 0);
|
2010-09-21 09:39:18 +00:00
|
|
|
redisReplyReaderFree(reader);
|
2010-09-20 12:05:23 +00:00
|
|
|
|
|
|
|
/* when the reply already contains multiple items, they must be free'd
|
|
|
|
* on an error. valgrind will bark when this doesn't happen. */
|
|
|
|
test("Memory cleanup in reply parser: ");
|
2010-09-21 09:39:18 +00:00
|
|
|
reader = redisReplyReaderCreate(NULL);
|
|
|
|
redisReplyReaderFeed(reader,(char*)"*2\r\n",4);
|
|
|
|
redisReplyReaderFeed(reader,(char*)"$5\r\nhello\r\n",11);
|
|
|
|
redisReplyReaderFeed(reader,(char*)"@foo\r\n",6);
|
2010-09-25 13:09:13 +00:00
|
|
|
ret = redisReplyReaderGetReply(reader,(void*)&reply);
|
|
|
|
err = redisReplyReaderGetError(reader);
|
|
|
|
test_cond(ret == REDIS_ERR &&
|
|
|
|
strcasecmp(err,"protocol error, got \"@\" as reply type byte") == 0);
|
2010-09-21 09:39:18 +00:00
|
|
|
redisReplyReaderFree(reader);
|
2010-09-20 12:05:23 +00:00
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
test("Throughput:\n");
|
2010-09-19 13:18:51 +00:00
|
|
|
for (i = 0; i < 500; i++)
|
2010-09-24 16:48:07 +00:00
|
|
|
freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
|
2010-09-19 13:18:51 +00:00
|
|
|
|
2010-09-25 13:33:46 +00:00
|
|
|
replies = malloc(sizeof(redisReply*)*1000);
|
2010-09-19 13:18:51 +00:00
|
|
|
t1 = usec();
|
2010-09-25 13:33:46 +00:00
|
|
|
for (i = 0; i < 1000; i++) replies[i] = redisCommand(c,"PING");
|
2010-09-19 13:18:51 +00:00
|
|
|
t2 = usec();
|
2010-09-25 13:33:46 +00:00
|
|
|
for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
|
|
|
|
free(replies);
|
2010-09-20 11:44:24 +00:00
|
|
|
printf("\t(1000x PING: %.2fs)\n", (t2-t1)/1000000.0);
|
2010-09-19 13:18:51 +00:00
|
|
|
|
2010-09-25 13:33:46 +00:00
|
|
|
replies = malloc(sizeof(redisReply*)*1000);
|
2010-09-19 13:18:51 +00:00
|
|
|
t1 = usec();
|
2010-09-25 13:33:46 +00:00
|
|
|
for (i = 0; i < 1000; i++) replies[i] = redisCommand(c,"LRANGE mylist 0 499");
|
2010-09-19 13:18:51 +00:00
|
|
|
t2 = usec();
|
2010-09-25 13:33:46 +00:00
|
|
|
for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
|
|
|
|
free(replies);
|
2010-09-20 11:44:24 +00:00
|
|
|
printf("\t(1000x LRANGE with 500 elements: %.2fs)\n", (t2-t1)/1000000.0);
|
2010-09-19 13:18:51 +00:00
|
|
|
|
|
|
|
/* Clean DB 9 */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"FLUSHDB");
|
2010-09-19 13:18:51 +00:00
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-20 11:44:24 +00:00
|
|
|
if (fails == 0) {
|
|
|
|
printf("ALL TESTS PASSED\n");
|
|
|
|
} else {
|
|
|
|
printf("*** %d TESTS FAILED ***\n", fails);
|
|
|
|
}
|
|
|
|
|
2010-09-25 13:33:27 +00:00
|
|
|
redisFree(c);
|
2010-05-18 18:12:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|