diff options
Diffstat (limited to 'test.c')
-rw-r--r-- | test.c | 122 |
1 files changed, 60 insertions, 62 deletions
@@ -268,75 +268,71 @@ static void test_blocking_connection(void) { } static void test_reply_reader(void) { - void *reader; + redisReader *reader; void *reply; - char *err; int ret; test("Error handling in reply parser: "); - reader = redisReplyReaderCreate(); - redisReplyReaderFeed(reader,(char*)"@foo\r\n",6); - ret = redisReplyReaderGetReply(reader,NULL); - err = redisReplyReaderGetError(reader); + reader = redisReaderCreate(); + redisReaderFeed(reader,(char*)"@foo\r\n",6); + ret = redisReaderGetReply(reader,NULL); test_cond(ret == REDIS_ERR && - strcasecmp(err,"Protocol error, got \"@\" as reply type byte") == 0); - redisReplyReaderFree(reader); + strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0); + redisReaderFree(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(); - redisReplyReaderFeed(reader,(char*)"*2\r\n",4); - redisReplyReaderFeed(reader,(char*)"$5\r\nhello\r\n",11); - redisReplyReaderFeed(reader,(char*)"@foo\r\n",6); - ret = redisReplyReaderGetReply(reader,NULL); - err = redisReplyReaderGetError(reader); + 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); test_cond(ret == REDIS_ERR && - strcasecmp(err,"Protocol error, got \"@\" as reply type byte") == 0); - redisReplyReaderFree(reader); + strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0); + redisReaderFree(reader); test("Set error on nested multi bulks with depth > 1: "); - reader = redisReplyReaderCreate(); - redisReplyReaderFeed(reader,(char*)"*1\r\n",4); - redisReplyReaderFeed(reader,(char*)"*1\r\n",4); - redisReplyReaderFeed(reader,(char*)"*1\r\n",4); - ret = redisReplyReaderGetReply(reader,NULL); - err = redisReplyReaderGetError(reader); + 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); test_cond(ret == REDIS_ERR && - strncasecmp(err,"No support for",14) == 0); - redisReplyReaderFree(reader); + strncasecmp(reader->errstr,"No support for",14) == 0); + redisReaderFree(reader); test("Works with NULL functions for reply: "); - reader = redisReplyReaderCreate(); - redisReplyReaderSetReplyObjectFunctions(reader,NULL); - redisReplyReaderFeed(reader,(char*)"+OK\r\n",5); - ret = redisReplyReaderGetReply(reader,&reply); + reader = redisReaderCreate(); + reader->fn = NULL; + redisReaderFeed(reader,(char*)"+OK\r\n",5); + ret = redisReaderGetReply(reader,&reply); test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS); - redisReplyReaderFree(reader); + redisReaderFree(reader); test("Works when a single newline (\\r\\n) covers two calls to feed: "); - reader = redisReplyReaderCreate(); - redisReplyReaderSetReplyObjectFunctions(reader,NULL); - redisReplyReaderFeed(reader,(char*)"+OK\r",4); - ret = redisReplyReaderGetReply(reader,&reply); + reader = redisReaderCreate(); + reader->fn = NULL; + redisReaderFeed(reader,(char*)"+OK\r",4); + ret = redisReaderGetReply(reader,&reply); assert(ret == REDIS_OK && reply == NULL); - redisReplyReaderFeed(reader,(char*)"\n",1); - ret = redisReplyReaderGetReply(reader,&reply); + redisReaderFeed(reader,(char*)"\n",1); + ret = redisReaderGetReply(reader,&reply); test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS); - redisReplyReaderFree(reader); + redisReaderFree(reader); - test("Properly reset state after protocol error: "); - reader = redisReplyReaderCreate(); - redisReplyReaderSetReplyObjectFunctions(reader,NULL); - redisReplyReaderFeed(reader,(char*)"x",1); - ret = redisReplyReaderGetReply(reader,&reply); + test("Don't reset state after protocol error: "); + reader = redisReaderCreate(); + reader->fn = NULL; + redisReaderFeed(reader,(char*)"x",1); + ret = redisReaderGetReply(reader,&reply); assert(ret == REDIS_ERR); - ret = redisReplyReaderGetReply(reader,&reply); - test_cond(ret == REDIS_OK && reply == NULL) + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_ERR && reply == NULL); } static void test_throughput(void) { - int i; + int i, num; long long t1, t2; redisContext *c = blocking_context; redisReply **replies; @@ -345,55 +341,57 @@ static void test_throughput(void) { for (i = 0; i < 500; i++) freeReplyObject(redisCommand(c,"LPUSH mylist foo")); - replies = malloc(sizeof(redisReply*)*1000); + num = 1000; + replies = malloc(sizeof(redisReply*)*num); t1 = usec(); - for (i = 0; i < 1000; i++) { + for (i = 0; i < num; i++) { replies[i] = redisCommand(c,"PING"); assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS); } t2 = usec(); - for (i = 0; i < 1000; i++) freeReplyObject(replies[i]); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); free(replies); - printf("\t(1000x PING: %.3fs)\n", (t2-t1)/1000000.0); + printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0); - replies = malloc(sizeof(redisReply*)*1000); + replies = malloc(sizeof(redisReply*)*num); t1 = usec(); - for (i = 0; i < 1000; i++) { + for (i = 0; i < num; 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); } t2 = usec(); - for (i = 0; i < 1000; i++) freeReplyObject(replies[i]); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); free(replies); - printf("\t(1000x LRANGE with 500 elements: %.3fs)\n", (t2-t1)/1000000.0); + printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0); - replies = malloc(sizeof(redisReply*)*10000); - for (i = 0; i < 10000; i++) + num = 10000; + replies = malloc(sizeof(redisReply*)*num); + for (i = 0; i < num; i++) redisAppendCommand(c,"PING"); t1 = usec(); - for (i = 0; i < 10000; i++) { + for (i = 0; i < num; i++) { assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK); assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS); } t2 = usec(); - for (i = 0; i < 10000; i++) freeReplyObject(replies[i]); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); free(replies); - printf("\t(10000x PING (pipelined): %.3fs)\n", (t2-t1)/1000000.0); + printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0); - replies = malloc(sizeof(redisReply*)*10000); - for (i = 0; i < 10000; i++) + replies = malloc(sizeof(redisReply*)*num); + for (i = 0; i < num; i++) redisAppendCommand(c,"LRANGE mylist 0 499"); t1 = usec(); - for (i = 0; i < 10000; i++) { + for (i = 0; i < num; i++) { 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(); - for (i = 0; i < 10000; i++) freeReplyObject(replies[i]); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); free(replies); - printf("\t(10000x LRANGE with 500 elements: %.3fs)\n", (t2-t1)/1000000.0); + printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0); } static void cleanup(void) { |