2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
10 Sample test application.
21 #include <sys/types.h>
27 #include <libmemcached/memcached.h>
29 #include <libtest/test.h>
31 static void world_stats_print(world_stats_st
*stats
)
34 fprintf(stderr
, "Total Collections\t\t\t\t%u\n", stats
->collection_total
);
35 fprintf(stderr
, "\tFailed Collections\t\t\t%u\n", stats
->collection_failed
);
36 fprintf(stderr
, "\tSkipped Collections\t\t\t%u\n", stats
->collection_skipped
);
37 fprintf(stderr
, "\tSucceeded Collections\t\t%u\n", stats
->collection_success
);
39 fprintf(stderr
, "Total\t\t\t\t%u\n", stats
->total
);
40 fprintf(stderr
, "\tFailed\t\t\t%u\n", stats
->failed
);
41 fprintf(stderr
, "\tSkipped\t\t\t%u\n", stats
->skipped
);
42 fprintf(stderr
, "\tSucceeded\t\t%u\n", stats
->success
);
45 long int timedif(struct timeval a
, struct timeval b
)
49 us
= (int)(a
.tv_usec
- b
.tv_usec
);
51 s
= (int)(a
.tv_sec
- b
.tv_sec
);
56 const char *test_strerror(test_return_t code
)
63 case TEST_MEMORY_ALLOCATION_FAILURE
:
64 return "memory allocation";
67 case TEST_MAXIMUM_RETURN
:
69 fprintf(stderr
, "Unknown return value\n");
74 void create_core(void)
76 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
86 while (waitpid(pid
, NULL
, 0) != pid
)
95 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
107 static world_runner_st defualt_runners
= {
113 static test_return_t
_default_callback(void *p
)
120 static inline void set_default_fn(test_callback_fn
*fn
)
124 *fn
= _default_callback
;
128 static collection_st
*init_world(world_st
*world
)
132 world
->runner
= &defualt_runners
;
135 set_default_fn(&world
->collection
.startup
);
136 set_default_fn(&world
->collection
.shutdown
);
138 return world
->collections
;
142 int main(int argc
, char *argv
[])
144 test_return_t return_code
;
146 char *collection_to_run
= NULL
;
147 char *wildcard
= NULL
;
149 collection_st
*collection
;
153 world_stats_st stats
;
155 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
156 if (sasl_client_init(NULL
) != SASL_OK
)
158 fprintf(stderr
, "Failed to initialize sasl library!\n");
163 memset(&stats
, 0, sizeof(stats
));
164 memset(&world
, 0, sizeof(world
));
167 collection
= init_world(&world
);
172 world_ptr
= world
.create(&error
);
173 if (error
!= TEST_SUCCESS
)
182 collection_to_run
= argv
[1];
187 for (next
= collection
; next
->name
; next
++)
189 test_return_t collection_rc
= TEST_SUCCESS
;
195 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
198 stats
.collection_total
++;
200 collection_rc
= world
.collection
.startup(world_ptr
);
202 if (collection_rc
!= TEST_SUCCESS
)
207 collection_rc
= world
.runner
->pre(next
->pre
, world_ptr
);
211 switch (collection_rc
)
214 fprintf(stderr
, "\n%s\n\n", next
->name
);
217 fprintf(stderr
, "\n%s [ failed ]\n\n", next
->name
);
218 stats
.collection_failed
++;
221 fprintf(stderr
, "\n%s [ skipping ]\n\n", next
->name
);
222 stats
.collection_skipped
++;
224 case TEST_MEMORY_ALLOCATION_FAILURE
:
225 case TEST_MAXIMUM_RETURN
:
232 for (x
= 0; run
->name
; run
++)
234 struct timeval start_time
, end_time
;
235 long int load_time
= 0;
237 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
240 fprintf(stderr
, "Testing %s", run
->name
);
242 if (world
.test
.startup
)
244 world
.test
.startup(world_ptr
);
247 if (run
->requires_flush
&& world
.test
.flush
)
249 world
.test
.flush(world_ptr
);
252 if (world
.test
.pre_run
)
254 world
.test
.pre_run(world_ptr
);
261 if (next
->pre
&& world
.runner
->pre
)
263 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
265 if (return_code
!= TEST_SUCCESS
)
272 gettimeofday(&start_time
, NULL
);
273 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
274 gettimeofday(&end_time
, NULL
);
275 load_time
= timedif(end_time
, start_time
);
278 if (next
->post
&& world
.runner
->post
)
280 (void) world
.runner
->post(next
->post
, world_ptr
);
285 if (world
.test
.post_run
)
287 world
.test
.post_run(world_ptr
);
292 fprintf(stderr
, "\t\t\t\t\t");
297 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
308 case TEST_MEMORY_ALLOCATION_FAILURE
:
309 fprintf(stderr
, "Exhausted memory, quitting\n");
311 case TEST_MAXIMUM_RETURN
:
313 assert(0); // Coding error.
317 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
319 if (world
.test
.on_error
)
322 rc
= world
.test
.on_error(return_code
, world_ptr
);
324 if (rc
!= TEST_SUCCESS
)
329 if (next
->post
&& world
.runner
->post
)
331 (void) world
.runner
->post(next
->post
, world_ptr
);
334 if (! failed
&& ! skipped
)
336 stats
.collection_success
++;
340 world
.collection
.shutdown(world_ptr
);
343 if (stats
.collection_failed
|| stats
.collection_skipped
)
345 fprintf(stderr
, "Some test failures and/or skipped test occurred.\n\n");
349 fprintf(stderr
, "All tests completed successfully\n\n");
355 error
= world
.destroy(world_ptr
);
357 if (error
!= TEST_SUCCESS
)
359 fprintf(stderr
, "Failure during shutdown.\n");
360 stats
.failed
++; // We do this to make our exit code return EXIT_FAILURE
364 world_stats_print(&stats
);
366 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
370 return stats
.failed
== 0 ? 0 : 1;