hiredis/test.c

381 lines
13 KiB
C
Raw Normal View History

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-10-18 12:39:43 +00:00
#include <assert.h>
#include <unistd.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" :) */
2010-10-18 12:39:43 +00:00
static int tests = 0, fails = 0;
2010-09-20 11:44:24 +00:00
#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;
}
static int use_unix = 0;
2010-10-18 12:39:43 +00:00
static redisContext *blocking_context = NULL;
static void __connect(redisContext **target) {
*target = blocking_context = (use_unix ?
redisConnectUnix("/tmp/redis.sock") : redisConnect((char*)"127.0.0.1", 6379));
if (blocking_context->err) {
printf("Connection error: %s\n", blocking_context->errstr);
2010-09-19 17:01:31 +00:00
exit(1);
}
}
static void test_format_commands() {
char *cmd;
int len;
test("Format command without interpolation: ");
len = redisFormatCommand(&cmd,"SET foo bar");
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
len == 4+4+(3+2)+4+(3+2)+4+(3+2));
free(cmd);
test("Format command with %%s string interpolation: ");
len = redisFormatCommand(&cmd,"SET %s %s","foo","bar");
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
len == 4+4+(3+2)+4+(3+2)+4+(3+2));
free(cmd);
test("Format command with %%b string interpolation: ");
len = redisFormatCommand(&cmd,"SET %b %b","foo",3,"b\0r",3);
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 &&
len == 4+4+(3+2)+4+(3+2)+4+(3+2));
free(cmd);
const char *argv[3];
argv[0] = "SET";
argv[1] = "foo";
argv[2] = "bar";
size_t lens[3] = { 3, 3, 3 };
int argc = 3;
test("Format command by passing argc/argv without lengths: ");
len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
len == 4+4+(3+2)+4+(3+2)+4+(3+2));
free(cmd);
test("Format command by passing argc/argv with lengths: ");
len = redisFormatCommandArgv(&cmd,argc,argv,lens);
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
len == 4+4+(3+2)+4+(3+2)+4+(3+2));
free(cmd);
}
2010-10-18 12:39:43 +00:00
static void test_blocking_connection() {
2010-09-24 16:48:07 +00:00
redisContext *c;
2010-10-18 12:39:43 +00:00
redisReply *reply;
2010-05-18 18:12:03 +00:00
__connect(&c);
2010-09-20 11:44:24 +00:00
test("Returns I/O error when the connection is lost: ");
2010-10-18 10:01:17 +00:00
reply = redisCommand(c,"QUIT");
test_cond(strcasecmp(reply->str,"OK") == 0 && redisCommand(c,"PING") == NULL);
/* Two conditions may happen, depending on the type of connection.
* When connected via TCP, the socket will not yet be aware of the closed
* connection and the write(2) call will succeed, but the read(2) will
* result in an EOF. When connected via Unix sockets, the socket will be
* immediately aware that it was closed and fail on the write(2) call. */
if (use_unix) {
fprintf(stderr,"Error: %s\n", c->errstr);
assert(c->err == REDIS_ERR_IO &&
strcmp(c->errstr,"Broken pipe") == 0);
} else {
fprintf(stderr,"Error: %s\n", c->errstr);
assert(c->err == REDIS_ERR_EOF &&
strcmp(c->errstr,"Server closed the connection") == 0);
}
2010-10-18 10:01:17 +00:00
freeReplyObject(reply);
redisFree(c);
2010-05-18 18:12:03 +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");
test_cond(reply->type == REDIS_REPLY_STATUS &&
strcasecmp(reply->str,"pong") == 0)
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");
test_cond (reply->type == REDIS_REPLY_STATUS &&
strcasecmp(reply->str,"ok") == 0)
2010-05-18 18:12:03 +00:00
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->str,"hello world") == 0);
2010-05-18 18:12:03 +00:00
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->str,"hello\x00world",11) == 0)
2010-05-18 18:12:03 +00:00
2010-09-20 11:44:24 +00:00
test("Binary reply length is correct: ");
test_cond(reply->len == 11)
2010-05-18 18:12:03 +00:00
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]->str,"bar",3) &&
!memcmp(reply->element[1]->str,"foo",3))
2010-05-18 18:12:03 +00:00
freeReplyObject(reply);
2010-09-20 11:44:24 +00:00
/* m/e with multi bulk reply *before* other reply.
* 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"));
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]->str,"bar",3) &&
!memcmp(reply->element[0]->element[1]->str,"foo",3) &&
reply->element[1]->type == REDIS_REPLY_STATUS &&
strcasecmp(reply->element[1]->str,"pong") == 0);
freeReplyObject(reply);
2010-10-18 12:39:43 +00:00
}
static void test_reply_reader() {
void *reader;
char *err;
int ret;
test("Error handling in reply parser: ");
reader = redisReplyReaderCreate(NULL);
redisReplyReaderFeed(reader,(char*)"@foo\r\n",6);
2010-10-18 12:39:43 +00:00
ret = redisReplyReaderGetReply(reader,NULL);
err = redisReplyReaderGetError(reader);
test_cond(ret == REDIS_ERR &&
strcasecmp(err,"protocol error, got \"@\" as reply type byte") == 0);
redisReplyReaderFree(reader);
/* 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: ");
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-10-18 12:39:43 +00:00
ret = redisReplyReaderGetReply(reader,NULL);
err = redisReplyReaderGetError(reader);
test_cond(ret == REDIS_ERR &&
strcasecmp(err,"protocol error, got \"@\" as reply type byte") == 0);
redisReplyReaderFree(reader);
2010-10-18 12:39:43 +00:00
}
static void test_throughput() {
int i;
long long t1, t2;
redisContext *c = blocking_context;
redisReply **replies;
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
replies = malloc(sizeof(redisReply*)*1000);
2010-09-19 13:18:51 +00:00
t1 = usec();
2010-11-03 10:43:01 +00:00
for (i = 0; i < 1000; i++) {
replies[i] = redisCommand(c,"PING");
assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
}
2010-09-19 13:18:51 +00:00
t2 = usec();
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
replies = malloc(sizeof(redisReply*)*1000);
2010-09-19 13:18:51 +00:00
t1 = usec();
2010-11-03 10:43:01 +00:00
for (i = 0; i < 1000; i++) {
replies[i] = redisCommand(c,"LRANGE mylist 0 499");
assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
assert(replies[i] != NULL && replies[i]->elements == 500);
}
2010-09-19 13:18:51 +00:00
t2 = usec();
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-10-18 12:39:43 +00:00
}
static void cleanup() {
redisContext *c = blocking_context;
redisReply *reply;
2010-09-19 13:18:51 +00:00
2010-10-18 12:39:43 +00:00
/* Make sure we're on DB 9 */
reply = redisCommand(c,"SELECT 9");
assert(reply != NULL); freeReplyObject(reply);
2010-09-24 16:48:07 +00:00
reply = redisCommand(c,"FLUSHDB");
2010-10-18 12:39:43 +00:00
assert(reply != NULL); freeReplyObject(reply);
redisFree(c);
}
// static long __test_callback_flags = 0;
// static void __test_callback(redisContext *c, void *privdata) {
// ((void)c);
// /* Shift to detect execution order */
// __test_callback_flags <<= 8;
// __test_callback_flags |= (long)privdata;
// }
//
// static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {
// ((void)c);
// /* Shift to detect execution order */
// __test_callback_flags <<= 8;
// __test_callback_flags |= (long)privdata;
// if (reply) freeReplyObject(reply);
// }
//
// static redisContext *__connect_nonblock() {
// /* Reset callback flags */
// __test_callback_flags = 0;
// return redisConnectNonBlock("127.0.0.1", 6379, NULL);
// }
//
// static void test_nonblocking_connection() {
// redisContext *c;
// int wdone = 0;
//
// test("Calls command callback when command is issued: ");
// c = __connect_nonblock();
// redisSetCommandCallback(c,__test_callback,(void*)1);
// redisCommand(c,"PING");
// test_cond(__test_callback_flags == 1);
// redisFree(c);
//
// test("Calls disconnect callback on redisDisconnect: ");
// c = __connect_nonblock();
// redisSetDisconnectCallback(c,__test_callback,(void*)2);
// redisDisconnect(c);
// test_cond(__test_callback_flags == 2);
// redisFree(c);
//
// test("Calls disconnect callback and free callback on redisFree: ");
// c = __connect_nonblock();
// redisSetDisconnectCallback(c,__test_callback,(void*)2);
// redisSetFreeCallback(c,__test_callback,(void*)4);
// redisFree(c);
// test_cond(__test_callback_flags == ((2 << 8) | 4));
//
// test("redisBufferWrite against empty write buffer: ");
// c = __connect_nonblock();
// test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
// redisFree(c);
//
// test("redisBufferWrite against not yet connected fd: ");
// c = __connect_nonblock();
// redisCommand(c,"PING");
// test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
// strncmp(c->error,"write:",6) == 0);
// redisFree(c);
//
// test("redisBufferWrite against closed fd: ");
// c = __connect_nonblock();
// redisCommand(c,"PING");
// redisDisconnect(c);
// test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
// strncmp(c->error,"write:",6) == 0);
// redisFree(c);
//
// test("Process callbacks in the right sequence: ");
// c = __connect_nonblock();
// redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING");
// redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
// redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING");
//
// /* Write output buffer */
// wdone = 0;
// while(!wdone) {
// usleep(500);
// redisBufferWrite(c,&wdone);
// }
//
// /* Read until at least one callback is executed (the 3 replies will
// * arrive in a single packet, causing all callbacks to be executed in
// * a single pass). */
// while(__test_callback_flags == 0) {
// assert(redisBufferRead(c) == REDIS_OK);
// redisProcessCallbacks(c);
// }
// test_cond(__test_callback_flags == 0x010203);
// redisFree(c);
//
// test("redisDisconnect executes pending callbacks with NULL reply: ");
// c = __connect_nonblock();
// redisSetDisconnectCallback(c,__test_callback,(void*)1);
// redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
// redisDisconnect(c);
// test_cond(__test_callback_flags == 0x0201);
// redisFree(c);
// }
2010-11-03 10:43:01 +00:00
int main(int argc, char **argv) {
if (argc > 1) {
if (strcmp(argv[1],"-s") == 0)
use_unix = 1;
}
signal(SIGPIPE, SIG_IGN);
test_format_commands();
2010-10-18 12:39:43 +00:00
test_blocking_connection();
test_reply_reader();
// test_nonblocking_connection();
2010-10-18 12:39:43 +00:00
test_throughput();
cleanup();
2010-09-19 13:18:51 +00:00
2010-09-20 11:44:24 +00:00
if (fails == 0) {
printf("ALL TESTS PASSED\n");
} else {
printf("*** %d TESTS FAILED ***\n", fails);
}
2010-05-18 18:12:03 +00:00
return 0;
}