Merged
[m6w6/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 #include <fnmatch.h>
14
15 #include "test.h"
16
17 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
18 #define TEST_SERVERS 5
19
20 long int timedif(struct timeval a, struct timeval b)
21 {
22 register int us, s;
23
24 us = a.tv_usec - b.tv_usec;
25 us /= 1000;
26 s = a.tv_sec - b.tv_sec;
27 s *= 1000;
28 return s + us;
29 }
30
31 char *server_startup()
32 {
33 unsigned int x;
34 char server_string_buffer[8096];
35 char *end_ptr;
36
37 end_ptr= server_string_buffer;
38
39 for (x= 0; x < TEST_SERVERS; x++)
40 {
41 char buffer[1024]; /* Nothing special for number */
42 int count;
43
44 sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -p %u", x, x+ TEST_PORT_BASE);
45 system(buffer);
46 count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
47 end_ptr+= count;
48 }
49 *end_ptr= 0;
50
51 return strdup(server_string_buffer);
52 }
53
54 void server_shutdown(char *server_string)
55 {
56 unsigned int x;
57
58 if (server_string)
59 {
60 for (x= 0; x < TEST_SERVERS; x++)
61 {
62 char buffer[1024]; /* Nothing special for number */
63 sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);
64 system(buffer);
65 }
66
67 free(server_string);
68 }
69 }
70
71 int main(int argc, char *argv[])
72 {
73 unsigned int x;
74 char *server_list;
75 char *collection_to_run= NULL;
76 char *wildcard= NULL;
77 memcached_server_st *servers;
78 collection_st *collection;
79 collection_st *next;
80 uint8_t failed;
81
82 collection= gets_collections();
83
84
85 if (argc > 1)
86 collection_to_run= argv[1];
87
88 if (argc == 3)
89 wildcard= argv[2];
90
91 if ((server_list= getenv("MEMCACHED_SERVERS")))
92 {
93 printf("servers %s\n", server_list);
94 servers= memcached_servers_parse(server_list);
95 server_list= NULL;
96 }
97 else
98 {
99 server_list= server_startup();
100 printf("servers %s\n", server_list);
101 servers= memcached_servers_parse(server_list);
102 }
103 assert(servers);
104
105 srandom(time(NULL));
106
107
108 for (x= 0; x < memcached_server_list_count(servers); x++)
109 {
110 printf("\t%s : %u\n", servers[x].hostname, servers[x].port);
111 assert(servers[x].fd == -1);
112 assert(servers[x].cursor_active == 0);
113 }
114
115 printf("\n");
116
117 for (next= collection; next->name; next++)
118 {
119 test_st *run;
120
121 run= next->tests;
122 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
123 continue;
124
125 fprintf(stderr, "\n%s\n\n", next->name);
126
127 for (x= 0; run->name; run++)
128 {
129 unsigned int loop;
130 memcached_st *memc;
131 memcached_return rc;
132 struct timeval start_time, end_time;
133 long int load_time;
134
135 if (wildcard && fnmatch(wildcard, run->name, 0))
136 continue;
137
138 fprintf(stderr, "Testing %s", run->name);
139
140 memc= memcached_create(NULL);
141 assert(memc);
142
143 rc= memcached_server_push(memc, servers);
144 assert(rc == MEMCACHED_SUCCESS);
145
146 if (run->requires_flush)
147 {
148 memcached_flush(memc, 0);
149 memcached_quit(memc);
150 }
151
152 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
153 {
154 assert(memc->hosts[loop].fd == -1);
155 assert(memc->hosts[loop].cursor_active == 0);
156 }
157
158 if (next->pre)
159 {
160 memcached_return rc;
161 rc= next->pre(memc);
162
163 if (rc != MEMCACHED_SUCCESS)
164 {
165 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
166 goto error;
167 }
168 }
169
170 gettimeofday(&start_time, NULL);
171 failed= run->function(memc);
172 gettimeofday(&end_time, NULL);
173 load_time= timedif(end_time, start_time);
174 if (failed)
175 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000,
176 load_time % 1000);
177 else
178 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
179 load_time % 1000);
180
181 if (next->post)
182 (void)next->post(memc);
183
184 assert(memc);
185 error:
186 memcached_free(memc);
187 }
188 }
189
190 fprintf(stderr, "All tests completed successfully\n\n");
191
192 memcached_server_list_free(servers);
193
194 server_shutdown(server_list);
195
196 return 0;
197 }