MErge Trond
[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 static const char *test_strerror(test_return_t code)
29 {
30 switch (code) {
31 case TEST_SUCCESS:
32 return "ok";
33 case TEST_FAILURE:
34 return "failed";
35 case TEST_MEMORY_ALLOCATION_FAILURE:
36 return "memory allocation";
37 case TEST_SKIPPED:
38 return "skipped";
39 case TEST_MAXIMUM_RETURN:
40 default:
41 fprintf(stderr, "Unknown return value\n");
42 abort();
43 }
44
45 }
46
47 void create_core(void)
48 {
49 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL && fork() == 0)
50 abort();
51 }
52
53 int main(int argc, char *argv[])
54 {
55 test_return_t failed;
56 unsigned int x;
57 char *collection_to_run= NULL;
58 char *wildcard= NULL;
59 server_startup_st *startup_ptr;
60 memcached_server_st *servers;
61 world_st world;
62 collection_st *collection;
63 collection_st *next;
64 void *world_ptr;
65
66 memset(&world, 0, sizeof(world_st));
67 get_world(&world);
68 collection= world.collections;
69
70 if (world.create)
71 world_ptr= world.create();
72 else
73 world_ptr= NULL;
74
75 startup_ptr= (server_startup_st *)world_ptr;
76 servers= (memcached_server_st *)startup_ptr->servers;
77
78 if (argc > 1)
79 collection_to_run= argv[1];
80
81 if (argc == 3)
82 wildcard= argv[2];
83
84 for (next= collection; next->name; next++)
85 {
86 test_st *run;
87
88 run= next->tests;
89 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
90 continue;
91
92 fprintf(stderr, "\n%s\n\n", next->name);
93
94 for (x= 0; run->name; run++)
95 {
96 unsigned int loop;
97 memcached_st *memc;
98 memcached_return rc;
99 struct timeval start_time, end_time;
100 long int load_time;
101
102 if (wildcard && fnmatch(wildcard, run->name, 0))
103 continue;
104
105 fprintf(stderr, "Testing %s", run->name);
106
107 memc= memcached_create(NULL);
108 test_truth(memc);
109
110 rc= memcached_server_push(memc, servers);
111 test_truth(rc == MEMCACHED_SUCCESS);
112
113 if (run->requires_flush)
114 {
115 memcached_flush(memc, 0);
116 memcached_quit(memc);
117 }
118
119 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
120 {
121 test_truth(memc->hosts[loop].fd == -1);
122 test_truth(memc->hosts[loop].cursor_active == 0);
123 }
124
125 if (next->pre)
126 {
127 rc= next->pre(memc);
128
129 if (rc != MEMCACHED_SUCCESS)
130 {
131 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
132 goto error;
133 }
134 }
135
136 gettimeofday(&start_time, NULL);
137 failed= run->function(memc);
138 gettimeofday(&end_time, NULL);
139 load_time= timedif(end_time, start_time);
140
141 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ %s ]\n", load_time / 1000,
142 load_time % 1000, test_strerror(failed));
143
144 if (next->post)
145 (void)next->post(memc);
146
147 test_truth(memc);
148 error:
149 memcached_free(memc);
150 }
151 }
152
153 fprintf(stderr, "All tests completed successfully\n\n");
154
155 if (world.destroy)
156 world.destroy(world_ptr);
157
158 return 0;
159 }