47db82c733a2e43aec483f3e5fe28d281a570e28
[awesomized/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 long int timedif(struct timeval a, struct timeval b)
18 {
19 register int us, s;
20
21 us = a.tv_usec - b.tv_usec;
22 us /= 1000;
23 s = 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 memcached_return rc;
103 rc= next->pre(memc);
104
105 if (rc != MEMCACHED_SUCCESS)
106 {
107 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
108 goto error;
109 }
110 }
111
112 gettimeofday(&start_time, NULL);
113 failed= run->function(memc);
114 gettimeofday(&end_time, NULL);
115 load_time= timedif(end_time, start_time);
116 if (failed)
117 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000,
118 load_time % 1000);
119 else
120 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
121 load_time % 1000);
122
123 if (next->post)
124 (void)next->post(memc);
125
126 assert(memc);
127 error:
128 memcached_free(memc);
129 }
130 }
131
132 fprintf(stderr, "All tests completed successfully\n\n");
133
134 if (world.destroy)
135 world.destroy(world_ptr);
136
137 return 0;
138 }