durable_rules 0.31.1

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