Fixup for release
[awesomized/libmemcached] / tests / test.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <time.h>
13
14 #include "test.h"
15
16 long int timedif(struct timeval a, struct timeval b)
17 {
18 register int us, s;
19
20 us = a.tv_usec - b.tv_usec;
21 us /= 1000;
22 s = a.tv_sec - b.tv_sec;
23 s *= 1000;
24 return s + us;
25 }
26
27 int main(int argc, char *argv[])
28 {
29 unsigned int x;
30 char *server_list;
31 char *collection_to_run= NULL;
32 char *wildcard= NULL;
33 memcached_server_st *servers;
34 collection_st *collection;
35
36 collection= gets_collections();
37
38
39 if (argc > 1)
40 collection_to_run= argv[1];
41
42 if (argc == 3)
43 wildcard= argv[2];
44
45 if (!(server_list= getenv("MEMCACHED_SERVERS")))
46 server_list= "localhost";
47
48 printf("servers %s\n", server_list);
49 srandom(time(NULL));
50
51 servers= memcached_servers_parse(server_list);
52 assert(servers);
53
54 for (x= 0; x < memcached_server_list_count(servers); x++)
55 {
56 printf("\t%s : %u\n", servers[x].hostname, servers[x].port);
57 assert(servers[x].stack_responses == 0);
58 assert(servers[x].fd == -1);
59 assert(servers[x].cursor_active == 0);
60 }
61
62 printf("\n");
63
64 collection_st *next;
65 for (next= collection; next->name; next++)
66 {
67 test_st *run;
68
69 run= next->tests;
70 if (collection_to_run && strcmp(collection_to_run, next->name))
71 continue;
72
73 fprintf(stderr, "\n%s\n\n", next->name);
74
75 for (x= 0; run->name; run++)
76 {
77 if (wildcard && strcmp(wildcard, run->name))
78 continue;
79
80 fprintf(stderr, "Testing %s", run->name);
81
82 memcached_st *memc;
83 memcached_return rc;
84 struct timeval start_time, end_time;
85
86 memc= memcached_create(NULL);
87 assert(memc);
88
89 if (run->requires_flush)
90 memcached_flush(memc, 0);
91
92 rc= memcached_server_push(memc, servers);
93 assert(rc == MEMCACHED_SUCCESS);
94
95 unsigned int loop;
96 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
97 {
98 assert(memc->hosts[loop].stack_responses == 0);
99 assert(memc->hosts[loop].fd == -1);
100 assert(memc->hosts[loop].cursor_active == 0);
101 }
102
103 if (next->pre)
104 {
105 memcached_return rc;
106 rc= next->pre(memc);
107
108 if (rc != MEMCACHED_SUCCESS)
109 {
110 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
111 goto error;
112 }
113 }
114
115 gettimeofday(&start_time, NULL);
116 run->function(memc);
117 gettimeofday(&end_time, NULL);
118 long int load_time= timedif(end_time, start_time);
119 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
120 load_time % 1000);
121
122 if (next->post)
123 (void)next->post(memc);
124
125 assert(memc);
126 error:
127 memcached_free(memc);
128 }
129 }
130
131 fprintf(stderr, "All tests completed successfully\n\n");
132
133 memcached_server_list_free(servers);
134
135 return 0;
136 }