threadsafe-hiredis 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/vendor/hiredis/sds.h DELETED
@@ -1,88 +0,0 @@
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
@@ -1,656 +0,0 @@
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
- int i;
208
-
209
- test("Error handling in reply parser: ");
210
- reader = redisReaderCreate();
211
- redisReaderFeed(reader,(char*)"@foo\r\n",6);
212
- ret = redisReaderGetReply(reader,NULL);
213
- test_cond(ret == REDIS_ERR &&
214
- strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0);
215
- redisReaderFree(reader);
216
-
217
- /* when the reply already contains multiple items, they must be free'd
218
- * on an error. valgrind will bark when this doesn't happen. */
219
- test("Memory cleanup in reply parser: ");
220
- reader = redisReaderCreate();
221
- redisReaderFeed(reader,(char*)"*2\r\n",4);
222
- redisReaderFeed(reader,(char*)"$5\r\nhello\r\n",11);
223
- redisReaderFeed(reader,(char*)"@foo\r\n",6);
224
- ret = redisReaderGetReply(reader,NULL);
225
- test_cond(ret == REDIS_ERR &&
226
- strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0);
227
- redisReaderFree(reader);
228
-
229
- test("Set error on nested multi bulks with depth > 7: ");
230
- reader = redisReaderCreate();
231
-
232
- for (i = 0; i < 9; i++) {
233
- redisReaderFeed(reader,(char*)"*1\r\n",4);
234
- }
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
-
241
- test("Works with NULL functions for reply: ");
242
- reader = redisReaderCreate();
243
- reader->fn = NULL;
244
- redisReaderFeed(reader,(char*)"+OK\r\n",5);
245
- ret = redisReaderGetReply(reader,&reply);
246
- test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
247
- redisReaderFree(reader);
248
-
249
- test("Works when a single newline (\\r\\n) covers two calls to feed: ");
250
- reader = redisReaderCreate();
251
- reader->fn = NULL;
252
- redisReaderFeed(reader,(char*)"+OK\r",4);
253
- ret = redisReaderGetReply(reader,&reply);
254
- assert(ret == REDIS_OK && reply == NULL);
255
- redisReaderFeed(reader,(char*)"\n",1);
256
- ret = redisReaderGetReply(reader,&reply);
257
- test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
258
- redisReaderFree(reader);
259
-
260
- test("Don't reset state after protocol error: ");
261
- reader = redisReaderCreate();
262
- reader->fn = NULL;
263
- redisReaderFeed(reader,(char*)"x",1);
264
- ret = redisReaderGetReply(reader,&reply);
265
- assert(ret == REDIS_ERR);
266
- ret = redisReaderGetReply(reader,&reply);
267
- test_cond(ret == REDIS_ERR && reply == NULL);
268
- redisReaderFree(reader);
269
-
270
- /* Regression test for issue #45 on GitHub. */
271
- test("Don't do empty allocation for empty multi bulk: ");
272
- reader = redisReaderCreate();
273
- redisReaderFeed(reader,(char*)"*0\r\n",4);
274
- ret = redisReaderGetReply(reader,&reply);
275
- test_cond(ret == REDIS_OK &&
276
- ((redisReply*)reply)->type == REDIS_REPLY_ARRAY &&
277
- ((redisReply*)reply)->elements == 0);
278
- freeReplyObject(reply);
279
- redisReaderFree(reader);
280
- }
281
-
282
- static void test_blocking_connection_errors(void) {
283
- redisContext *c;
284
-
285
- test("Returns error when host cannot be resolved: ");
286
- c = redisConnect((char*)"idontexist.local", 6379);
287
- test_cond(c->err == REDIS_ERR_OTHER &&
288
- (strcmp(c->errstr,"Name or service not known") == 0 ||
289
- strcmp(c->errstr,"Can't resolve: idontexist.local") == 0));
290
- redisFree(c);
291
-
292
- test("Returns error when the port is not open: ");
293
- c = redisConnect((char*)"localhost", 1);
294
- test_cond(c->err == REDIS_ERR_IO &&
295
- strcmp(c->errstr,"Connection refused") == 0);
296
- redisFree(c);
297
-
298
- test("Returns error when the unix socket path doesn't accept connections: ");
299
- c = redisConnectUnix((char*)"/tmp/idontexist.sock");
300
- test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */
301
- redisFree(c);
302
- }
303
-
304
- static void test_blocking_connection(struct config config) {
305
- redisContext *c;
306
- redisReply *reply;
307
-
308
- c = connect(config);
309
-
310
- test("Is able to deliver commands: ");
311
- reply = redisCommand(c,"PING");
312
- test_cond(reply->type == REDIS_REPLY_STATUS &&
313
- strcasecmp(reply->str,"pong") == 0)
314
- freeReplyObject(reply);
315
-
316
- test("Is a able to send commands verbatim: ");
317
- reply = redisCommand(c,"SET foo bar");
318
- test_cond (reply->type == REDIS_REPLY_STATUS &&
319
- strcasecmp(reply->str,"ok") == 0)
320
- freeReplyObject(reply);
321
-
322
- test("%%s String interpolation works: ");
323
- reply = redisCommand(c,"SET %s %s","foo","hello world");
324
- freeReplyObject(reply);
325
- reply = redisCommand(c,"GET foo");
326
- test_cond(reply->type == REDIS_REPLY_STRING &&
327
- strcmp(reply->str,"hello world") == 0);
328
- freeReplyObject(reply);
329
-
330
- test("%%b String interpolation works: ");
331
- reply = redisCommand(c,"SET %b %b","foo",3,"hello\x00world",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
-
337
- test("Binary reply length is correct: ");
338
- test_cond(reply->len == 11)
339
- freeReplyObject(reply);
340
-
341
- test("Can parse nil replies: ");
342
- reply = redisCommand(c,"GET nokey");
343
- test_cond(reply->type == REDIS_REPLY_NIL)
344
- freeReplyObject(reply);
345
-
346
- /* test 7 */
347
- test("Can parse integer replies: ");
348
- reply = redisCommand(c,"INCR mycounter");
349
- test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
350
- freeReplyObject(reply);
351
-
352
- test("Can parse multi bulk replies: ");
353
- freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
354
- freeReplyObject(redisCommand(c,"LPUSH mylist bar"));
355
- reply = redisCommand(c,"LRANGE mylist 0 -1");
356
- test_cond(reply->type == REDIS_REPLY_ARRAY &&
357
- reply->elements == 2 &&
358
- !memcmp(reply->element[0]->str,"bar",3) &&
359
- !memcmp(reply->element[1]->str,"foo",3))
360
- freeReplyObject(reply);
361
-
362
- /* m/e with multi bulk reply *before* other reply.
363
- * specifically test ordering of reply items to parse. */
364
- test("Can handle nested multi bulk replies: ");
365
- freeReplyObject(redisCommand(c,"MULTI"));
366
- freeReplyObject(redisCommand(c,"LRANGE mylist 0 -1"));
367
- freeReplyObject(redisCommand(c,"PING"));
368
- reply = (redisCommand(c,"EXEC"));
369
- test_cond(reply->type == REDIS_REPLY_ARRAY &&
370
- reply->elements == 2 &&
371
- reply->element[0]->type == REDIS_REPLY_ARRAY &&
372
- reply->element[0]->elements == 2 &&
373
- !memcmp(reply->element[0]->element[0]->str,"bar",3) &&
374
- !memcmp(reply->element[0]->element[1]->str,"foo",3) &&
375
- reply->element[1]->type == REDIS_REPLY_STATUS &&
376
- strcasecmp(reply->element[1]->str,"pong") == 0);
377
- freeReplyObject(reply);
378
-
379
- disconnect(c);
380
- }
381
-
382
- static void test_blocking_io_errors(struct config config) {
383
- redisContext *c;
384
- redisReply *reply;
385
- void *_reply;
386
- int major, minor;
387
-
388
- /* Connect to target given by config. */
389
- c = connect(config);
390
- {
391
- /* Find out Redis version to determine the path for the next test */
392
- const char *field = "redis_version:";
393
- char *p, *eptr;
394
-
395
- reply = redisCommand(c,"INFO");
396
- p = strstr(reply->str,field);
397
- major = strtol(p+strlen(field),&eptr,10);
398
- p = eptr+1; /* char next to the first "." */
399
- minor = strtol(p,&eptr,10);
400
- freeReplyObject(reply);
401
- }
402
-
403
- test("Returns I/O error when the connection is lost: ");
404
- reply = redisCommand(c,"QUIT");
405
- if (major >= 2 && minor > 0) {
406
- /* > 2.0 returns OK on QUIT and read() should be issued once more
407
- * to know the descriptor is at EOF. */
408
- test_cond(strcasecmp(reply->str,"OK") == 0 &&
409
- redisGetReply(c,&_reply) == REDIS_ERR);
410
- freeReplyObject(reply);
411
- } else {
412
- test_cond(reply == NULL);
413
- }
414
-
415
- /* On 2.0, QUIT will cause the connection to be closed immediately and
416
- * the read(2) for the reply on QUIT will set the error to EOF.
417
- * On >2.0, QUIT will return with OK and another read(2) needed to be
418
- * issued to find out the socket was closed by the server. In both
419
- * conditions, the error will be set to EOF. */
420
- assert(c->err == REDIS_ERR_EOF &&
421
- strcmp(c->errstr,"Server closed the connection") == 0);
422
- redisFree(c);
423
-
424
- c = connect(config);
425
- test("Returns I/O error on socket timeout: ");
426
- struct timeval tv = { 0, 1000 };
427
- assert(redisSetTimeout(c,tv) == REDIS_OK);
428
- test_cond(redisGetReply(c,&_reply) == REDIS_ERR &&
429
- c->err == REDIS_ERR_IO && errno == EAGAIN);
430
- redisFree(c);
431
- }
432
-
433
- static void test_throughput(struct config config) {
434
- redisContext *c = connect(config);
435
- redisReply **replies;
436
- int i, num;
437
- long long t1, t2;
438
-
439
- test("Throughput:\n");
440
- for (i = 0; i < 500; i++)
441
- freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
442
-
443
- num = 1000;
444
- replies = malloc(sizeof(redisReply*)*num);
445
- t1 = usec();
446
- for (i = 0; i < num; i++) {
447
- replies[i] = redisCommand(c,"PING");
448
- assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
449
- }
450
- t2 = usec();
451
- for (i = 0; i < num; i++) freeReplyObject(replies[i]);
452
- free(replies);
453
- printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0);
454
-
455
- replies = malloc(sizeof(redisReply*)*num);
456
- t1 = usec();
457
- for (i = 0; i < num; i++) {
458
- replies[i] = redisCommand(c,"LRANGE mylist 0 499");
459
- assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
460
- assert(replies[i] != NULL && replies[i]->elements == 500);
461
- }
462
- t2 = usec();
463
- for (i = 0; i < num; i++) freeReplyObject(replies[i]);
464
- free(replies);
465
- printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0);
466
-
467
- num = 10000;
468
- replies = malloc(sizeof(redisReply*)*num);
469
- for (i = 0; i < num; i++)
470
- redisAppendCommand(c,"PING");
471
- t1 = usec();
472
- for (i = 0; i < num; i++) {
473
- assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
474
- assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
475
- }
476
- t2 = usec();
477
- for (i = 0; i < num; i++) freeReplyObject(replies[i]);
478
- free(replies);
479
- printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
480
-
481
- replies = malloc(sizeof(redisReply*)*num);
482
- for (i = 0; i < num; i++)
483
- redisAppendCommand(c,"LRANGE mylist 0 499");
484
- t1 = usec();
485
- for (i = 0; i < num; i++) {
486
- assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
487
- assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
488
- assert(replies[i] != NULL && replies[i]->elements == 500);
489
- }
490
- t2 = usec();
491
- for (i = 0; i < num; i++) freeReplyObject(replies[i]);
492
- free(replies);
493
- printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
494
-
495
- disconnect(c);
496
- }
497
-
498
- // static long __test_callback_flags = 0;
499
- // static void __test_callback(redisContext *c, void *privdata) {
500
- // ((void)c);
501
- // /* Shift to detect execution order */
502
- // __test_callback_flags <<= 8;
503
- // __test_callback_flags |= (long)privdata;
504
- // }
505
- //
506
- // static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {
507
- // ((void)c);
508
- // /* Shift to detect execution order */
509
- // __test_callback_flags <<= 8;
510
- // __test_callback_flags |= (long)privdata;
511
- // if (reply) freeReplyObject(reply);
512
- // }
513
- //
514
- // static redisContext *__connect_nonblock() {
515
- // /* Reset callback flags */
516
- // __test_callback_flags = 0;
517
- // return redisConnectNonBlock("127.0.0.1", port, NULL);
518
- // }
519
- //
520
- // static void test_nonblocking_connection() {
521
- // redisContext *c;
522
- // int wdone = 0;
523
- //
524
- // test("Calls command callback when command is issued: ");
525
- // c = __connect_nonblock();
526
- // redisSetCommandCallback(c,__test_callback,(void*)1);
527
- // redisCommand(c,"PING");
528
- // test_cond(__test_callback_flags == 1);
529
- // redisFree(c);
530
- //
531
- // test("Calls disconnect callback on redisDisconnect: ");
532
- // c = __connect_nonblock();
533
- // redisSetDisconnectCallback(c,__test_callback,(void*)2);
534
- // redisDisconnect(c);
535
- // test_cond(__test_callback_flags == 2);
536
- // redisFree(c);
537
- //
538
- // test("Calls disconnect callback and free callback on redisFree: ");
539
- // c = __connect_nonblock();
540
- // redisSetDisconnectCallback(c,__test_callback,(void*)2);
541
- // redisSetFreeCallback(c,__test_callback,(void*)4);
542
- // redisFree(c);
543
- // test_cond(__test_callback_flags == ((2 << 8) | 4));
544
- //
545
- // test("redisBufferWrite against empty write buffer: ");
546
- // c = __connect_nonblock();
547
- // test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
548
- // redisFree(c);
549
- //
550
- // test("redisBufferWrite against not yet connected fd: ");
551
- // c = __connect_nonblock();
552
- // redisCommand(c,"PING");
553
- // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
554
- // strncmp(c->error,"write:",6) == 0);
555
- // redisFree(c);
556
- //
557
- // test("redisBufferWrite against closed fd: ");
558
- // c = __connect_nonblock();
559
- // redisCommand(c,"PING");
560
- // redisDisconnect(c);
561
- // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
562
- // strncmp(c->error,"write:",6) == 0);
563
- // redisFree(c);
564
- //
565
- // test("Process callbacks in the right sequence: ");
566
- // c = __connect_nonblock();
567
- // redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING");
568
- // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
569
- // redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING");
570
- //
571
- // /* Write output buffer */
572
- // wdone = 0;
573
- // while(!wdone) {
574
- // usleep(500);
575
- // redisBufferWrite(c,&wdone);
576
- // }
577
- //
578
- // /* Read until at least one callback is executed (the 3 replies will
579
- // * arrive in a single packet, causing all callbacks to be executed in
580
- // * a single pass). */
581
- // while(__test_callback_flags == 0) {
582
- // assert(redisBufferRead(c) == REDIS_OK);
583
- // redisProcessCallbacks(c);
584
- // }
585
- // test_cond(__test_callback_flags == 0x010203);
586
- // redisFree(c);
587
- //
588
- // test("redisDisconnect executes pending callbacks with NULL reply: ");
589
- // c = __connect_nonblock();
590
- // redisSetDisconnectCallback(c,__test_callback,(void*)1);
591
- // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
592
- // redisDisconnect(c);
593
- // test_cond(__test_callback_flags == 0x0201);
594
- // redisFree(c);
595
- // }
596
-
597
- int main(int argc, char **argv) {
598
- struct config cfg = {
599
- .tcp = {
600
- .host = "127.0.0.1",
601
- .port = 6379
602
- },
603
- .unix = {
604
- .path = "/tmp/redis.sock"
605
- }
606
- };
607
- int throughput = 1;
608
-
609
- /* Ignore broken pipe signal (for I/O error tests). */
610
- signal(SIGPIPE, SIG_IGN);
611
-
612
- /* Parse command line options. */
613
- argv++; argc--;
614
- while (argc) {
615
- if (argc >= 2 && !strcmp(argv[0],"-h")) {
616
- argv++; argc--;
617
- cfg.tcp.host = argv[0];
618
- } else if (argc >= 2 && !strcmp(argv[0],"-p")) {
619
- argv++; argc--;
620
- cfg.tcp.port = atoi(argv[0]);
621
- } else if (argc >= 2 && !strcmp(argv[0],"-s")) {
622
- argv++; argc--;
623
- cfg.unix.path = argv[0];
624
- } else if (argc >= 1 && !strcmp(argv[0],"--skip-throughput")) {
625
- throughput = 0;
626
- } else {
627
- fprintf(stderr, "Invalid argument: %s\n", argv[0]);
628
- exit(1);
629
- }
630
- argv++; argc--;
631
- }
632
-
633
- test_format_commands();
634
- test_reply_reader();
635
- test_blocking_connection_errors();
636
-
637
- printf("\nTesting against TCP connection (%s:%d):\n", cfg.tcp.host, cfg.tcp.port);
638
- cfg.type = CONN_TCP;
639
- test_blocking_connection(cfg);
640
- test_blocking_io_errors(cfg);
641
- if (throughput) test_throughput(cfg);
642
-
643
- printf("\nTesting against Unix socket connection (%s):\n", cfg.unix.path);
644
- cfg.type = CONN_UNIX;
645
- test_blocking_connection(cfg);
646
- test_blocking_io_errors(cfg);
647
- if (throughput) test_throughput(cfg);
648
-
649
- if (fails) {
650
- printf("*** %d TESTS FAILED ***\n", fails);
651
- return 1;
652
- }
653
-
654
- printf("ALL TESTS PASSED\n");
655
- return 0;
656
- }