2 Sample test application.
12 long int timedif(struct timeval a
, struct timeval b
)
16 us
= a
.tv_usec
- b
.tv_usec
;
18 s
= a
.tv_sec
- b
.tv_sec
;
23 void init_test(memcached_st
*not_used
)
27 (void)memcached_create(&memc
);
28 memcached_free(&memc
);
31 void allocation_test(memcached_st
*not_used
)
34 memc
= memcached_create(NULL
);
39 void connection_test(memcached_st
*memc
)
43 rc
= memcached_server_add(memc
, "localhost", 0);
44 assert(rc
== MEMCACHED_SUCCESS
);
47 void error_test(memcached_st
*memc
)
51 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
53 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
57 void set_test(memcached_st
*memc
)
61 char *value
= "when we sanitize";
63 rc
= memcached_set(memc
, key
, strlen(key
),
65 (time_t)0, (uint16_t)0);
66 assert(rc
== MEMCACHED_SUCCESS
);
69 void add_test(memcached_st
*memc
)
73 char *value
= "when we sanitize";
75 rc
= memcached_add(memc
, key
, strlen(key
),
77 (time_t)0, (uint16_t)0);
78 assert(rc
== MEMCACHED_NOTSTORED
);
81 void replace_test(memcached_st
*memc
)
85 char *value
= "when we sanitize";
87 rc
= memcached_replace(memc
, key
, strlen(key
),
89 (time_t)0, (uint16_t)0);
90 assert(rc
== MEMCACHED_SUCCESS
);
93 void delete_test(memcached_st
*memc
)
97 char *value
= "when we sanitize";
99 rc
= memcached_set(memc
, key
, strlen(key
),
100 value
, strlen(value
),
101 (time_t)0, (uint16_t)0);
102 assert(rc
== MEMCACHED_SUCCESS
);
104 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
105 assert(rc
== MEMCACHED_SUCCESS
);
108 void flush_test(memcached_st
*memc
)
112 rc
= memcached_flush(memc
, 0);
113 assert(rc
== MEMCACHED_SUCCESS
);
116 void get_test(memcached_st
*memc
)
121 size_t string_length
;
124 string
= memcached_get(memc
, key
, strlen(key
),
125 &string_length
, &flags
, &rc
);
127 assert(rc
== MEMCACHED_NOTFOUND
);
128 assert(string_length
== 0);
132 void get_test2(memcached_st
*memc
)
136 char *value
= "when we sanitize";
138 size_t string_length
;
141 rc
= memcached_set(memc
, key
, strlen(key
),
142 value
, strlen(value
),
143 (time_t)0, (uint16_t)0);
144 assert(rc
== MEMCACHED_SUCCESS
);
146 string
= memcached_get(memc
, key
, strlen(key
),
147 &string_length
, &flags
, &rc
);
150 assert(rc
== MEMCACHED_SUCCESS
);
151 assert(string_length
== strlen(value
));
152 assert(!memcmp(string
, value
, string_length
));
157 void set_test2(memcached_st
*memc
)
161 char *value
= "train in the brain";
162 size_t value_length
= strlen(value
);
165 for (x
= 0; x
< 10; x
++)
167 rc
= memcached_set(memc
, key
, strlen(key
),
169 (time_t)0, (uint16_t)0);
170 assert(rc
== MEMCACHED_SUCCESS
);
174 void set_test3(memcached_st
*memc
)
179 size_t value_length
= 8191;
182 value
= (char*)malloc(value_length
);
185 for (x
= 0; x
< value_length
; x
++)
186 value
[x
] = (char) (x
% 127);
188 for (x
= 0; x
< 1; x
++)
190 rc
= memcached_set(memc
, key
, strlen(key
),
192 (time_t)0, (uint16_t)0);
193 assert(rc
== MEMCACHED_SUCCESS
);
199 void get_test3(memcached_st
*memc
)
204 size_t value_length
= 8191;
206 size_t string_length
;
210 value
= (char*)malloc(value_length
);
213 for (x
= 0; x
< value_length
; x
++)
214 value
[x
] = (char) (x
% 127);
216 rc
= memcached_set(memc
, key
, strlen(key
),
218 (time_t)0, (uint16_t)0);
219 assert(rc
== MEMCACHED_SUCCESS
);
221 string
= memcached_get(memc
, key
, strlen(key
),
222 &string_length
, &flags
, &rc
);
224 assert(rc
== MEMCACHED_SUCCESS
);
226 assert(string_length
== value_length
);
227 assert(!memcmp(string
, value
, string_length
));
233 void get_test4(memcached_st
*memc
)
238 size_t value_length
= 8191;
240 size_t string_length
;
244 value
= (char*)malloc(value_length
);
247 for (x
= 0; x
< value_length
; x
++)
248 value
[x
] = (char) (x
% 127);
250 rc
= memcached_set(memc
, key
, strlen(key
),
252 (time_t)0, (uint16_t)0);
253 assert(rc
== MEMCACHED_SUCCESS
);
255 for (x
= 0; x
< 10; x
++)
257 string
= memcached_get(memc
, key
, strlen(key
),
258 &string_length
, &flags
, &rc
);
260 assert(rc
== MEMCACHED_SUCCESS
);
262 assert(string_length
== value_length
);
263 assert(!memcmp(string
, value
, string_length
));
270 void stats_servername_test(memcached_st
*memc
)
273 memcached_stat_st stat
;
274 rc
= memcached_stat_servername(&stat
, NULL
,
276 MEMCACHED_DEFAULT_PORT
);
279 void increment_test(memcached_st
*memc
)
281 unsigned int new_number
;
286 rc
= memcached_set(memc
, key
, strlen(key
),
287 value
, strlen(value
),
288 (time_t)0, (uint16_t)0);
289 assert(rc
== MEMCACHED_SUCCESS
);
291 rc
= memcached_increment(memc
, key
, strlen(key
),
293 assert(rc
== MEMCACHED_SUCCESS
);
294 assert(new_number
== 1);
296 rc
= memcached_increment(memc
, key
, strlen(key
),
298 assert(rc
== MEMCACHED_SUCCESS
);
299 assert(new_number
== 2);
302 void decrement_test(memcached_st
*memc
)
304 unsigned int new_number
;
309 rc
= memcached_set(memc
, key
, strlen(key
),
310 value
, strlen(value
),
311 (time_t)0, (uint16_t)0);
312 assert(rc
== MEMCACHED_SUCCESS
);
314 rc
= memcached_decrement(memc
, key
, strlen(key
),
316 assert(rc
== MEMCACHED_SUCCESS
);
317 assert(new_number
== 2);
319 rc
= memcached_decrement(memc
, key
, strlen(key
),
321 assert(rc
== MEMCACHED_SUCCESS
);
322 assert(new_number
== 1);
325 void quit_test(memcached_st
*memc
)
329 char *value
= "sanford and sun";
331 rc
= memcached_set(memc
, key
, strlen(key
),
332 value
, strlen(value
),
333 (time_t)10, (uint16_t)3);
334 assert(rc
== MEMCACHED_SUCCESS
);
335 memcached_quit(memc
);
337 rc
= memcached_set(memc
, key
, strlen(key
),
338 value
, strlen(value
),
339 (time_t)50, (uint16_t)9);
340 assert(rc
== MEMCACHED_SUCCESS
);
343 void mget_test(memcached_st
*memc
)
346 char *keys
[]= {"fudge", "son", "food"};
347 size_t key_length
[]= {5, 3, 4};
351 char return_key
[MEMCACHED_MAX_KEY
];
352 size_t return_key_length
;
354 size_t return_value_length
;
356 /* We need to empty the server before continueing test */
357 rc
= memcached_flush(memc
, 0);
358 assert(rc
== MEMCACHED_SUCCESS
);
360 rc
= memcached_mget(memc
, keys
, key_length
, 3);
361 assert(rc
== MEMCACHED_SUCCESS
);
363 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
364 &return_value_length
, &flags
, &rc
)) != NULL
)
366 assert(return_value
);
368 assert(!return_value
);
369 assert(return_value_length
== 0);
370 assert(rc
== MEMCACHED_NOTFOUND
);
372 for (x
= 0; x
< 3; x
++)
374 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
375 keys
[x
], key_length
[x
],
376 (time_t)50, (uint16_t)9);
377 assert(rc
== MEMCACHED_SUCCESS
);
380 rc
= memcached_mget(memc
, keys
, key_length
, 3);
381 assert(rc
== MEMCACHED_SUCCESS
);
384 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
385 &return_value_length
, &flags
, &rc
)))
387 assert(return_value
);
388 assert(rc
== MEMCACHED_SUCCESS
);
389 assert(return_key_length
== return_value_length
);
390 assert(!memcmp(return_value
, return_key
, return_value_length
));
396 void get_stats_keys(memcached_st
*memc
)
400 memcached_stat_st stat
;
403 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
404 assert(rc
== MEMCACHED_SUCCESS
);
405 for (ptr
= list
; *ptr
; ptr
++)
406 printf("Found key %s\n", *ptr
);
412 void get_stats(memcached_st
*memc
)
418 memcached_stat_st
*stat
;
420 stat
= memcached_stat(memc
, NULL
, &rc
);
421 assert(rc
== MEMCACHED_SUCCESS
);
423 assert(rc
== MEMCACHED_SUCCESS
);
426 for (x
= 0; x
< memcached_server_count(memc
); x
++)
428 list
= memcached_stat_get_keys(memc
, &stat
[x
], &rc
);
429 assert(rc
== MEMCACHED_SUCCESS
);
430 for (ptr
= list
; *ptr
; ptr
++)
431 printf("Found key %s\n", *ptr
);
439 void add_host_test(memcached_st
*memc
)
442 memcached_server_st
*servers
;
444 char servername
[]= "0.example.com";
446 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
448 assert(1 == memcached_server_list_count(servers
));
450 for (x
= 2; x
< 20; x
++)
452 char buffer
[SMALL_STRING_LEN
];
454 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
455 servers
= memcached_server_list_append(servers
, buffer
, 401,
457 assert(rc
== MEMCACHED_SUCCESS
);
458 assert(x
== memcached_server_list_count(servers
));
461 rc
= memcached_server_push(memc
, servers
);
462 assert(rc
== MEMCACHED_SUCCESS
);
463 rc
= memcached_server_push(memc
, servers
);
464 assert(rc
== MEMCACHED_SUCCESS
);
466 memcached_server_list_free(servers
);
469 /* We don't test the behavior itself, we test the switches */
470 void behavior_test(memcached_st
*memc
)
472 unsigned long long value
;
475 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
476 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
479 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
480 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
483 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_MD5_HASHING
, &set
);
484 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_MD5_HASHING
);
489 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
490 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
493 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
494 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
497 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_MD5_HASHING
, &set
);
498 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_MD5_HASHING
);
501 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
504 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
508 /* Test case provided by Cal Haldenbrand */
509 void user_supplied_bug1(memcached_st
*memc
)
511 unsigned int setter
= 1;
518 char *randomstuff
= (char *)malloc(6 * 1024);
519 memset(randomstuff
, 0, 6 * 1024);
523 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
524 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
528 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
532 size
= (rand() % ( 5 * 1024 ) ) + 400;
533 memset(randomstuff
, 0, 6 * 1024);
534 assert(size
< 6 * 1024); /* Being safe here */
536 for (j
= 0 ; j
< size
;j
++)
537 randomstuff
[j
] = (char) (rand() % 26) + 97;
540 sprintf(key
, "%d", x
);
541 rc
= memcached_set(memc
, key
, strlen(key
),
542 randomstuff
, strlen(randomstuff
), 10, 0);
543 /* If we fail, lets try again */
544 if (rc
!= MEMCACHED_SUCCESS
)
545 rc
= memcached_set(memc
, key
, strlen(key
),
546 randomstuff
, strlen(randomstuff
), 10, 0);
547 assert(rc
== MEMCACHED_SUCCESS
);
550 void add_host_test1(memcached_st
*memc
)
554 char servername
[]= "0.example.com";
555 memcached_server_st
*servers
;
557 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
559 assert(1 == memcached_server_list_count(servers
));
561 for (x
= 2; x
< 20; x
++)
563 char buffer
[SMALL_STRING_LEN
];
565 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
566 servers
= memcached_server_list_append(servers
, buffer
, 401,
568 assert(rc
== MEMCACHED_SUCCESS
);
569 assert(x
== memcached_server_list_count(servers
));
572 rc
= memcached_server_push(memc
, servers
);
573 assert(rc
== MEMCACHED_SUCCESS
);
574 rc
= memcached_server_push(memc
, servers
);
575 assert(rc
== MEMCACHED_SUCCESS
);
577 memcached_server_list_free(servers
);
580 typedef struct test_st test_st
;
584 unsigned int requires_flush
;
585 void (*function
)(memcached_st
*memc
);
588 int main(int argc
, char *argv
[])
592 char *test_to_run
= NULL
;
593 char *wildcard
= NULL
;
594 memcached_server_st
*servers
;
597 test_to_run
= argv
[1];
602 if (!(server_list
= getenv("MEMCACHED_SERVERS")))
603 server_list
= "localhost";
605 printf("servers %s\n", server_list
);
608 servers
= memcached_servers_parse(server_list
);
611 for (x
= 0; x
< memcached_server_list_count(servers
); x
++)
613 printf("\t%s : %u\n", servers
[x
].hostname
, servers
[x
].port
);
614 assert(servers
[x
].stack_responses
== 0);
615 assert(servers
[x
].fd
== -1);
616 assert(servers
[x
].cursor_active
== 0);
621 /* Clean the server before beginning testing */
623 {"flush", 0, flush_test
},
624 {"init", 0, init_test
},
625 {"allocation", 0, allocation_test
},
626 {"error", 0, error_test
},
627 {"set", 0, set_test
},
628 {"set2", 0, set_test2
},
629 {"set3", 0, set_test3
},
630 {"add", 0, add_test
},
631 {"replace", 0, replace_test
},
632 {"delete", 1, delete_test
},
633 {"get", 0, get_test
},
634 {"get2", 0, get_test2
},
635 {"get3", 0, get_test3
},
636 {"get4", 0, get_test4
},
637 {"stats_servername", 0, stats_servername_test
},
638 {"increment", 0, increment_test
},
639 {"decrement", 0, decrement_test
},
640 {"quit", 0, quit_test
},
641 {"mget", 0, mget_test
},
642 {"get_stats", 0, get_stats
},
643 {"add_host_test", 0, add_host_test
},
644 {"get_stats_keys", 0, get_stats_keys
},
645 {"behavior_test", 0, get_stats_keys
},
649 test_st user_tests
[] ={
650 {"user_supplied_bug1", 0, user_supplied_bug1
},
654 if ((test_to_run
&& !strcmp(test_to_run
, "block")) || !test_to_run
)
656 fprintf(stderr
, "\nBlock tests\n\n");
657 for (x
= 0; tests
[x
].function_name
; x
++)
660 if (strcmp(wildcard
, tests
[x
].function_name
))
665 struct timeval start_time
, end_time
;
667 memc
= memcached_create(NULL
);
670 rc
= memcached_server_push(memc
, servers
);
671 assert(rc
== MEMCACHED_SUCCESS
);
674 for (loop
= 0; loop
< memcached_server_list_count(servers
); loop
++)
676 assert(memc
->hosts
[loop
].stack_responses
== 0);
677 assert(memc
->hosts
[loop
].fd
== -1);
678 assert(memc
->hosts
[loop
].cursor_active
== 0);
681 fprintf(stderr
, "Testing %s", tests
[x
].function_name
);
682 gettimeofday(&start_time
, NULL
);
683 tests
[x
].function(memc
);
684 gettimeofday(&end_time
, NULL
);
685 long int load_time
= timedif(end_time
, start_time
);
686 fprintf(stderr
, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time
/ 1000,
689 memcached_free(memc
);
693 if ((test_to_run
&& !strcmp(test_to_run
, "nonblock")) || !test_to_run
)
695 fprintf(stderr
, "\nNonblock tests\n\n");
696 for (x
= 0; tests
[x
].function_name
; x
++)
699 if (strcmp(wildcard
, tests
[x
].function_name
))
704 struct timeval start_time
, end_time
;
706 memc
= memcached_create(NULL
);
709 rc
= memcached_server_push(memc
, servers
);
710 assert(rc
== MEMCACHED_SUCCESS
);
712 fprintf(stderr
, "Testing %s", tests
[x
].function_name
);
713 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
714 gettimeofday(&start_time
, NULL
);
715 tests
[x
].function(memc
);
716 gettimeofday(&end_time
, NULL
);
717 long int load_time
= timedif(end_time
, start_time
);
718 fprintf(stderr
, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time
/ 1000,
721 memcached_free(memc
);
725 if ((test_to_run
&& !strcmp(test_to_run
, "nodelay")) || !test_to_run
)
727 fprintf(stderr
, "\nTCP Nodelay tests\n\n");
728 for (x
= 0; tests
[x
].function_name
; x
++)
731 if (strcmp(wildcard
, tests
[x
].function_name
))
736 struct timeval start_time
, end_time
;
738 memc
= memcached_create(NULL
);
741 rc
= memcached_server_push(memc
, servers
);
742 assert(rc
== MEMCACHED_SUCCESS
);
744 fprintf(stderr
, "Testing %s", tests
[x
].function_name
);
745 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
746 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, NULL
);
747 gettimeofday(&start_time
, NULL
);
748 tests
[x
].function(memc
);
749 gettimeofday(&end_time
, NULL
);
750 long int load_time
= timedif(end_time
, start_time
);
751 fprintf(stderr
, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time
/ 1000,
754 memcached_free(memc
);
758 if ((test_to_run
&& !strcmp(test_to_run
, "md5")) || !test_to_run
)
760 fprintf(stderr
, "\nMD5 Hashing\n\n");
761 for (x
= 0; tests
[x
].function_name
; x
++)
764 if (strcmp(wildcard
, tests
[x
].function_name
))
769 struct timeval start_time
, end_time
;
771 memc
= memcached_create(NULL
);
774 rc
= memcached_server_push(memc
, servers
);
775 assert(rc
== MEMCACHED_SUCCESS
);
777 fprintf(stderr
, "Testing %s", tests
[x
].function_name
);
778 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_MD5_HASHING
, NULL
);
779 gettimeofday(&start_time
, NULL
);
780 tests
[x
].function(memc
);
781 gettimeofday(&end_time
, NULL
);
782 long int load_time
= timedif(end_time
, start_time
);
783 fprintf(stderr
, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time
/ 1000,
786 memcached_free(memc
);
790 if ((test_to_run
&& !strcmp(test_to_run
, "user")) || !test_to_run
)
792 fprintf(stderr
, "\nUser Supplied tests\n\n");
793 for (x
= 0; user_tests
[x
].function_name
; x
++)
796 if (strcmp(wildcard
, tests
[x
].function_name
))
801 struct timeval start_time
, end_time
;
803 memc
= memcached_create(NULL
);
806 rc
= memcached_server_push(memc
, servers
);
807 assert(rc
== MEMCACHED_SUCCESS
);
809 fprintf(stderr
, "Testing %s", user_tests
[x
].function_name
);
810 gettimeofday(&start_time
, NULL
);
811 user_tests
[x
].function(memc
);
812 gettimeofday(&end_time
, NULL
);
813 long int load_time
= timedif(end_time
, start_time
);
814 fprintf(stderr
, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time
/ 1000,
817 memcached_free(memc
);
821 /* Clean up whatever we might have left */
824 memc
= memcached_create(NULL
);
827 memcached_free(memc
);
830 fprintf(stderr
, "All tests completed successfully\n\n");