Allocation fixes in string library (now we can run bigger tests!)
[awesomized/libmemcached] / tests / test.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <memcached.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/time.h>
10 #include <time.h>
11 #include "../lib/common.h"
12
13 long int timedif(struct timeval a, struct timeval b)
14 {
15 register int us, s;
16
17 us = a.tv_usec - b.tv_usec;
18 us /= 1000;
19 s = a.tv_sec - b.tv_sec;
20 s *= 1000;
21 return s + us;
22 }
23
24 void init_test(memcached_st *not_used)
25 {
26 memcached_st memc;
27
28 (void)memcached_create(&memc);
29 memcached_free(&memc);
30 }
31
32 void allocation_test(memcached_st *not_used)
33 {
34 memcached_st *memc;
35 memc= memcached_create(NULL);
36 assert(memc);
37 memcached_free(memc);
38 }
39
40 void connection_test(memcached_st *memc)
41 {
42 memcached_return rc;
43
44 rc= memcached_server_add(memc, "localhost", 0);
45 assert(rc == MEMCACHED_SUCCESS);
46 }
47
48 void error_test(memcached_st *memc)
49 {
50 memcached_return rc;
51
52 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
53 {
54 printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc));
55 }
56 }
57
58 void set_test(memcached_st *memc)
59 {
60 memcached_return rc;
61 char *key= "foo";
62 char *value= "when we sanitize";
63
64 rc= memcached_set(memc, key, strlen(key),
65 value, strlen(value),
66 (time_t)0, (uint16_t)0);
67 assert(rc == MEMCACHED_SUCCESS);
68 }
69
70 void add_test(memcached_st *memc)
71 {
72 memcached_return rc;
73 char *key= "foo";
74 char *value= "when we sanitize";
75
76 rc= memcached_add(memc, key, strlen(key),
77 value, strlen(value),
78 (time_t)0, (uint16_t)0);
79 assert(rc == MEMCACHED_NOTSTORED);
80 }
81
82 void replace_test(memcached_st *memc)
83 {
84 memcached_return rc;
85 char *key= "foo";
86 char *value= "when we sanitize";
87
88 rc= memcached_replace(memc, key, strlen(key),
89 value, strlen(value),
90 (time_t)0, (uint16_t)0);
91 assert(rc == MEMCACHED_SUCCESS);
92 }
93
94 void delete_test(memcached_st *memc)
95 {
96 memcached_return rc;
97 char *key= "foo";
98 char *value= "when we sanitize";
99
100 rc= memcached_set(memc, key, strlen(key),
101 value, strlen(value),
102 (time_t)0, (uint16_t)0);
103 assert(rc == MEMCACHED_SUCCESS);
104
105 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
106 assert(rc == MEMCACHED_SUCCESS);
107 }
108
109 void flush_test(memcached_st *memc)
110 {
111 memcached_return rc;
112
113 rc= memcached_flush(memc, 0);
114 assert(rc == MEMCACHED_SUCCESS);
115 }
116
117 void get_test(memcached_st *memc)
118 {
119 memcached_return rc;
120 char *key= "foo";
121 char *string;
122 size_t string_length;
123 uint16_t flags;
124
125 string= memcached_get(memc, key, strlen(key),
126 &string_length, &flags, &rc);
127
128 assert(rc == MEMCACHED_NOTFOUND);
129 assert(string_length == 0);
130 assert(!string);
131 }
132
133 void get_test2(memcached_st *memc)
134 {
135 memcached_return rc;
136 char *key= "foo";
137 char *value= "when we sanitize";
138 char *string;
139 size_t string_length;
140 uint16_t flags;
141
142 rc= memcached_set(memc, key, strlen(key),
143 value, strlen(value),
144 (time_t)0, (uint16_t)0);
145 assert(rc == MEMCACHED_SUCCESS);
146
147 string= memcached_get(memc, key, strlen(key),
148 &string_length, &flags, &rc);
149
150 assert(string);
151 assert(rc == MEMCACHED_SUCCESS);
152 assert(string_length == strlen(value));
153 assert(!memcmp(string, value, string_length));
154
155 free(string);
156 }
157
158 void set_test2(memcached_st *memc)
159 {
160 memcached_return rc;
161 char *key= "foo";
162 char *value= "train in the brain";
163 size_t value_length= strlen(value);
164 unsigned int x;
165
166 for (x= 0; x < 10; x++)
167 {
168 rc= memcached_set(memc, key, strlen(key),
169 value, value_length,
170 (time_t)0, (uint16_t)0);
171 assert(rc == MEMCACHED_SUCCESS);
172 }
173 }
174
175 void set_test3(memcached_st *memc)
176 {
177 memcached_return rc;
178 char *key= "foo";
179 char *value;
180 size_t value_length= 8191;
181 unsigned int x;
182
183 value = (char*)malloc(value_length);
184 assert(value);
185
186 for (x= 0; x < value_length; x++)
187 value[x] = (char) (x % 127);
188
189 for (x= 0; x < 1; x++)
190 {
191 rc= memcached_set(memc, key, strlen(key),
192 value, value_length,
193 (time_t)0, (uint16_t)0);
194 assert(rc == MEMCACHED_SUCCESS);
195 }
196
197 free(value);
198 }
199
200 void get_test3(memcached_st *memc)
201 {
202 memcached_return rc;
203 char *key= "foo";
204 char *value;
205 size_t value_length= 8191;
206 char *string;
207 size_t string_length;
208 uint16_t flags;
209 int x;
210
211 value = (char*)malloc(value_length);
212 assert(value);
213
214 for (x= 0; x < value_length; x++)
215 value[x] = (char) (x % 127);
216
217 rc= memcached_set(memc, key, strlen(key),
218 value, value_length,
219 (time_t)0, (uint16_t)0);
220 assert(rc == MEMCACHED_SUCCESS);
221
222 string= memcached_get(memc, key, strlen(key),
223 &string_length, &flags, &rc);
224
225 assert(rc == MEMCACHED_SUCCESS);
226 assert(string);
227 assert(string_length == value_length);
228 assert(!memcmp(string, value, string_length));
229
230 free(string);
231 free(value);
232 }
233
234 void get_test4(memcached_st *memc)
235 {
236 memcached_return rc;
237 char *key= "foo";
238 char *value;
239 size_t value_length= 8191;
240 char *string;
241 size_t string_length;
242 uint16_t flags;
243 int x;
244
245 value = (char*)malloc(value_length);
246 assert(value);
247
248 for (x= 0; x < value_length; x++)
249 value[x] = (char) (x % 127);
250
251 rc= memcached_set(memc, key, strlen(key),
252 value, value_length,
253 (time_t)0, (uint16_t)0);
254 assert(rc == MEMCACHED_SUCCESS);
255
256 for (x= 0; x < 10; x++)
257 {
258 string= memcached_get(memc, key, strlen(key),
259 &string_length, &flags, &rc);
260
261 assert(rc == MEMCACHED_SUCCESS);
262 assert(string);
263 assert(string_length == value_length);
264 assert(!memcmp(string, value, string_length));
265 free(string);
266 }
267
268 free(value);
269 }
270
271 void stats_servername_test(memcached_st *memc)
272 {
273 memcached_return rc;
274 memcached_stat_st stat;
275 rc= memcached_stat_servername(&stat, NULL,
276 "localhost",
277 MEMCACHED_DEFAULT_PORT);
278 }
279
280 void increment_test(memcached_st *memc)
281 {
282 unsigned int new_number;
283 memcached_return rc;
284 char *key= "number";
285 char *value= "0";
286
287 rc= memcached_set(memc, key, strlen(key),
288 value, strlen(value),
289 (time_t)0, (uint16_t)0);
290 assert(rc == MEMCACHED_SUCCESS);
291
292 rc= memcached_increment(memc, key, strlen(key),
293 1, &new_number);
294 assert(rc == MEMCACHED_SUCCESS);
295 assert(new_number == 1);
296
297 rc= memcached_increment(memc, key, strlen(key),
298 1, &new_number);
299 assert(rc == MEMCACHED_SUCCESS);
300 assert(new_number == 2);
301 }
302
303 void decrement_test(memcached_st *memc)
304 {
305 unsigned int new_number;
306 memcached_return rc;
307 char *key= "number";
308 char *value= "3";
309
310 rc= memcached_set(memc, key, strlen(key),
311 value, strlen(value),
312 (time_t)0, (uint16_t)0);
313 assert(rc == MEMCACHED_SUCCESS);
314
315 rc= memcached_decrement(memc, key, strlen(key),
316 1, &new_number);
317 assert(rc == MEMCACHED_SUCCESS);
318 assert(new_number == 2);
319
320 rc= memcached_decrement(memc, key, strlen(key),
321 1, &new_number);
322 assert(rc == MEMCACHED_SUCCESS);
323 assert(new_number == 1);
324 }
325
326 void quit_test(memcached_st *memc)
327 {
328 memcached_return rc;
329 char *key= "fudge";
330 char *value= "sanford and sun";
331
332 rc= memcached_set(memc, key, strlen(key),
333 value, strlen(value),
334 (time_t)10, (uint16_t)3);
335 assert(rc == MEMCACHED_SUCCESS);
336 memcached_quit(memc);
337
338 rc= memcached_set(memc, key, strlen(key),
339 value, strlen(value),
340 (time_t)50, (uint16_t)9);
341 assert(rc == MEMCACHED_SUCCESS);
342 }
343
344 void mget_test(memcached_st *memc)
345 {
346 memcached_return rc;
347 char *keys[]= {"fudge", "son", "food"};
348 size_t key_length[]= {5, 3, 4};
349 unsigned int x;
350 uint16_t flags;
351
352 char return_key[MEMCACHED_MAX_KEY];
353 size_t return_key_length;
354 char *return_value;
355 size_t return_value_length;
356
357 /* We need to empty the server before continueing test */
358 rc= memcached_flush(memc, 0);
359 assert(rc == MEMCACHED_SUCCESS);
360
361 rc= memcached_mget(memc, keys, key_length, 3);
362 assert(rc == MEMCACHED_SUCCESS);
363
364 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
365 &return_value_length, &flags, &rc)) != NULL)
366 {
367 assert(return_value);
368 }
369 assert(!return_value);
370 assert(return_value_length == 0);
371 assert(rc == MEMCACHED_NOTFOUND);
372
373 for (x= 0; x < 3; x++)
374 {
375 rc= memcached_set(memc, keys[x], key_length[x],
376 keys[x], key_length[x],
377 (time_t)50, (uint16_t)9);
378 assert(rc == MEMCACHED_SUCCESS);
379 }
380
381 rc= memcached_mget(memc, keys, key_length, 3);
382 assert(rc == MEMCACHED_SUCCESS);
383
384 x= 0;
385 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
386 &return_value_length, &flags, &rc)))
387 {
388 assert(return_value);
389 assert(rc == MEMCACHED_SUCCESS);
390 assert(return_key_length == return_value_length);
391 assert(!memcmp(return_value, return_key, return_value_length));
392 free(return_value);
393 x++;
394 }
395 }
396
397 void get_stats_keys(memcached_st *memc)
398 {
399 char **list;
400 char **ptr;
401 memcached_stat_st stat;
402 memcached_return rc;
403
404 list= memcached_stat_get_keys(memc, &stat, &rc);
405 assert(rc == MEMCACHED_SUCCESS);
406 for (ptr= list; *ptr; ptr++)
407 printf("Found key %s\n", *ptr);
408 fflush(stdout);
409
410 free(list);
411 }
412
413 void get_stats(memcached_st *memc)
414 {
415 unsigned int x;
416 char **list;
417 char **ptr;
418 memcached_return rc;
419 memcached_stat_st *stat;
420
421 stat= memcached_stat(memc, NULL, &rc);
422 assert(rc == MEMCACHED_SUCCESS);
423
424 assert(rc == MEMCACHED_SUCCESS);
425 assert(stat);
426
427 for (x= 0; x < memcached_server_count(memc); x++)
428 {
429 list= memcached_stat_get_keys(memc, &stat[x], &rc);
430 assert(rc == MEMCACHED_SUCCESS);
431 for (ptr= list; *ptr; ptr++)
432 printf("Found key %s\n", *ptr);
433
434 free(list);
435 }
436
437 free(stat);
438 }
439
440 void add_host_test(memcached_st *memc)
441 {
442 unsigned int x;
443 memcached_server_st *servers;
444 memcached_return rc;
445 char servername[]= "0.example.com";
446
447 servers= memcached_server_list_append(NULL, servername, 400, &rc);
448 assert(servers);
449 assert(1 == memcached_server_list_count(servers));
450
451 for (x= 2; x < 20; x++)
452 {
453 char buffer[SMALL_STRING_LEN];
454
455 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
456 servers= memcached_server_list_append(servers, buffer, 401,
457 &rc);
458 assert(rc == MEMCACHED_SUCCESS);
459 assert(x == memcached_server_list_count(servers));
460 }
461
462 rc= memcached_server_push(memc, servers);
463 assert(rc == MEMCACHED_SUCCESS);
464 rc= memcached_server_push(memc, servers);
465 assert(rc == MEMCACHED_SUCCESS);
466
467 memcached_server_list_free(servers);
468 }
469
470 /* We don't test the behavior itself, we test the switches */
471 void behavior_test(memcached_st *memc)
472 {
473 unsigned long long value;
474 unsigned int set= 1;
475
476 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
477 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
478 assert(value == 1);
479
480 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
481 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
482 assert(value == 1);
483
484 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_MD5_HASHING, &set);
485 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_MD5_HASHING);
486 assert(value == 1);
487
488 set= 0;
489
490 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
491 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
492 assert(value == 0);
493
494 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
495 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
496 assert(value == 0);
497
498 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_MD5_HASHING, &set);
499 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_MD5_HASHING);
500 assert(value == 0);
501
502 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
503 assert(value > 0);
504
505 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
506 assert(value > 0);
507 }
508
509 /* Test case provided by Cal Haldenbrand */
510 void user_supplied_bug1(memcached_st *memc)
511 {
512 unsigned int setter= 1;
513 unsigned int x;
514
515 unsigned long long total= 0;
516 int size= 0;
517 char key[10];
518 char randomstuff[6 * 1024];
519 memcached_return rc;
520
521 memset(randomstuff, 0, 6 * 1024);
522
523 /* We just keep looking at the same values over and over */
524 srandom(10);
525
526 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
527 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
528
529
530 /* add key */
531 for (x= 0 ; total < 20 * 1024576 ; x++ )
532 {
533 unsigned int j= 0;
534
535 size= (rand() % ( 5 * 1024 ) ) + 400;
536 memset(randomstuff, 0, 6 * 1024);
537 assert(size < 6 * 1024); /* Being safe here */
538
539 for (j= 0 ; j < size ;j++)
540 randomstuff[j] = (char) (rand() % 26) + 97;
541
542 total += size;
543 sprintf(key, "%d", x);
544 rc = memcached_set(memc, key, strlen(key),
545 randomstuff, strlen(randomstuff), 10, 0);
546 /* If we fail, lets try again */
547 if (rc != MEMCACHED_SUCCESS)
548 rc = memcached_set(memc, key, strlen(key),
549 randomstuff, strlen(randomstuff), 10, 0);
550 assert(rc == MEMCACHED_SUCCESS);
551 }
552 }
553
554 /* Test case provided by Cal Haldenbrand */
555 void user_supplied_bug2(memcached_st *memc)
556 {
557 int errors;
558 unsigned int setter;
559 unsigned int x;
560 unsigned long long total;
561
562 setter= 1;
563 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
564 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
565 #ifdef NOT_YET
566 setter = 20 * 1024576;
567 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
568 setter = 20 * 1024576;
569 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
570 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
571 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
572 #endif
573
574 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
575 {
576 memcached_return rc= MEMCACHED_SUCCESS;
577 char buffer[SMALL_STRING_LEN];
578 uint16_t flags= 0;
579 size_t val_len= 0;
580 char *getval;
581
582 memset(buffer, 0, SMALL_STRING_LEN);
583
584 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
585 getval= memcached_get(memc, buffer, strlen(buffer),
586 &val_len, &flags, &rc);
587 if (rc != MEMCACHED_SUCCESS)
588 {
589 if (rc == MEMCACHED_NOTFOUND)
590 errors++;
591 else
592 assert(0);
593
594 continue;
595 }
596 total+= val_len;
597 errors= 0;
598 free(getval);
599 }
600 }
601
602 #define KEY_COUNT 2000 // * 1024576
603 void user_supplied_bug3(memcached_st *memc)
604 {
605 memcached_return rc;
606 unsigned int setter;
607 unsigned int x;
608 char **keys;
609 size_t key_lengths[KEY_COUNT];
610
611 setter= 1;
612 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
613 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
614 #ifdef NOT_YET
615 setter = 20 * 1024576;
616 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
617 setter = 20 * 1024576;
618 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
619 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
620 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
621 #endif
622
623 keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
624 assert(keys);
625 memset(keys, 0, (sizeof(char *) * KEY_COUNT));
626 for (x= 0; x < KEY_COUNT; x++)
627 {
628 char buffer[20];
629
630 snprintf(buffer, 30, "%u", x);
631 keys[x]= strdup(buffer);
632 key_lengths[x]= strlen(keys[x]);
633 }
634
635 rc= memcached_mget(memc, keys, key_lengths, KEY_COUNT);
636 assert(rc == MEMCACHED_SUCCESS);
637
638 /* Turn this into a help function */
639 {
640 char *return_key;
641 size_t return_key_length;
642 char *return_value;
643 size_t return_value_length;
644 uint16_t flags;
645
646 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
647 &return_value_length, &flags, &rc)))
648 {
649 assert(return_value);
650 assert(rc == MEMCACHED_SUCCESS);
651 free(return_value);
652 }
653 }
654
655 for (x= 0; x < KEY_COUNT; x++)
656 free(keys[x]);
657 free(keys);
658 }
659
660 void string_alloc_null(memcached_st *memc)
661 {
662 memcached_string_st *string;
663
664 string= memcached_string_create(memc, 0);
665 assert(string);
666 memcached_string_free(memc, string);
667 }
668
669 void string_alloc_with_size(memcached_st *memc)
670 {
671 memcached_string_st *string;
672
673 string= memcached_string_create(memc, 1024);
674 assert(string);
675 memcached_string_free(memc, string);
676 }
677
678 void string_alloc_with_size_toobig(memcached_st *memc)
679 {
680 memcached_string_st *string;
681
682 string= memcached_string_create(memc, 1024*100000000000);
683 assert(string == NULL);
684 }
685
686 void string_alloc_append(memcached_st *memc)
687 {
688 unsigned int x;
689 char buffer[SMALL_STRING_LEN];
690 memcached_string_st *string;
691
692 /* Ring the bell! */
693 memset(buffer, 6, SMALL_STRING_LEN);
694
695 string= memcached_string_create(memc, 100);
696 assert(string);
697
698 for (x= 0; x < 1024; x++)
699 {
700 memcached_return rc;
701 rc= memcached_string_append(memc, string, buffer, SMALL_STRING_LEN);
702 assert(rc == MEMCACHED_SUCCESS);
703 }
704 memcached_string_free(memc, string);
705 }
706
707 void string_alloc_append_toobig(memcached_st *memc)
708 {
709 memcached_return rc;
710 unsigned int x;
711 char buffer[SMALL_STRING_LEN];
712 memcached_string_st *string;
713
714 /* Ring the bell! */
715 memset(buffer, 6, SMALL_STRING_LEN);
716
717 string= memcached_string_create(memc, 100);
718 assert(string);
719
720 for (x= 0; x < 1024; x++)
721 {
722 rc= memcached_string_append(memc, string, buffer, SMALL_STRING_LEN);
723 assert(rc == MEMCACHED_SUCCESS);
724 }
725 rc= memcached_string_append(memc, string, buffer, 1024*100000000000);
726 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
727 memcached_string_free(memc, string);
728 }
729
730 void add_host_test1(memcached_st *memc)
731 {
732 unsigned int x;
733 memcached_return rc;
734 char servername[]= "0.example.com";
735 memcached_server_st *servers;
736
737 servers= memcached_server_list_append(NULL, servername, 400, &rc);
738 assert(servers);
739 assert(1 == memcached_server_list_count(servers));
740
741 for (x= 2; x < 20; x++)
742 {
743 char buffer[SMALL_STRING_LEN];
744
745 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
746 servers= memcached_server_list_append(servers, buffer, 401,
747 &rc);
748 assert(rc == MEMCACHED_SUCCESS);
749 assert(x == memcached_server_list_count(servers));
750 }
751
752 rc= memcached_server_push(memc, servers);
753 assert(rc == MEMCACHED_SUCCESS);
754 rc= memcached_server_push(memc, servers);
755 assert(rc == MEMCACHED_SUCCESS);
756
757 memcached_server_list_free(servers);
758 }
759
760 typedef struct test_st test_st;
761
762 struct test_st {
763 char *function_name;
764 unsigned int requires_flush;
765 void (*function)(memcached_st *memc);
766 };
767
768 int main(int argc, char *argv[])
769 {
770 unsigned int x;
771 char *server_list;
772 char *test_to_run= NULL;
773 char *wildcard= NULL;
774 memcached_server_st *servers;
775
776 if (argc > 2)
777 test_to_run= argv[1];
778
779 if (argc == 3)
780 wildcard= argv[2];
781
782 if (!(server_list= getenv("MEMCACHED_SERVERS")))
783 server_list= "localhost";
784
785 printf("servers %s\n", server_list);
786 srandom(time(NULL));
787
788 servers= memcached_servers_parse(server_list);
789 assert(servers);
790
791 for (x= 0; x < memcached_server_list_count(servers); x++)
792 {
793 printf("\t%s : %u\n", servers[x].hostname, servers[x].port);
794 assert(servers[x].stack_responses == 0);
795 assert(servers[x].fd == -1);
796 assert(servers[x].cursor_active == 0);
797 }
798
799 printf("\n");
800
801 /* Clean the server before beginning testing */
802 test_st tests[] ={
803 {"flush", 0, flush_test },
804 {"init", 0, init_test },
805 {"allocation", 0, allocation_test },
806 {"error", 0, error_test },
807 {"set", 0, set_test },
808 {"set2", 0, set_test2 },
809 {"set3", 0, set_test3 },
810 {"add", 0, add_test },
811 {"replace", 0, replace_test },
812 {"delete", 1, delete_test },
813 {"get", 1, get_test },
814 {"get2", 0, get_test2 },
815 {"get3", 0, get_test3 },
816 {"get4", 0, get_test4 },
817 {"stats_servername", 0, stats_servername_test },
818 {"increment", 0, increment_test },
819 {"decrement", 0, decrement_test },
820 {"quit", 0, quit_test },
821 {"mget", 0, mget_test },
822 {"get_stats", 0, get_stats },
823 {"add_host_test", 0, add_host_test },
824 {"get_stats_keys", 0, get_stats_keys },
825 {"behavior_test", 0, get_stats_keys },
826 {0, 0, 0}
827 };
828
829 test_st string_tests[] ={
830 {"string alloc with null", 0, string_alloc_null },
831 {"string alloc with 1K", 0, string_alloc_with_size },
832 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
833 {"string append", 0, string_alloc_append },
834 {"string append failure (too big)", 0, string_alloc_append_toobig },
835 {0, 0, 0}
836 };
837
838 test_st user_tests[] ={
839 {"user_supplied_bug1", 0, user_supplied_bug1 },
840 {"user_supplied_bug2", 0, user_supplied_bug2 },
841 // {"user_supplied_bug3", 0, user_supplied_bug3 },
842 {0, 0, 0}
843 };
844
845 if ((test_to_run && !strcmp(test_to_run, "block")) || !test_to_run)
846 {
847 fprintf(stderr, "\nBlock tests\n\n");
848 for (x= 0; tests[x].function_name; x++)
849 {
850 if (wildcard)
851 if (strcmp(wildcard, tests[x].function_name))
852 continue;
853
854 memcached_st *memc;
855 memcached_return rc;
856 struct timeval start_time, end_time;
857
858 memc= memcached_create(NULL);
859 assert(memc);
860
861 if (tests[x].requires_flush)
862 memcached_flush(memc, 0);
863
864 rc= memcached_server_push(memc, servers);
865 assert(rc == MEMCACHED_SUCCESS);
866
867 unsigned int loop;
868 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
869 {
870 assert(memc->hosts[loop].stack_responses == 0);
871 assert(memc->hosts[loop].fd == -1);
872 assert(memc->hosts[loop].cursor_active == 0);
873 }
874
875 fprintf(stderr, "Testing %s", tests[x].function_name);
876 gettimeofday(&start_time, NULL);
877 tests[x].function(memc);
878 gettimeofday(&end_time, NULL);
879 long int load_time= timedif(end_time, start_time);
880 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
881 load_time % 1000);
882 assert(memc);
883 memcached_free(memc);
884 }
885 }
886
887 if ((test_to_run && !strcmp(test_to_run, "nonblock")) || !test_to_run)
888 {
889 fprintf(stderr, "\nNonblock tests\n\n");
890 for (x= 0; tests[x].function_name; x++)
891 {
892 if (wildcard)
893 if (strcmp(wildcard, tests[x].function_name))
894 continue;
895
896 memcached_st *memc;
897 memcached_return rc;
898 struct timeval start_time, end_time;
899
900 memc= memcached_create(NULL);
901 assert(memc);
902
903 if (tests[x].requires_flush)
904 memcached_flush(memc, 0);
905
906 rc= memcached_server_push(memc, servers);
907 assert(rc == MEMCACHED_SUCCESS);
908
909 fprintf(stderr, "Testing %s", tests[x].function_name);
910 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
911 gettimeofday(&start_time, NULL);
912 tests[x].function(memc);
913 gettimeofday(&end_time, NULL);
914 long int load_time= timedif(end_time, start_time);
915 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
916 load_time % 1000);
917 assert(memc);
918 memcached_free(memc);
919 }
920 }
921
922 if ((test_to_run && !strcmp(test_to_run, "nodelay")) || !test_to_run)
923 {
924 fprintf(stderr, "\nTCP Nodelay tests\n\n");
925 for (x= 0; tests[x].function_name; x++)
926 {
927 if (wildcard)
928 if (strcmp(wildcard, tests[x].function_name))
929 continue;
930
931 memcached_st *memc;
932 memcached_return rc;
933 struct timeval start_time, end_time;
934
935 memc= memcached_create(NULL);
936 assert(memc);
937
938 if (tests[x].requires_flush)
939 memcached_flush(memc, 0);
940
941 rc= memcached_server_push(memc, servers);
942 assert(rc == MEMCACHED_SUCCESS);
943
944 fprintf(stderr, "Testing %s", tests[x].function_name);
945 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
946 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, NULL);
947 gettimeofday(&start_time, NULL);
948 tests[x].function(memc);
949 gettimeofday(&end_time, NULL);
950 long int load_time= timedif(end_time, start_time);
951 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
952 load_time % 1000);
953 assert(memc);
954 memcached_free(memc);
955 }
956 }
957
958 if ((test_to_run && !strcmp(test_to_run, "md5")) || !test_to_run)
959 {
960 fprintf(stderr, "\nMD5 Hashing\n\n");
961 for (x= 0; tests[x].function_name; x++)
962 {
963 if (wildcard)
964 if (strcmp(wildcard, tests[x].function_name))
965 continue;
966
967 memcached_st *memc;
968 memcached_return rc;
969 struct timeval start_time, end_time;
970
971 memc= memcached_create(NULL);
972 assert(memc);
973
974 if (tests[x].requires_flush)
975 memcached_flush(memc, 0);
976
977 rc= memcached_server_push(memc, servers);
978 assert(rc == MEMCACHED_SUCCESS);
979
980 fprintf(stderr, "Testing %s", tests[x].function_name);
981 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_MD5_HASHING, NULL);
982 gettimeofday(&start_time, NULL);
983 tests[x].function(memc);
984 gettimeofday(&end_time, NULL);
985 long int load_time= timedif(end_time, start_time);
986 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
987 load_time % 1000);
988 assert(memc);
989 memcached_free(memc);
990 }
991 }
992
993 if ((test_to_run && !strcmp(test_to_run, "string")) || !test_to_run)
994 {
995 fprintf(stderr, "\nString tests (internal API)\n\n");
996 for (x= 0; string_tests[x].function_name; x++)
997 {
998 if (wildcard)
999 if (strcmp(wildcard, string_tests[x].function_name))
1000 continue;
1001
1002 memcached_st *memc;
1003 memcached_return rc;
1004 struct timeval start_time, end_time;
1005
1006 memc= memcached_create(NULL);
1007 assert(memc);
1008
1009 if (tests[x].requires_flush)
1010 memcached_flush(memc, 0);
1011
1012 rc= memcached_server_push(memc, servers);
1013 assert(rc == MEMCACHED_SUCCESS);
1014
1015 fprintf(stderr, "Testing %s", string_tests[x].function_name);
1016 gettimeofday(&start_time, NULL);
1017 string_tests[x].function(memc);
1018 gettimeofday(&end_time, NULL);
1019 long int load_time= timedif(end_time, start_time);
1020 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
1021 load_time % 1000);
1022 assert(memc);
1023 memcached_free(memc);
1024 }
1025 }
1026
1027 if ((test_to_run && !strcmp(test_to_run, "user")) || !test_to_run)
1028 {
1029 fprintf(stderr, "\nUser Supplied tests\n\n");
1030 for (x= 0; user_tests[x].function_name; x++)
1031 {
1032 if (wildcard)
1033 if (strcmp(wildcard, user_tests[x].function_name))
1034 continue;
1035
1036 memcached_st *memc;
1037 memcached_return rc;
1038 struct timeval start_time, end_time;
1039
1040 memc= memcached_create(NULL);
1041 assert(memc);
1042
1043 if (tests[x].requires_flush)
1044 memcached_flush(memc, 0);
1045
1046 rc= memcached_server_push(memc, servers);
1047 assert(rc == MEMCACHED_SUCCESS);
1048
1049 fprintf(stderr, "Testing %s", user_tests[x].function_name);
1050 gettimeofday(&start_time, NULL);
1051 user_tests[x].function(memc);
1052 gettimeofday(&end_time, NULL);
1053 long int load_time= timedif(end_time, start_time);
1054 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
1055 load_time % 1000);
1056 assert(memc);
1057 memcached_free(memc);
1058 }
1059 }
1060
1061 /* Clean up whatever we might have left */
1062 {
1063 memcached_st *memc;
1064 memc= memcached_create(NULL);
1065 assert(memc);
1066 flush_test(memc);
1067 memcached_free(memc);
1068 }
1069
1070 fprintf(stderr, "All tests completed successfully\n\n");
1071
1072 memcached_server_list_free(servers);
1073
1074 return 0;
1075 }