/hiredis/test.c

http://github.com/nicolasff/webdis · C · 806 lines · 569 code · 110 blank · 127 comment · 200 complexity · 966520bb1125045a2f47a3b4b00e1c79 MD5 · raw file

  1. #include "fmacros.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <strings.h>
  6. #include <sys/time.h>
  7. #include <assert.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <errno.h>
  11. #include <limits.h>
  12. #include "hiredis.h"
  13. #include "net.h"
  14. enum connection_type {
  15. CONN_TCP,
  16. CONN_UNIX,
  17. CONN_FD
  18. };
  19. struct config {
  20. enum connection_type type;
  21. struct {
  22. const char *host;
  23. int port;
  24. struct timeval timeout;
  25. } tcp;
  26. struct {
  27. const char *path;
  28. } unix;
  29. };
  30. /* The following lines make up our testing "framework" :) */
  31. static int tests = 0, fails = 0;
  32. #define test(_s) { printf("#%02d ", ++tests); printf(_s); }
  33. #define test_cond(_c) if(_c) printf("\033[0;32mPASSED\033[0;0m\n"); else {printf("\033[0;31mFAILED\033[0;0m\n"); fails++;}
  34. static long long usec(void) {
  35. struct timeval tv;
  36. gettimeofday(&tv,NULL);
  37. return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
  38. }
  39. /* The assert() calls below have side effects, so we need assert()
  40. * even if we are compiling without asserts (-DNDEBUG). */
  41. #ifdef NDEBUG
  42. #undef assert
  43. #define assert(e) (void)(e)
  44. #endif
  45. static redisContext *select_database(redisContext *c) {
  46. redisReply *reply;
  47. /* Switch to DB 9 for testing, now that we know we can chat. */
  48. reply = redisCommand(c,"SELECT 9");
  49. assert(reply != NULL);
  50. freeReplyObject(reply);
  51. /* Make sure the DB is emtpy */
  52. reply = redisCommand(c,"DBSIZE");
  53. assert(reply != NULL);
  54. if (reply->type == REDIS_REPLY_INTEGER && reply->integer == 0) {
  55. /* Awesome, DB 9 is empty and we can continue. */
  56. freeReplyObject(reply);
  57. } else {
  58. printf("Database #9 is not empty, test can not continue\n");
  59. exit(1);
  60. }
  61. return c;
  62. }
  63. static int disconnect(redisContext *c, int keep_fd) {
  64. redisReply *reply;
  65. /* Make sure we're on DB 9. */
  66. reply = redisCommand(c,"SELECT 9");
  67. assert(reply != NULL);
  68. freeReplyObject(reply);
  69. reply = redisCommand(c,"FLUSHDB");
  70. assert(reply != NULL);
  71. freeReplyObject(reply);
  72. /* Free the context as well, but keep the fd if requested. */
  73. if (keep_fd)
  74. return redisFreeKeepFd(c);
  75. redisFree(c);
  76. return -1;
  77. }
  78. static redisContext *connect(struct config config) {
  79. redisContext *c = NULL;
  80. if (config.type == CONN_TCP) {
  81. c = redisConnect(config.tcp.host, config.tcp.port);
  82. } else if (config.type == CONN_UNIX) {
  83. c = redisConnectUnix(config.unix.path);
  84. } else if (config.type == CONN_FD) {
  85. /* Create a dummy connection just to get an fd to inherit */
  86. redisContext *dummy_ctx = redisConnectUnix(config.unix.path);
  87. if (dummy_ctx) {
  88. int fd = disconnect(dummy_ctx, 1);
  89. printf("Connecting to inherited fd %d\n", fd);
  90. c = redisConnectFd(fd);
  91. }
  92. } else {
  93. assert(NULL);
  94. }
  95. if (c == NULL) {
  96. printf("Connection error: can't allocate redis context\n");
  97. exit(1);
  98. } else if (c->err) {
  99. printf("Connection error: %s\n", c->errstr);
  100. redisFree(c);
  101. exit(1);
  102. }
  103. return select_database(c);
  104. }
  105. static void test_format_commands(void) {
  106. char *cmd;
  107. int len;
  108. test("Format command without interpolation: ");
  109. len = redisFormatCommand(&cmd,"SET foo bar");
  110. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
  111. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  112. free(cmd);
  113. test("Format command with %%s string interpolation: ");
  114. len = redisFormatCommand(&cmd,"SET %s %s","foo","bar");
  115. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
  116. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  117. free(cmd);
  118. test("Format command with %%s and an empty string: ");
  119. len = redisFormatCommand(&cmd,"SET %s %s","foo","");
  120. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
  121. len == 4+4+(3+2)+4+(3+2)+4+(0+2));
  122. free(cmd);
  123. test("Format command with an empty string in between proper interpolations: ");
  124. len = redisFormatCommand(&cmd,"SET %s %s","","foo");
  125. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",len) == 0 &&
  126. len == 4+4+(3+2)+4+(0+2)+4+(3+2));
  127. free(cmd);
  128. test("Format command with %%b string interpolation: ");
  129. len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"b\0r",(size_t)3);
  130. 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 &&
  131. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  132. free(cmd);
  133. test("Format command with %%b and an empty string: ");
  134. len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"",(size_t)0);
  135. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
  136. len == 4+4+(3+2)+4+(3+2)+4+(0+2));
  137. free(cmd);
  138. test("Format command with literal %%: ");
  139. len = redisFormatCommand(&cmd,"SET %% %%");
  140. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$1\r\n%\r\n$1\r\n%\r\n",len) == 0 &&
  141. len == 4+4+(3+2)+4+(1+2)+4+(1+2));
  142. free(cmd);
  143. /* Vararg width depends on the type. These tests make sure that the
  144. * width is correctly determined using the format and subsequent varargs
  145. * can correctly be interpolated. */
  146. #define INTEGER_WIDTH_TEST(fmt, type) do { \
  147. type value = 123; \
  148. test("Format command with printf-delegation (" #type "): "); \
  149. len = redisFormatCommand(&cmd,"key:%08" fmt " str:%s", value, "hello"); \
  150. test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:00000123\r\n$9\r\nstr:hello\r\n",len) == 0 && \
  151. len == 4+5+(12+2)+4+(9+2)); \
  152. free(cmd); \
  153. } while(0)
  154. #define FLOAT_WIDTH_TEST(type) do { \
  155. type value = 123.0; \
  156. test("Format command with printf-delegation (" #type "): "); \
  157. len = redisFormatCommand(&cmd,"key:%08.3f str:%s", value, "hello"); \
  158. test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:0123.000\r\n$9\r\nstr:hello\r\n",len) == 0 && \
  159. len == 4+5+(12+2)+4+(9+2)); \
  160. free(cmd); \
  161. } while(0)
  162. INTEGER_WIDTH_TEST("d", int);
  163. INTEGER_WIDTH_TEST("hhd", char);
  164. INTEGER_WIDTH_TEST("hd", short);
  165. INTEGER_WIDTH_TEST("ld", long);
  166. INTEGER_WIDTH_TEST("lld", long long);
  167. INTEGER_WIDTH_TEST("u", unsigned int);
  168. INTEGER_WIDTH_TEST("hhu", unsigned char);
  169. INTEGER_WIDTH_TEST("hu", unsigned short);
  170. INTEGER_WIDTH_TEST("lu", unsigned long);
  171. INTEGER_WIDTH_TEST("llu", unsigned long long);
  172. FLOAT_WIDTH_TEST(float);
  173. FLOAT_WIDTH_TEST(double);
  174. test("Format command with invalid printf format: ");
  175. len = redisFormatCommand(&cmd,"key:%08p %b",(void*)1234,"foo",(size_t)3);
  176. test_cond(len == -1);
  177. const char *argv[3];
  178. argv[0] = "SET";
  179. argv[1] = "foo\0xxx";
  180. argv[2] = "bar";
  181. size_t lens[3] = { 3, 7, 3 };
  182. int argc = 3;
  183. test("Format command by passing argc/argv without lengths: ");
  184. len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
  185. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
  186. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  187. free(cmd);
  188. test("Format command by passing argc/argv with lengths: ");
  189. len = redisFormatCommandArgv(&cmd,argc,argv,lens);
  190. 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 &&
  191. len == 4+4+(3+2)+4+(7+2)+4+(3+2));
  192. free(cmd);
  193. }
  194. static void test_append_formatted_commands(struct config config) {
  195. redisContext *c;
  196. redisReply *reply;
  197. char *cmd;
  198. int len;
  199. c = connect(config);
  200. test("Append format command: ");
  201. len = redisFormatCommand(&cmd, "SET foo bar");
  202. test_cond(redisAppendFormattedCommand(c, cmd, len) == REDIS_OK);
  203. assert(redisGetReply(c, (void*)&reply) == REDIS_OK);
  204. free(cmd);
  205. freeReplyObject(reply);
  206. disconnect(c, 0);
  207. }
  208. static void test_reply_reader(void) {
  209. redisReader *reader;
  210. void *reply;
  211. int ret;
  212. int i;
  213. test("Error handling in reply parser: ");
  214. reader = redisReaderCreate();
  215. redisReaderFeed(reader,(char*)"@foo\r\n",6);
  216. ret = redisReaderGetReply(reader,NULL);
  217. test_cond(ret == REDIS_ERR &&
  218. strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0);
  219. redisReaderFree(reader);
  220. /* when the reply already contains multiple items, they must be free'd
  221. * on an error. valgrind will bark when this doesn't happen. */
  222. test("Memory cleanup in reply parser: ");
  223. reader = redisReaderCreate();
  224. redisReaderFeed(reader,(char*)"*2\r\n",4);
  225. redisReaderFeed(reader,(char*)"$5\r\nhello\r\n",11);
  226. redisReaderFeed(reader,(char*)"@foo\r\n",6);
  227. ret = redisReaderGetReply(reader,NULL);
  228. test_cond(ret == REDIS_ERR &&
  229. strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0);
  230. redisReaderFree(reader);
  231. test("Set error on nested multi bulks with depth > 7: ");
  232. reader = redisReaderCreate();
  233. for (i = 0; i < 9; i++) {
  234. redisReaderFeed(reader,(char*)"*1\r\n",4);
  235. }
  236. ret = redisReaderGetReply(reader,NULL);
  237. test_cond(ret == REDIS_ERR &&
  238. strncasecmp(reader->errstr,"No support for",14) == 0);
  239. redisReaderFree(reader);
  240. test("Works with NULL functions for reply: ");
  241. reader = redisReaderCreate();
  242. reader->fn = NULL;
  243. redisReaderFeed(reader,(char*)"+OK\r\n",5);
  244. ret = redisReaderGetReply(reader,&reply);
  245. test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
  246. redisReaderFree(reader);
  247. test("Works when a single newline (\\r\\n) covers two calls to feed: ");
  248. reader = redisReaderCreate();
  249. reader->fn = NULL;
  250. redisReaderFeed(reader,(char*)"+OK\r",4);
  251. ret = redisReaderGetReply(reader,&reply);
  252. assert(ret == REDIS_OK && reply == NULL);
  253. redisReaderFeed(reader,(char*)"\n",1);
  254. ret = redisReaderGetReply(reader,&reply);
  255. test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
  256. redisReaderFree(reader);
  257. test("Don't reset state after protocol error: ");
  258. reader = redisReaderCreate();
  259. reader->fn = NULL;
  260. redisReaderFeed(reader,(char*)"x",1);
  261. ret = redisReaderGetReply(reader,&reply);
  262. assert(ret == REDIS_ERR);
  263. ret = redisReaderGetReply(reader,&reply);
  264. test_cond(ret == REDIS_ERR && reply == NULL);
  265. redisReaderFree(reader);
  266. /* Regression test for issue #45 on GitHub. */
  267. test("Don't do empty allocation for empty multi bulk: ");
  268. reader = redisReaderCreate();
  269. redisReaderFeed(reader,(char*)"*0\r\n",4);
  270. ret = redisReaderGetReply(reader,&reply);
  271. test_cond(ret == REDIS_OK &&
  272. ((redisReply*)reply)->type == REDIS_REPLY_ARRAY &&
  273. ((redisReply*)reply)->elements == 0);
  274. freeReplyObject(reply);
  275. redisReaderFree(reader);
  276. }
  277. static void test_free_null(void) {
  278. void *redisContext = NULL;
  279. void *reply = NULL;
  280. test("Don't fail when redisFree is passed a NULL value: ");
  281. redisFree(redisContext);
  282. test_cond(redisContext == NULL);
  283. test("Don't fail when freeReplyObject is passed a NULL value: ");
  284. freeReplyObject(reply);
  285. test_cond(reply == NULL);
  286. }
  287. static void test_blocking_connection_errors(void) {
  288. redisContext *c;
  289. test("Returns error when host cannot be resolved: ");
  290. c = redisConnect((char*)"idontexist.test", 6379);
  291. test_cond(c->err == REDIS_ERR_OTHER &&
  292. (strcmp(c->errstr,"Name or service not known") == 0 ||
  293. strcmp(c->errstr,"Can't resolve: idontexist.test") == 0 ||
  294. strcmp(c->errstr,"nodename nor servname provided, or not known") == 0 ||
  295. strcmp(c->errstr,"No address associated with hostname") == 0 ||
  296. strcmp(c->errstr,"Temporary failure in name resolution") == 0 ||
  297. strcmp(c->errstr,"no address associated with name") == 0));
  298. redisFree(c);
  299. test("Returns error when the port is not open: ");
  300. c = redisConnect((char*)"localhost", 1);
  301. test_cond(c->err == REDIS_ERR_IO &&
  302. strcmp(c->errstr,"Connection refused") == 0);
  303. redisFree(c);
  304. test("Returns error when the unix socket path doesn't accept connections: ");
  305. c = redisConnectUnix((char*)"/tmp/idontexist.sock");
  306. test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */
  307. redisFree(c);
  308. }
  309. static void test_blocking_connection(struct config config) {
  310. redisContext *c;
  311. redisReply *reply;
  312. c = connect(config);
  313. test("Is able to deliver commands: ");
  314. reply = redisCommand(c,"PING");
  315. test_cond(reply->type == REDIS_REPLY_STATUS &&
  316. strcasecmp(reply->str,"pong") == 0)
  317. freeReplyObject(reply);
  318. test("Is a able to send commands verbatim: ");
  319. reply = redisCommand(c,"SET foo bar");
  320. test_cond (reply->type == REDIS_REPLY_STATUS &&
  321. strcasecmp(reply->str,"ok") == 0)
  322. freeReplyObject(reply);
  323. test("%%s String interpolation works: ");
  324. reply = redisCommand(c,"SET %s %s","foo","hello world");
  325. freeReplyObject(reply);
  326. reply = redisCommand(c,"GET foo");
  327. test_cond(reply->type == REDIS_REPLY_STRING &&
  328. strcmp(reply->str,"hello world") == 0);
  329. freeReplyObject(reply);
  330. test("%%b String interpolation works: ");
  331. reply = redisCommand(c,"SET %b %b","foo",(size_t)3,"hello\x00world",(size_t)11);
  332. freeReplyObject(reply);
  333. reply = redisCommand(c,"GET foo");
  334. test_cond(reply->type == REDIS_REPLY_STRING &&
  335. memcmp(reply->str,"hello\x00world",11) == 0)
  336. test("Binary reply length is correct: ");
  337. test_cond(reply->len == 11)
  338. freeReplyObject(reply);
  339. test("Can parse nil replies: ");
  340. reply = redisCommand(c,"GET nokey");
  341. test_cond(reply->type == REDIS_REPLY_NIL)
  342. freeReplyObject(reply);
  343. /* test 7 */
  344. test("Can parse integer replies: ");
  345. reply = redisCommand(c,"INCR mycounter");
  346. test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
  347. freeReplyObject(reply);
  348. test("Can parse multi bulk replies: ");
  349. freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
  350. freeReplyObject(redisCommand(c,"LPUSH mylist bar"));
  351. reply = redisCommand(c,"LRANGE mylist 0 -1");
  352. test_cond(reply->type == REDIS_REPLY_ARRAY &&
  353. reply->elements == 2 &&
  354. !memcmp(reply->element[0]->str,"bar",3) &&
  355. !memcmp(reply->element[1]->str,"foo",3))
  356. freeReplyObject(reply);
  357. /* m/e with multi bulk reply *before* other reply.
  358. * specifically test ordering of reply items to parse. */
  359. test("Can handle nested multi bulk replies: ");
  360. freeReplyObject(redisCommand(c,"MULTI"));
  361. freeReplyObject(redisCommand(c,"LRANGE mylist 0 -1"));
  362. freeReplyObject(redisCommand(c,"PING"));
  363. reply = (redisCommand(c,"EXEC"));
  364. test_cond(reply->type == REDIS_REPLY_ARRAY &&
  365. reply->elements == 2 &&
  366. reply->element[0]->type == REDIS_REPLY_ARRAY &&
  367. reply->element[0]->elements == 2 &&
  368. !memcmp(reply->element[0]->element[0]->str,"bar",3) &&
  369. !memcmp(reply->element[0]->element[1]->str,"foo",3) &&
  370. reply->element[1]->type == REDIS_REPLY_STATUS &&
  371. strcasecmp(reply->element[1]->str,"pong") == 0);
  372. freeReplyObject(reply);
  373. disconnect(c, 0);
  374. }
  375. static void test_blocking_connection_timeouts(struct config config) {
  376. redisContext *c;
  377. redisReply *reply;
  378. ssize_t s;
  379. const char *cmd = "DEBUG SLEEP 3\r\n";
  380. struct timeval tv;
  381. c = connect(config);
  382. test("Successfully completes a command when the timeout is not exceeded: ");
  383. reply = redisCommand(c,"SET foo fast");
  384. freeReplyObject(reply);
  385. tv.tv_sec = 0;
  386. tv.tv_usec = 10000;
  387. redisSetTimeout(c, tv);
  388. reply = redisCommand(c, "GET foo");
  389. test_cond(reply != NULL && reply->type == REDIS_REPLY_STRING && memcmp(reply->str, "fast", 4) == 0);
  390. freeReplyObject(reply);
  391. disconnect(c, 0);
  392. c = connect(config);
  393. test("Does not return a reply when the command times out: ");
  394. s = write(c->fd, cmd, strlen(cmd));
  395. tv.tv_sec = 0;
  396. tv.tv_usec = 10000;
  397. redisSetTimeout(c, tv);
  398. reply = redisCommand(c, "GET foo");
  399. test_cond(s > 0 && reply == NULL && c->err == REDIS_ERR_IO && strcmp(c->errstr, "Resource temporarily unavailable") == 0);
  400. freeReplyObject(reply);
  401. test("Reconnect properly reconnects after a timeout: ");
  402. redisReconnect(c);
  403. reply = redisCommand(c, "PING");
  404. test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0);
  405. freeReplyObject(reply);
  406. test("Reconnect properly uses owned parameters: ");
  407. config.tcp.host = "foo";
  408. config.unix.path = "foo";
  409. redisReconnect(c);
  410. reply = redisCommand(c, "PING");
  411. test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0);
  412. freeReplyObject(reply);
  413. disconnect(c, 0);
  414. }
  415. static void test_blocking_io_errors(struct config config) {
  416. redisContext *c;
  417. redisReply *reply;
  418. void *_reply;
  419. int major, minor;
  420. /* Connect to target given by config. */
  421. c = connect(config);
  422. {
  423. /* Find out Redis version to determine the path for the next test */
  424. const char *field = "redis_version:";
  425. char *p, *eptr;
  426. reply = redisCommand(c,"INFO");
  427. p = strstr(reply->str,field);
  428. major = strtol(p+strlen(field),&eptr,10);
  429. p = eptr+1; /* char next to the first "." */
  430. minor = strtol(p,&eptr,10);
  431. freeReplyObject(reply);
  432. }
  433. test("Returns I/O error when the connection is lost: ");
  434. reply = redisCommand(c,"QUIT");
  435. if (major > 2 || (major == 2 && minor > 0)) {
  436. /* > 2.0 returns OK on QUIT and read() should be issued once more
  437. * to know the descriptor is at EOF. */
  438. test_cond(strcasecmp(reply->str,"OK") == 0 &&
  439. redisGetReply(c,&_reply) == REDIS_ERR);
  440. freeReplyObject(reply);
  441. } else {
  442. test_cond(reply == NULL);
  443. }
  444. /* On 2.0, QUIT will cause the connection to be closed immediately and
  445. * the read(2) for the reply on QUIT will set the error to EOF.
  446. * On >2.0, QUIT will return with OK and another read(2) needed to be
  447. * issued to find out the socket was closed by the server. In both
  448. * conditions, the error will be set to EOF. */
  449. assert(c->err == REDIS_ERR_EOF &&
  450. strcmp(c->errstr,"Server closed the connection") == 0);
  451. redisFree(c);
  452. c = connect(config);
  453. test("Returns I/O error on socket timeout: ");
  454. struct timeval tv = { 0, 1000 };
  455. assert(redisSetTimeout(c,tv) == REDIS_OK);
  456. test_cond(redisGetReply(c,&_reply) == REDIS_ERR &&
  457. c->err == REDIS_ERR_IO && errno == EAGAIN);
  458. redisFree(c);
  459. }
  460. static void test_invalid_timeout_errors(struct config config) {
  461. redisContext *c;
  462. test("Set error when an invalid timeout usec value is given to redisConnectWithTimeout: ");
  463. config.tcp.timeout.tv_sec = 0;
  464. config.tcp.timeout.tv_usec = 10000001;
  465. c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);
  466. test_cond(c->err == REDIS_ERR_IO);
  467. redisFree(c);
  468. test("Set error when an invalid timeout sec value is given to redisConnectWithTimeout: ");
  469. config.tcp.timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1;
  470. config.tcp.timeout.tv_usec = 0;
  471. c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);
  472. test_cond(c->err == REDIS_ERR_IO);
  473. redisFree(c);
  474. }
  475. static void test_throughput(struct config config) {
  476. redisContext *c = connect(config);
  477. redisReply **replies;
  478. int i, num;
  479. long long t1, t2;
  480. test("Throughput:\n");
  481. for (i = 0; i < 500; i++)
  482. freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
  483. num = 1000;
  484. replies = malloc(sizeof(redisReply*)*num);
  485. t1 = usec();
  486. for (i = 0; i < num; i++) {
  487. replies[i] = redisCommand(c,"PING");
  488. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
  489. }
  490. t2 = usec();
  491. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  492. free(replies);
  493. printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0);
  494. replies = malloc(sizeof(redisReply*)*num);
  495. t1 = usec();
  496. for (i = 0; i < num; i++) {
  497. replies[i] = redisCommand(c,"LRANGE mylist 0 499");
  498. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
  499. assert(replies[i] != NULL && replies[i]->elements == 500);
  500. }
  501. t2 = usec();
  502. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  503. free(replies);
  504. printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0);
  505. num = 10000;
  506. replies = malloc(sizeof(redisReply*)*num);
  507. for (i = 0; i < num; i++)
  508. redisAppendCommand(c,"PING");
  509. t1 = usec();
  510. for (i = 0; i < num; i++) {
  511. assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
  512. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
  513. }
  514. t2 = usec();
  515. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  516. free(replies);
  517. printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
  518. replies = malloc(sizeof(redisReply*)*num);
  519. for (i = 0; i < num; i++)
  520. redisAppendCommand(c,"LRANGE mylist 0 499");
  521. t1 = usec();
  522. for (i = 0; i < num; i++) {
  523. assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
  524. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
  525. assert(replies[i] != NULL && replies[i]->elements == 500);
  526. }
  527. t2 = usec();
  528. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  529. free(replies);
  530. printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
  531. disconnect(c, 0);
  532. }
  533. // static long __test_callback_flags = 0;
  534. // static void __test_callback(redisContext *c, void *privdata) {
  535. // ((void)c);
  536. // /* Shift to detect execution order */
  537. // __test_callback_flags <<= 8;
  538. // __test_callback_flags |= (long)privdata;
  539. // }
  540. //
  541. // static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {
  542. // ((void)c);
  543. // /* Shift to detect execution order */
  544. // __test_callback_flags <<= 8;
  545. // __test_callback_flags |= (long)privdata;
  546. // if (reply) freeReplyObject(reply);
  547. // }
  548. //
  549. // static redisContext *__connect_nonblock() {
  550. // /* Reset callback flags */
  551. // __test_callback_flags = 0;
  552. // return redisConnectNonBlock("127.0.0.1", port, NULL);
  553. // }
  554. //
  555. // static void test_nonblocking_connection() {
  556. // redisContext *c;
  557. // int wdone = 0;
  558. //
  559. // test("Calls command callback when command is issued: ");
  560. // c = __connect_nonblock();
  561. // redisSetCommandCallback(c,__test_callback,(void*)1);
  562. // redisCommand(c,"PING");
  563. // test_cond(__test_callback_flags == 1);
  564. // redisFree(c);
  565. //
  566. // test("Calls disconnect callback on redisDisconnect: ");
  567. // c = __connect_nonblock();
  568. // redisSetDisconnectCallback(c,__test_callback,(void*)2);
  569. // redisDisconnect(c);
  570. // test_cond(__test_callback_flags == 2);
  571. // redisFree(c);
  572. //
  573. // test("Calls disconnect callback and free callback on redisFree: ");
  574. // c = __connect_nonblock();
  575. // redisSetDisconnectCallback(c,__test_callback,(void*)2);
  576. // redisSetFreeCallback(c,__test_callback,(void*)4);
  577. // redisFree(c);
  578. // test_cond(__test_callback_flags == ((2 << 8) | 4));
  579. //
  580. // test("redisBufferWrite against empty write buffer: ");
  581. // c = __connect_nonblock();
  582. // test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
  583. // redisFree(c);
  584. //
  585. // test("redisBufferWrite against not yet connected fd: ");
  586. // c = __connect_nonblock();
  587. // redisCommand(c,"PING");
  588. // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
  589. // strncmp(c->error,"write:",6) == 0);
  590. // redisFree(c);
  591. //
  592. // test("redisBufferWrite against closed fd: ");
  593. // c = __connect_nonblock();
  594. // redisCommand(c,"PING");
  595. // redisDisconnect(c);
  596. // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
  597. // strncmp(c->error,"write:",6) == 0);
  598. // redisFree(c);
  599. //
  600. // test("Process callbacks in the right sequence: ");
  601. // c = __connect_nonblock();
  602. // redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING");
  603. // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
  604. // redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING");
  605. //
  606. // /* Write output buffer */
  607. // wdone = 0;
  608. // while(!wdone) {
  609. // usleep(500);
  610. // redisBufferWrite(c,&wdone);
  611. // }
  612. //
  613. // /* Read until at least one callback is executed (the 3 replies will
  614. // * arrive in a single packet, causing all callbacks to be executed in
  615. // * a single pass). */
  616. // while(__test_callback_flags == 0) {
  617. // assert(redisBufferRead(c) == REDIS_OK);
  618. // redisProcessCallbacks(c);
  619. // }
  620. // test_cond(__test_callback_flags == 0x010203);
  621. // redisFree(c);
  622. //
  623. // test("redisDisconnect executes pending callbacks with NULL reply: ");
  624. // c = __connect_nonblock();
  625. // redisSetDisconnectCallback(c,__test_callback,(void*)1);
  626. // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
  627. // redisDisconnect(c);
  628. // test_cond(__test_callback_flags == 0x0201);
  629. // redisFree(c);
  630. // }
  631. int main(int argc, char **argv) {
  632. struct config cfg = {
  633. .tcp = {
  634. .host = "127.0.0.1",
  635. .port = 6379
  636. },
  637. .unix = {
  638. .path = "/tmp/redis.sock"
  639. }
  640. };
  641. int throughput = 1;
  642. int test_inherit_fd = 1;
  643. /* Ignore broken pipe signal (for I/O error tests). */
  644. signal(SIGPIPE, SIG_IGN);
  645. /* Parse command line options. */
  646. argv++; argc--;
  647. while (argc) {
  648. if (argc >= 2 && !strcmp(argv[0],"-h")) {
  649. argv++; argc--;
  650. cfg.tcp.host = argv[0];
  651. } else if (argc >= 2 && !strcmp(argv[0],"-p")) {
  652. argv++; argc--;
  653. cfg.tcp.port = atoi(argv[0]);
  654. } else if (argc >= 2 && !strcmp(argv[0],"-s")) {
  655. argv++; argc--;
  656. cfg.unix.path = argv[0];
  657. } else if (argc >= 1 && !strcmp(argv[0],"--skip-throughput")) {
  658. throughput = 0;
  659. } else if (argc >= 1 && !strcmp(argv[0],"--skip-inherit-fd")) {
  660. test_inherit_fd = 0;
  661. } else {
  662. fprintf(stderr, "Invalid argument: %s\n", argv[0]);
  663. exit(1);
  664. }
  665. argv++; argc--;
  666. }
  667. test_format_commands();
  668. test_reply_reader();
  669. test_blocking_connection_errors();
  670. test_free_null();
  671. printf("\nTesting against TCP connection (%s:%d):\n", cfg.tcp.host, cfg.tcp.port);
  672. cfg.type = CONN_TCP;
  673. test_blocking_connection(cfg);
  674. test_blocking_connection_timeouts(cfg);
  675. test_blocking_io_errors(cfg);
  676. test_invalid_timeout_errors(cfg);
  677. test_append_formatted_commands(cfg);
  678. if (throughput) test_throughput(cfg);
  679. printf("\nTesting against Unix socket connection (%s):\n", cfg.unix.path);
  680. cfg.type = CONN_UNIX;
  681. test_blocking_connection(cfg);
  682. test_blocking_connection_timeouts(cfg);
  683. test_blocking_io_errors(cfg);
  684. if (throughput) test_throughput(cfg);
  685. if (test_inherit_fd) {
  686. printf("\nTesting against inherited fd (%s):\n", cfg.unix.path);
  687. cfg.type = CONN_FD;
  688. test_blocking_connection(cfg);
  689. }
  690. if (fails) {
  691. printf("*** %d TESTS FAILED ***\n", fails);
  692. return 1;
  693. }
  694. printf("ALL TESTS PASSED\n");
  695. return 0;
  696. }