2011-06-18 14:02:42 +00:00
|
|
|
#include "fmacros.h"
|
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>
|
2010-10-18 13:49:52 +00:00
|
|
|
#include <unistd.h>
|
2010-11-05 16:24:11 +00:00
|
|
|
#include <signal.h>
|
2011-01-07 12:04:42 +00:00
|
|
|
#include <errno.h>
|
2010-05-18 18:12:03 +00:00
|
|
|
|
|
|
|
#include "hiredis.h"
|
|
|
|
|
2011-05-22 14:25:52 +00:00
|
|
|
enum connection_type {
|
|
|
|
CONN_TCP,
|
|
|
|
CONN_UNIX
|
|
|
|
};
|
|
|
|
|
|
|
|
struct config {
|
|
|
|
enum connection_type type;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *host;
|
|
|
|
int port;
|
|
|
|
} tcp;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *path;
|
|
|
|
} unix;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-22 14:25:52 +00:00
|
|
|
static redisContext *select_database(redisContext *c) {
|
|
|
|
redisReply *reply;
|
|
|
|
|
|
|
|
/* Switch to DB 9 for testing, now that we know we can chat. */
|
|
|
|
reply = redisCommand(c,"SELECT 9");
|
|
|
|
assert(reply != NULL);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
/* Make sure the DB is emtpy */
|
|
|
|
reply = redisCommand(c,"DBSIZE");
|
|
|
|
assert(reply != NULL);
|
|
|
|
if (reply->type == REDIS_REPLY_INTEGER && reply->integer == 0) {
|
|
|
|
/* Awesome, DB 9 is empty and we can continue. */
|
|
|
|
freeReplyObject(reply);
|
|
|
|
} else {
|
|
|
|
printf("Database #9 is not empty, test can not continue\n");
|
2010-09-19 17:01:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-05-22 14:25:52 +00:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disconnect(redisContext *c) {
|
|
|
|
redisReply *reply;
|
|
|
|
|
|
|
|
/* Make sure we're on DB 9. */
|
|
|
|
reply = redisCommand(c,"SELECT 9");
|
|
|
|
assert(reply != NULL);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
reply = redisCommand(c,"FLUSHDB");
|
|
|
|
assert(reply != NULL);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
/* Free the context as well. */
|
|
|
|
redisFree(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static redisContext *connect(struct config config) {
|
2011-06-18 10:29:49 +00:00
|
|
|
redisContext *c = NULL;
|
2011-05-22 14:25:52 +00:00
|
|
|
|
|
|
|
if (config.type == CONN_TCP) {
|
|
|
|
c = redisConnect(config.tcp.host, config.tcp.port);
|
|
|
|
} else if (config.type == CONN_UNIX) {
|
|
|
|
c = redisConnectUnix(config.unix.path);
|
|
|
|
} else {
|
|
|
|
assert(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->err) {
|
|
|
|
printf("Connection error: %s\n", c->errstr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return select_database(c);
|
2010-09-19 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
2011-01-27 11:50:55 +00:00
|
|
|
static void test_format_commands(void) {
|
2010-10-31 09:32:31 +00:00
|
|
|
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);
|
|
|
|
|
2010-11-22 08:55:54 +00:00
|
|
|
test("Format command with %%s and an empty string: ");
|
|
|
|
len = redisFormatCommand(&cmd,"SET %s %s","foo","");
|
|
|
|
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
|
|
|
|
len == 4+4+(3+2)+4+(3+2)+4+(0+2));
|
|
|
|
free(cmd);
|
|
|
|
|
2011-03-06 10:38:07 +00:00
|
|
|
test("Format command with an empty string in between proper interpolations: ");
|
|
|
|
len = redisFormatCommand(&cmd,"SET %s %s","","foo");
|
|
|
|
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",len) == 0 &&
|
|
|
|
len == 4+4+(3+2)+4+(0+2)+4+(3+2));
|
|
|
|
free(cmd);
|
|
|
|
|
2010-10-31 09:32:31 +00:00
|
|
|
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);
|
|
|
|
|
2010-11-22 08:55:54 +00:00
|
|
|
test("Format command with %%b and an empty string: ");
|
|
|
|
len = redisFormatCommand(&cmd,"SET %b %b","foo",3,"",0);
|
|
|
|
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
|
|
|
|
len == 4+4+(3+2)+4+(3+2)+4+(0+2));
|
|
|
|
free(cmd);
|
|
|
|
|
2010-11-22 09:00:45 +00:00
|
|
|
test("Format command with literal %%: ");
|
|
|
|
len = redisFormatCommand(&cmd,"SET %% %%");
|
|
|
|
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$1\r\n%\r\n$1\r\n%\r\n",len) == 0 &&
|
|
|
|
len == 4+4+(3+2)+4+(1+2)+4+(1+2));
|
|
|
|
free(cmd);
|
|
|
|
|
2010-12-02 15:18:30 +00:00
|
|
|
test("Format command with printf-delegation (long long): ");
|
|
|
|
len = redisFormatCommand(&cmd,"key:%08lld",1234ll);
|
|
|
|
test_cond(strncmp(cmd,"*1\r\n$12\r\nkey:00001234\r\n",len) == 0 &&
|
|
|
|
len == 4+5+(12+2));
|
|
|
|
free(cmd);
|
|
|
|
|
|
|
|
test("Format command with printf-delegation (float): ");
|
|
|
|
len = redisFormatCommand(&cmd,"v:%06.1f",12.34f);
|
|
|
|
test_cond(strncmp(cmd,"*1\r\n$8\r\nv:0012.3\r\n",len) == 0 &&
|
|
|
|
len == 4+4+(8+2));
|
|
|
|
free(cmd);
|
|
|
|
|
|
|
|
test("Format command with printf-delegation and extra interpolation: ");
|
|
|
|
len = redisFormatCommand(&cmd,"key:%d %b",1234,"foo",3);
|
|
|
|
test_cond(strncmp(cmd,"*2\r\n$8\r\nkey:1234\r\n$3\r\nfoo\r\n",len) == 0 &&
|
|
|
|
len == 4+4+(8+2)+4+(3+2));
|
|
|
|
free(cmd);
|
|
|
|
|
|
|
|
test("Format command with wrong printf format and extra interpolation: ");
|
|
|
|
len = redisFormatCommand(&cmd,"key:%08p %b",1234,"foo",3);
|
|
|
|
test_cond(strncmp(cmd,"*2\r\n$6\r\nkey:8p\r\n$3\r\nfoo\r\n",len) == 0 &&
|
|
|
|
len == 4+4+(6+2)+4+(3+2));
|
|
|
|
free(cmd);
|
|
|
|
|
2010-10-31 09:32:31 +00:00
|
|
|
const char *argv[3];
|
|
|
|
argv[0] = "SET";
|
2010-11-22 09:06:43 +00:00
|
|
|
argv[1] = "foo\0xxx";
|
2010-10-31 09:32:31 +00:00
|
|
|
argv[2] = "bar";
|
2010-11-22 09:06:43 +00:00
|
|
|
size_t lens[3] = { 3, 7, 3 };
|
2010-10-31 09:32:31 +00:00
|
|
|
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);
|
2010-11-22 09:06:43 +00:00
|
|
|
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$7\r\nfoo\0xxx\r\n$3\r\nbar\r\n",len) == 0 &&
|
|
|
|
len == 4+4+(3+2)+4+(7+2)+4+(3+2));
|
2010-10-31 09:32:31 +00:00
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
|
2011-01-27 11:50:55 +00:00
|
|
|
static void test_reply_reader(void) {
|
2011-04-21 13:01:58 +00:00
|
|
|
redisReader *reader;
|
2010-11-04 09:54:01 +00:00
|
|
|
void *reply;
|
2010-10-18 12:39:43 +00:00
|
|
|
int ret;
|
2010-09-19 16:47:05 +00:00
|
|
|
|
2010-09-20 12:05:23 +00:00
|
|
|
test("Error handling in reply parser: ");
|
2011-04-21 14:03:54 +00:00
|
|
|
reader = redisReaderCreate();
|
|
|
|
redisReaderFeed(reader,(char*)"@foo\r\n",6);
|
|
|
|
ret = redisReaderGetReply(reader,NULL);
|
2010-09-25 13:09:13 +00:00
|
|
|
test_cond(ret == REDIS_ERR &&
|
2011-04-21 14:03:54 +00:00
|
|
|
strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0);
|
|
|
|
redisReaderFree(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: ");
|
2011-04-21 14:03:54 +00:00
|
|
|
reader = redisReaderCreate();
|
|
|
|
redisReaderFeed(reader,(char*)"*2\r\n",4);
|
|
|
|
redisReaderFeed(reader,(char*)"$5\r\nhello\r\n",11);
|
|
|
|
redisReaderFeed(reader,(char*)"@foo\r\n",6);
|
|
|
|
ret = redisReaderGetReply(reader,NULL);
|
2010-09-25 13:09:13 +00:00
|
|
|
test_cond(ret == REDIS_ERR &&
|
2011-04-21 14:03:54 +00:00
|
|
|
strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0);
|
|
|
|
redisReaderFree(reader);
|
2010-11-04 09:54:01 +00:00
|
|
|
|
2010-11-24 14:46:05 +00:00
|
|
|
test("Set error on nested multi bulks with depth > 1: ");
|
2011-04-21 14:03:54 +00:00
|
|
|
reader = redisReaderCreate();
|
|
|
|
redisReaderFeed(reader,(char*)"*1\r\n",4);
|
|
|
|
redisReaderFeed(reader,(char*)"*1\r\n",4);
|
|
|
|
redisReaderFeed(reader,(char*)"*1\r\n",4);
|
|
|
|
ret = redisReaderGetReply(reader,NULL);
|
2010-11-24 14:46:05 +00:00
|
|
|
test_cond(ret == REDIS_ERR &&
|
2011-04-21 14:03:54 +00:00
|
|
|
strncasecmp(reader->errstr,"No support for",14) == 0);
|
|
|
|
redisReaderFree(reader);
|
2010-11-24 14:46:05 +00:00
|
|
|
|
2010-11-04 09:54:01 +00:00
|
|
|
test("Works with NULL functions for reply: ");
|
2011-04-21 14:03:54 +00:00
|
|
|
reader = redisReaderCreate();
|
2011-04-21 13:01:58 +00:00
|
|
|
reader->fn = NULL;
|
2011-04-21 14:03:54 +00:00
|
|
|
redisReaderFeed(reader,(char*)"+OK\r\n",5);
|
|
|
|
ret = redisReaderGetReply(reader,&reply);
|
2010-11-04 09:54:01 +00:00
|
|
|
test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
|
2011-04-21 14:03:54 +00:00
|
|
|
redisReaderFree(reader);
|
2010-11-04 22:52:47 +00:00
|
|
|
|
|
|
|
test("Works when a single newline (\\r\\n) covers two calls to feed: ");
|
2011-04-21 14:03:54 +00:00
|
|
|
reader = redisReaderCreate();
|
2011-04-21 13:01:58 +00:00
|
|
|
reader->fn = NULL;
|
2011-04-21 14:03:54 +00:00
|
|
|
redisReaderFeed(reader,(char*)"+OK\r",4);
|
|
|
|
ret = redisReaderGetReply(reader,&reply);
|
2010-11-04 22:52:47 +00:00
|
|
|
assert(ret == REDIS_OK && reply == NULL);
|
2011-04-21 14:03:54 +00:00
|
|
|
redisReaderFeed(reader,(char*)"\n",1);
|
|
|
|
ret = redisReaderGetReply(reader,&reply);
|
2010-11-04 22:52:47 +00:00
|
|
|
test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
|
2011-04-21 14:03:54 +00:00
|
|
|
redisReaderFree(reader);
|
2011-01-27 13:39:34 +00:00
|
|
|
|
2011-04-21 08:54:54 +00:00
|
|
|
test("Don't reset state after protocol error: ");
|
2011-04-21 14:03:54 +00:00
|
|
|
reader = redisReaderCreate();
|
2011-04-21 13:01:58 +00:00
|
|
|
reader->fn = NULL;
|
2011-04-21 14:03:54 +00:00
|
|
|
redisReaderFeed(reader,(char*)"x",1);
|
|
|
|
ret = redisReaderGetReply(reader,&reply);
|
2011-01-27 13:39:34 +00:00
|
|
|
assert(ret == REDIS_ERR);
|
2011-04-21 14:03:54 +00:00
|
|
|
ret = redisReaderGetReply(reader,&reply);
|
2011-04-21 08:54:54 +00:00
|
|
|
test_cond(ret == REDIS_ERR && reply == NULL);
|
2011-06-09 07:23:54 +00:00
|
|
|
redisReaderFree(reader);
|
|
|
|
|
|
|
|
/* Regression test for issue #45 on GitHub. */
|
|
|
|
test("Don't do empty allocation for empty multi bulk: ");
|
|
|
|
reader = redisReaderCreate();
|
|
|
|
redisReaderFeed(reader,(char*)"*0\r\n",4);
|
|
|
|
ret = redisReaderGetReply(reader,&reply);
|
|
|
|
test_cond(ret == REDIS_OK &&
|
|
|
|
((redisReply*)reply)->type == REDIS_REPLY_ARRAY &&
|
|
|
|
((redisReply*)reply)->elements == 0);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
redisReaderFree(reader);
|
2010-10-18 12:39:43 +00:00
|
|
|
}
|
|
|
|
|
2011-05-02 12:21:48 +00:00
|
|
|
static void *test_create_string(const redisReadTask *task, char *str, size_t len) {
|
|
|
|
redisReader *r = (redisReader*)task->privdata;
|
|
|
|
const char *roff = r->buf+r->roff;
|
|
|
|
((void)str); ((void)len);
|
|
|
|
|
|
|
|
assert(task->plen > 0);
|
|
|
|
assert(task->clen > 0);
|
|
|
|
switch(task->type) {
|
|
|
|
case REDIS_REPLY_STATUS:
|
|
|
|
assert(strncmp("+status\r\n", roff+task->poff, task->plen) == 0);
|
|
|
|
assert(strncmp("status", roff+task->coff, task->clen) == 0);
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_ERROR:
|
|
|
|
assert(strncmp("-error\r\n", roff+task->poff, task->plen) == 0);
|
|
|
|
assert(strncmp("error", roff+task->coff, task->clen) == 0);
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_STRING: /* bulk */
|
|
|
|
assert(strncmp("$4\r\nbulk\r\n", roff+task->poff, task->plen) == 0);
|
|
|
|
assert(strncmp("bulk", roff+task->coff, task->clen) == 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(NULL);
|
|
|
|
}
|
|
|
|
return (void*)1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *test_create_array(const redisReadTask *task, int len) {
|
|
|
|
redisReader *r = (redisReader*)task->privdata;
|
|
|
|
const char *roff = r->buf+r->roff;
|
|
|
|
((void)len);
|
|
|
|
|
|
|
|
assert(task->plen > 0);
|
|
|
|
assert(task->clen == 0);
|
|
|
|
assert(strncmp("*5\r\n", roff+task->poff, task->plen) == 0);
|
|
|
|
return (void*)1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *test_create_integer(const redisReadTask *task, long long value) {
|
|
|
|
redisReader *r = (redisReader*)task->privdata;
|
|
|
|
const char *roff = r->buf+r->roff;
|
|
|
|
((void)value);
|
|
|
|
|
|
|
|
assert(task->plen > 0);
|
|
|
|
assert(task->clen > 0);
|
|
|
|
assert(strncmp(":1234\r\n", roff+task->poff, task->plen) == 0);
|
|
|
|
assert(strncmp("1234", roff+task->coff, task->clen) == 0);
|
|
|
|
return (void*)1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *test_create_nil(const redisReadTask *task) {
|
|
|
|
redisReader *r = (redisReader*)task->privdata;
|
|
|
|
const char *roff = r->buf+r->roff;
|
|
|
|
|
|
|
|
assert(task->plen > 0);
|
|
|
|
assert(task->clen == 0);
|
|
|
|
assert(strncmp("$-1\r\n", roff+task->poff, task->plen) == 0);
|
|
|
|
return (void*)1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static redisReplyObjectFunctions test_reader_fn = {
|
|
|
|
test_create_string,
|
|
|
|
test_create_array,
|
|
|
|
test_create_integer,
|
|
|
|
test_create_nil,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_reader_functions(void) {
|
|
|
|
redisReader *reader;
|
|
|
|
const char *input;
|
|
|
|
int ret;
|
|
|
|
void *obj;
|
|
|
|
|
|
|
|
input =
|
|
|
|
"*5\r\n"
|
|
|
|
"$-1\r\n"
|
|
|
|
":1234\r\n"
|
|
|
|
"+status\r\n"
|
|
|
|
"-error\r\n"
|
|
|
|
"$4\r\nbulk\r\n";
|
|
|
|
|
|
|
|
test("Custom object functions in reply reader: ");
|
|
|
|
reader = redisReaderCreate();
|
|
|
|
reader->fn = &test_reader_fn;
|
|
|
|
reader->privdata = reader;
|
|
|
|
|
|
|
|
redisReaderFeed(reader,input,strlen(input));
|
|
|
|
ret = redisReaderGetReply(reader,&obj);
|
|
|
|
test_cond(ret == REDIS_OK && obj == (void*)1);
|
|
|
|
redisReaderFree(reader);
|
|
|
|
}
|
|
|
|
|
2011-05-22 14:25:52 +00:00
|
|
|
static void test_blocking_connection_errors(void) {
|
|
|
|
redisContext *c;
|
|
|
|
|
|
|
|
test("Returns error when host cannot be resolved: ");
|
|
|
|
c = redisConnect((char*)"idontexist.local", 6379);
|
|
|
|
test_cond(c->err == REDIS_ERR_OTHER &&
|
|
|
|
strcmp(c->errstr,"Can't resolve: idontexist.local") == 0);
|
|
|
|
redisFree(c);
|
|
|
|
|
|
|
|
test("Returns error when the port is not open: ");
|
|
|
|
c = redisConnect((char*)"localhost", 1);
|
|
|
|
test_cond(c->err == REDIS_ERR_IO &&
|
|
|
|
strcmp(c->errstr,"Connection refused") == 0);
|
|
|
|
redisFree(c);
|
|
|
|
|
|
|
|
test("Returns error when the unix socket path doesn't accept connections: ");
|
|
|
|
c = redisConnectUnix((char*)"/tmp/idontexist.sock");
|
|
|
|
test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */
|
|
|
|
redisFree(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_blocking_connection(struct config config) {
|
|
|
|
redisContext *c;
|
|
|
|
redisReply *reply;
|
|
|
|
|
|
|
|
c = connect(config);
|
|
|
|
|
|
|
|
test("Is able to deliver commands: ");
|
|
|
|
reply = redisCommand(c,"PING");
|
|
|
|
test_cond(reply->type == REDIS_REPLY_STATUS &&
|
|
|
|
strcasecmp(reply->str,"pong") == 0)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
test("Is a able to send commands verbatim: ");
|
|
|
|
reply = redisCommand(c,"SET foo bar");
|
|
|
|
test_cond (reply->type == REDIS_REPLY_STATUS &&
|
|
|
|
strcasecmp(reply->str,"ok") == 0)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
test("%%s String interpolation works: ");
|
|
|
|
reply = redisCommand(c,"SET %s %s","foo","hello world");
|
|
|
|
freeReplyObject(reply);
|
|
|
|
reply = redisCommand(c,"GET foo");
|
|
|
|
test_cond(reply->type == REDIS_REPLY_STRING &&
|
|
|
|
strcmp(reply->str,"hello world") == 0);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
test("%%b String interpolation works: ");
|
|
|
|
reply = redisCommand(c,"SET %b %b","foo",3,"hello\x00world",11);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
reply = redisCommand(c,"GET foo");
|
|
|
|
test_cond(reply->type == REDIS_REPLY_STRING &&
|
|
|
|
memcmp(reply->str,"hello\x00world",11) == 0)
|
|
|
|
|
|
|
|
test("Binary reply length is correct: ");
|
|
|
|
test_cond(reply->len == 11)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
test("Can parse nil replies: ");
|
|
|
|
reply = redisCommand(c,"GET nokey");
|
|
|
|
test_cond(reply->type == REDIS_REPLY_NIL)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
/* test 7 */
|
|
|
|
test("Can parse integer replies: ");
|
|
|
|
reply = redisCommand(c,"INCR mycounter");
|
|
|
|
test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
test("Can parse multi bulk replies: ");
|
|
|
|
freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
|
|
|
|
freeReplyObject(redisCommand(c,"LPUSH mylist bar"));
|
|
|
|
reply = redisCommand(c,"LRANGE mylist 0 -1");
|
|
|
|
test_cond(reply->type == REDIS_REPLY_ARRAY &&
|
|
|
|
reply->elements == 2 &&
|
|
|
|
!memcmp(reply->element[0]->str,"bar",3) &&
|
|
|
|
!memcmp(reply->element[1]->str,"foo",3))
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
/* m/e with multi bulk reply *before* other reply.
|
|
|
|
* specifically test ordering of reply items to parse. */
|
|
|
|
test("Can handle nested multi bulk replies: ");
|
|
|
|
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);
|
|
|
|
|
|
|
|
disconnect(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_blocking_io_errors(struct config config) {
|
|
|
|
redisContext *c;
|
|
|
|
redisReply *reply;
|
2011-06-18 10:29:49 +00:00
|
|
|
void *_reply;
|
2011-05-22 14:25:52 +00:00
|
|
|
int major, minor;
|
|
|
|
|
|
|
|
/* Connect to target given by config. */
|
|
|
|
c = connect(config);
|
|
|
|
{
|
|
|
|
/* Find out Redis version to determine the path for the next test */
|
|
|
|
const char *field = "redis_version:";
|
|
|
|
char *p, *eptr;
|
|
|
|
|
|
|
|
reply = redisCommand(c,"INFO");
|
|
|
|
p = strstr(reply->str,field);
|
|
|
|
major = strtol(p+strlen(field),&eptr,10);
|
|
|
|
p = eptr+1; /* char next to the first "." */
|
|
|
|
minor = strtol(p,&eptr,10);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
test("Returns I/O error when the connection is lost: ");
|
|
|
|
reply = redisCommand(c,"QUIT");
|
|
|
|
if (major >= 2 && minor > 0) {
|
|
|
|
/* > 2.0 returns OK on QUIT and read() should be issued once more
|
|
|
|
* to know the descriptor is at EOF. */
|
|
|
|
test_cond(strcasecmp(reply->str,"OK") == 0 &&
|
2011-06-18 10:29:49 +00:00
|
|
|
redisGetReply(c,&_reply) == REDIS_ERR);
|
2011-05-22 14:25:52 +00:00
|
|
|
freeReplyObject(reply);
|
|
|
|
} else {
|
|
|
|
test_cond(reply == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On 2.0, QUIT will cause the connection to be closed immediately and
|
|
|
|
* the read(2) for the reply on QUIT will set the error to EOF.
|
|
|
|
* On >2.0, QUIT will return with OK and another read(2) needed to be
|
|
|
|
* issued to find out the socket was closed by the server. In both
|
|
|
|
* conditions, the error will be set to EOF. */
|
|
|
|
assert(c->err == REDIS_ERR_EOF &&
|
|
|
|
strcmp(c->errstr,"Server closed the connection") == 0);
|
|
|
|
redisFree(c);
|
|
|
|
|
|
|
|
c = connect(config);
|
|
|
|
test("Returns I/O error on socket timeout: ");
|
|
|
|
struct timeval tv = { 0, 1000 };
|
|
|
|
assert(redisSetTimeout(c,tv) == REDIS_OK);
|
2011-06-18 10:29:49 +00:00
|
|
|
test_cond(redisGetReply(c,&_reply) == REDIS_ERR &&
|
2011-05-22 14:25:52 +00:00
|
|
|
c->err == REDIS_ERR_IO && errno == EAGAIN);
|
|
|
|
redisFree(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_throughput(struct config config) {
|
|
|
|
redisContext *c = connect(config);
|
|
|
|
redisReply **replies;
|
2011-04-19 21:03:29 +00:00
|
|
|
int i, num;
|
2010-10-18 12:39:43 +00:00
|
|
|
long long t1, t2;
|
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
|
|
|
|
2011-04-19 21:03:29 +00:00
|
|
|
num = 1000;
|
|
|
|
replies = malloc(sizeof(redisReply*)*num);
|
2010-09-19 13:18:51 +00:00
|
|
|
t1 = usec();
|
2011-04-19 21:03:29 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2010-11-03 10:43:01 +00:00
|
|
|
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();
|
2011-04-19 21:03:29 +00:00
|
|
|
for (i = 0; i < num; i++) freeReplyObject(replies[i]);
|
2010-09-25 13:33:46 +00:00
|
|
|
free(replies);
|
2011-04-19 21:03:29 +00:00
|
|
|
printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0);
|
2010-09-19 13:18:51 +00:00
|
|
|
|
2011-04-19 21:03:29 +00:00
|
|
|
replies = malloc(sizeof(redisReply*)*num);
|
2010-09-19 13:18:51 +00:00
|
|
|
t1 = usec();
|
2011-04-19 21:03:29 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2010-11-03 10:43:01 +00:00
|
|
|
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();
|
2011-04-19 21:03:29 +00:00
|
|
|
for (i = 0; i < num; i++) freeReplyObject(replies[i]);
|
2010-09-25 13:33:46 +00:00
|
|
|
free(replies);
|
2011-04-19 21:03:29 +00:00
|
|
|
printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0);
|
2011-03-31 10:41:46 +00:00
|
|
|
|
2011-04-19 21:03:29 +00:00
|
|
|
num = 10000;
|
|
|
|
replies = malloc(sizeof(redisReply*)*num);
|
|
|
|
for (i = 0; i < num; i++)
|
2011-03-31 10:41:46 +00:00
|
|
|
redisAppendCommand(c,"PING");
|
|
|
|
t1 = usec();
|
2011-04-19 21:03:29 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2011-03-31 10:41:46 +00:00
|
|
|
assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
|
|
|
|
assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
|
|
|
|
}
|
|
|
|
t2 = usec();
|
2011-04-19 21:03:29 +00:00
|
|
|
for (i = 0; i < num; i++) freeReplyObject(replies[i]);
|
2011-03-31 10:41:46 +00:00
|
|
|
free(replies);
|
2011-04-19 21:03:29 +00:00
|
|
|
printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
|
2011-03-31 10:41:46 +00:00
|
|
|
|
2011-04-19 21:03:29 +00:00
|
|
|
replies = malloc(sizeof(redisReply*)*num);
|
|
|
|
for (i = 0; i < num; i++)
|
2011-03-31 10:41:46 +00:00
|
|
|
redisAppendCommand(c,"LRANGE mylist 0 499");
|
|
|
|
t1 = usec();
|
2011-04-19 21:03:29 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2011-03-31 10:41:46 +00:00
|
|
|
assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
|
|
|
|
assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
|
|
|
|
assert(replies[i] != NULL && replies[i]->elements == 500);
|
|
|
|
}
|
|
|
|
t2 = usec();
|
2011-04-19 21:03:29 +00:00
|
|
|
for (i = 0; i < num; i++) freeReplyObject(replies[i]);
|
2011-03-31 10:41:46 +00:00
|
|
|
free(replies);
|
2011-04-19 21:03:29 +00:00
|
|
|
printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
|
2010-10-18 12:39:43 +00:00
|
|
|
|
2011-05-22 14:25:52 +00:00
|
|
|
disconnect(c);
|
2010-10-18 12:39:43 +00:00
|
|
|
}
|
|
|
|
|
2010-10-31 09:56:24 +00:00
|
|
|
// 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;
|
2011-04-13 05:45:08 +00:00
|
|
|
// return redisConnectNonBlock("127.0.0.1", port, NULL);
|
2010-10-31 09:56:24 +00:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// 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-10-18 13:39:56 +00:00
|
|
|
|
2010-11-03 10:43:01 +00:00
|
|
|
int main(int argc, char **argv) {
|
2011-05-22 14:25:52 +00:00
|
|
|
struct config cfg = {
|
|
|
|
.tcp = {
|
|
|
|
.host = "127.0.0.1",
|
|
|
|
.port = 6379
|
|
|
|
},
|
|
|
|
.unix = {
|
|
|
|
.path = "/tmp/redis.sock"
|
|
|
|
}
|
|
|
|
};
|
2011-06-09 07:16:31 +00:00
|
|
|
int throughput = 1;
|
2011-05-22 14:25:52 +00:00
|
|
|
|
|
|
|
/* Ignore broken pipe signal (for I/O error tests). */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
/* Parse command line options. */
|
|
|
|
argv++; argc--;
|
|
|
|
while (argc) {
|
|
|
|
if (argc >= 2 && !strcmp(argv[0],"-h")) {
|
|
|
|
argv++; argc--;
|
|
|
|
cfg.tcp.host = argv[0];
|
|
|
|
} else if (argc >= 2 && !strcmp(argv[0],"-p")) {
|
|
|
|
argv++; argc--;
|
|
|
|
cfg.tcp.port = atoi(argv[0]);
|
|
|
|
} else if (argc >= 2 && !strcmp(argv[0],"-s")) {
|
|
|
|
argv++; argc--;
|
|
|
|
cfg.unix.path = argv[0];
|
2011-06-09 07:16:31 +00:00
|
|
|
} else if (argc >= 1 && !strcmp(argv[0],"--skip-throughput")) {
|
|
|
|
throughput = 0;
|
2011-05-22 14:25:52 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Invalid argument: %s\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
argv++; argc--;
|
2010-11-03 10:43:01 +00:00
|
|
|
}
|
|
|
|
|
2010-10-31 09:32:31 +00:00
|
|
|
test_format_commands();
|
2010-10-18 12:39:43 +00:00
|
|
|
test_reply_reader();
|
2011-05-02 12:21:48 +00:00
|
|
|
test_reader_functions();
|
2011-05-22 14:25:52 +00:00
|
|
|
test_blocking_connection_errors();
|
|
|
|
|
|
|
|
printf("\nTesting against TCP connection (%s:%d):\n", cfg.tcp.host, cfg.tcp.port);
|
|
|
|
cfg.type = CONN_TCP;
|
|
|
|
test_blocking_connection(cfg);
|
|
|
|
test_blocking_io_errors(cfg);
|
2011-06-09 07:16:31 +00:00
|
|
|
if (throughput) test_throughput(cfg);
|
2011-05-22 14:25:52 +00:00
|
|
|
|
|
|
|
printf("\nTesting against Unix socket connection (%s):\n", cfg.unix.path);
|
|
|
|
cfg.type = CONN_UNIX;
|
|
|
|
test_blocking_connection(cfg);
|
|
|
|
test_blocking_io_errors(cfg);
|
2011-06-09 07:16:31 +00:00
|
|
|
if (throughput) test_throughput(cfg);
|
2010-09-19 13:18:51 +00:00
|
|
|
|
2011-05-29 17:03:39 +00:00
|
|
|
if (fails) {
|
2010-09-20 11:44:24 +00:00
|
|
|
printf("*** %d TESTS FAILED ***\n", fails);
|
2011-05-29 17:03:39 +00:00
|
|
|
return 1;
|
2010-09-20 11:44:24 +00:00
|
|
|
}
|
2011-05-29 17:03:39 +00:00
|
|
|
|
|
|
|
printf("ALL TESTS PASSED\n");
|
2010-05-18 18:12:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|