methodmissing_hiredis 0.4.6

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