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