60db54cbd8969269d3dad0b6468a4f6be50dc9f6
[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 #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 uint8_t failed;
80
81 collection= gets_collections();
82
83
84 if (argc > 1)
85 collection_to_run= argv[1];
86
87 if (argc == 3)
88 wildcard= argv[2];
89
90 if ((server_list= getenv("MEMCACHED_SERVERS")))
91 {
92 printf("servers %s\n", server_list);
93 servers= memcached_servers_parse(server_list);
94 server_list= NULL;
95 }
96 else
97 {
98 server_list= server_startup();
99 printf("servers %s\n", server_list);
100 servers= memcached_servers_parse(server_list);
101 }
102 assert(servers);
103
104 srandom(time(NULL));
105
106
107 for (x= 0; x < memcached_server_list_count(servers); x++)
108 {
109 printf("\t%s : %u\n", servers[x].hostname, servers[x].port);
110 assert(servers[x].fd == -1);
111 assert(servers[x].cursor_active == 0);
112 }
113
114 printf("\n");
115
116 collection_st *next;
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
134 if (wildcard && fnmatch(wildcard, run->name, 0))
135 continue;
136
137 fprintf(stderr, "Testing %s", run->name);
138
139 memc= memcached_create(NULL);
140 assert(memc);
141
142 rc= memcached_server_push(memc, servers);
143 assert(rc == MEMCACHED_SUCCESS);
144
145 if (run->requires_flush)
146 {
147 memcached_flush(memc, 0);
148 memcached_quit(memc);
149 }
150
151 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
152 {
153 assert(memc->hosts[loop].fd == -1);
154 assert(memc->hosts[loop].cursor_active == 0);
155 }
156
157 if (next->pre)
158 {
159 memcached_return rc;
160 rc= next->pre(memc);
161
162 if (rc != MEMCACHED_SUCCESS)
163 {
164 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
165 goto error;
166 }
167 }
168
169 gettimeofday(&start_time, NULL);
170 failed= run->function(memc);
171 gettimeofday(&end_time, NULL);
172 long int load_time= timedif(end_time, start_time);
173 if (failed)
174 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000,
175 load_time % 1000);
176 else
177 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
178 load_time % 1000);
179
180 if (next->post)
181 (void)next->post(memc);
182
183 assert(memc);
184 error:
185 memcached_free(memc);
186 }
187 }
188
189 fprintf(stderr, "All tests completed successfully\n\n");
190
191 memcached_server_list_free(servers);
192
193 server_shutdown(server_list);
194
195 return 0;
196 }