Updating to test trigger
[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 else
49 world_ptr= NULL;
50
51 startup_ptr= (server_startup_st *)world_ptr;
52 servers= (memcached_server_st *)startup_ptr->servers;
53
54 if (argc > 1)
55 collection_to_run= argv[1];
56
57 if (argc == 3)
58 wildcard= argv[2];
59
60 for (next= collection; next->name; next++)
61 {
62 test_st *run;
63
64 run= next->tests;
65 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
66 continue;
67
68 fprintf(stderr, "\n%s\n\n", next->name);
69
70 for (x= 0; run->name; run++)
71 {
72 unsigned int loop;
73 memcached_st *memc;
74 memcached_return rc;
75 struct timeval start_time, end_time;
76 long int load_time;
77
78 if (wildcard && fnmatch(wildcard, run->name, 0))
79 continue;
80
81 fprintf(stderr, "Testing %s", run->name);
82
83 memc= memcached_create(NULL);
84 assert(memc);
85
86 rc= memcached_server_push(memc, servers);
87 assert(rc == MEMCACHED_SUCCESS);
88
89 if (run->requires_flush)
90 {
91 memcached_flush(memc, 0);
92 memcached_quit(memc);
93 }
94
95 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
96 {
97 assert(memc->hosts[loop].fd == -1);
98 assert(memc->hosts[loop].cursor_active == 0);
99 }
100
101 if (next->pre)
102 {
103 memcached_return rc;
104 rc= next->pre(memc);
105
106 if (rc != MEMCACHED_SUCCESS)
107 {
108 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
109 goto error;
110 }
111 }
112
113 gettimeofday(&start_time, NULL);
114 failed= run->function(memc);
115 gettimeofday(&end_time, NULL);
116 load_time= timedif(end_time, start_time);
117 if (failed)
118 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000,
119 load_time % 1000);
120 else
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 if (world.destroy)
136 world.destroy(world_ptr);
137
138 return 0;
139 }