Work inspired by matt knox
[m6w6/libmemcached] / tests / test.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/time.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <time.h>
12 #include <fnmatch.h>
13 #include "server.h"
14
15 #include "test.h"
16
17 static long int timedif(struct timeval a, struct timeval b)
18 {
19 register int us, s;
20
21 us = (int)(a.tv_usec - b.tv_usec);
22 us /= 1000;
23 s = (int)(a.tv_sec - b.tv_sec);
24 s *= 1000;
25 return s + us;
26 }
27
28 int main(int argc, char *argv[])
29 {
30 unsigned int x;
31 char *collection_to_run= NULL;
32 char *wildcard= NULL;
33 server_startup_st *startup_ptr;
34 memcached_server_st *servers;
35 world_st world;
36 collection_st *collection;
37 collection_st *next;
38 uint8_t failed;
39 void *world_ptr;
40
41 memset(&world, 0, sizeof(world_st));
42 get_world(&world);
43 collection= world.collections;
44
45 if (world.create)
46 world_ptr= world.create();
47 else
48 world_ptr= NULL;
49
50 startup_ptr= (server_startup_st *)world_ptr;
51 servers= (memcached_server_st *)startup_ptr->servers;
52
53 if (argc > 1)
54 collection_to_run= argv[1];
55
56 if (argc == 3)
57 wildcard= argv[2];
58
59 for (next= collection; next->name; next++)
60 {
61 test_st *run;
62
63 run= next->tests;
64 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
65 continue;
66
67 fprintf(stderr, "\n%s\n\n", next->name);
68
69 for (x= 0; run->name; run++)
70 {
71 unsigned int loop;
72 memcached_st *memc;
73 memcached_return rc;
74 struct timeval start_time, end_time;
75 long int load_time;
76
77 if (wildcard && fnmatch(wildcard, run->name, 0))
78 continue;
79
80 fprintf(stderr, "Testing %s", run->name);
81
82 memc= memcached_create(NULL);
83 assert(memc);
84
85 rc= memcached_server_push(memc, servers);
86 assert(rc == MEMCACHED_SUCCESS);
87
88 if (run->requires_flush)
89 {
90 memcached_flush(memc, 0);
91 memcached_quit(memc);
92 }
93
94 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
95 {
96 assert(memc->hosts[loop].fd == -1);
97 assert(memc->hosts[loop].cursor_active == 0);
98 }
99
100 if (next->pre)
101 {
102 rc= next->pre(memc);
103
104 if (rc != MEMCACHED_SUCCESS)
105 {
106 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
107 goto error;
108 }
109 }
110
111 gettimeofday(&start_time, NULL);
112 failed= run->function(memc);
113 gettimeofday(&end_time, NULL);
114 load_time= timedif(end_time, start_time);
115 if (failed)
116 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000,
117 load_time % 1000);
118 else
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 if (world.destroy)
134 world.destroy(world_ptr);
135
136 return 0;
137 }