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