6c6bea645c4da1376069b70ef387e48a97447ccf
[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 #include "server.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 *collection_to_run= NULL;
33 char *wildcard= NULL;
34 server_startup_st *startup_ptr;
35 memcached_server_st *servers;
36 world_st world;
37 collection_st *collection;
38 collection_st *next;
39 uint8_t failed;
40 void *world_ptr;
41
42 memset(&world, 0, sizeof(world_st));
43 get_world(&world);
44 collection= world.collections;
45
46 if (world.create)
47 world_ptr= world.create();
48
49 startup_ptr= (server_startup_st *)world_ptr;
50 servers= (memcached_server_st *)startup_ptr->servers;
51
52 if (argc > 1)
53 collection_to_run= argv[1];
54
55 if (argc == 3)
56 wildcard= argv[2];
57
58 for (next= collection; next->name; next++)
59 {
60 test_st *run;
61
62 run= next->tests;
63 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
64 continue;
65
66 fprintf(stderr, "\n%s\n\n", next->name);
67
68 for (x= 0; run->name; run++)
69 {
70 unsigned int loop;
71 memcached_st *memc;
72 memcached_return rc;
73 struct timeval start_time, end_time;
74 long int load_time;
75
76 if (wildcard && fnmatch(wildcard, run->name, 0))
77 continue;
78
79 fprintf(stderr, "Testing %s", run->name);
80
81 memc= memcached_create(NULL);
82 assert(memc);
83
84 rc= memcached_server_push(memc, servers);
85 assert(rc == MEMCACHED_SUCCESS);
86
87 if (run->requires_flush)
88 {
89 memcached_flush(memc, 0);
90 memcached_quit(memc);
91 }
92
93 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
94 {
95 assert(memc->hosts[loop].fd == -1);
96 assert(memc->hosts[loop].cursor_active == 0);
97 }
98
99 if (next->pre)
100 {
101 memcached_return rc;
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 }