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 <libtest/test.h>
28 #include <libtest/failed.h>
30 static void world_stats_print(world_stats_st
*stats
)
33 fprintf(stderr
, "Total Collections\t\t\t\t%u\n", stats
->collection_total
);
34 fprintf(stderr
, "\tFailed Collections\t\t\t%u\n", stats
->collection_failed
);
35 fprintf(stderr
, "\tSkipped Collections\t\t\t%u\n", stats
->collection_skipped
);
36 fprintf(stderr
, "\tSucceeded Collections\t\t%u\n", stats
->collection_success
);
38 fprintf(stderr
, "Total\t\t\t\t%u\n", stats
->total
);
39 fprintf(stderr
, "\tFailed\t\t\t%u\n", stats
->failed
);
40 fprintf(stderr
, "\tSkipped\t\t\t%u\n", stats
->skipped
);
41 fprintf(stderr
, "\tSucceeded\t\t%u\n", stats
->success
);
44 long int timedif(struct timeval a
, struct timeval b
)
48 us
= (int)(a
.tv_usec
- b
.tv_usec
);
50 s
= (int)(a
.tv_sec
- b
.tv_sec
);
55 const char *test_strerror(test_return_t code
)
62 case TEST_MEMORY_ALLOCATION_FAILURE
:
63 return "memory allocation";
66 case TEST_MAXIMUM_RETURN
:
68 fprintf(stderr
, "Unknown return value\n");
73 void create_core(void)
75 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
85 while (waitpid(pid
, NULL
, 0) != pid
)
94 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
106 static world_runner_st defualt_runners
= {
112 static test_return_t
_default_callback(void *p
)
119 static inline void set_default_fn(test_callback_fn
*fn
)
123 *fn
= _default_callback
;
127 static collection_st
*init_world(world_st
*world
)
131 world
->runner
= &defualt_runners
;
134 set_default_fn(&world
->collection
.startup
);
135 set_default_fn(&world
->collection
.shutdown
);
137 return world
->collections
;
141 int main(int argc
, char *argv
[])
143 test_return_t return_code
;
145 char *collection_to_run
= NULL
;
146 char *wildcard
= NULL
;
148 collection_st
*collection
;
152 world_stats_st stats
;
154 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
155 if (sasl_client_init(NULL
) != SASL_OK
)
157 fprintf(stderr
, "Failed to initialize sasl library!\n");
162 memset(&stats
, 0, sizeof(stats
));
163 memset(&world
, 0, sizeof(world
));
166 collection
= init_world(&world
);
171 world_ptr
= world
.create(&error
);
172 if (error
!= TEST_SUCCESS
)
181 collection_to_run
= argv
[1];
186 for (next
= collection
; next
->name
; next
++)
188 test_return_t collection_rc
= TEST_SUCCESS
;
194 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
197 stats
.collection_total
++;
199 collection_rc
= world
.collection
.startup(world_ptr
);
201 if (collection_rc
!= TEST_SUCCESS
)
206 collection_rc
= world
.runner
->pre(next
->pre
, world_ptr
);
210 switch (collection_rc
)
213 fprintf(stderr
, "\n%s\n\n", next
->name
);
216 fprintf(stderr
, "\n%s [ failed ]\n\n", next
->name
);
217 stats
.collection_failed
++;
220 fprintf(stderr
, "\n%s [ skipping ]\n\n", next
->name
);
221 stats
.collection_skipped
++;
223 case TEST_MEMORY_ALLOCATION_FAILURE
:
224 case TEST_MAXIMUM_RETURN
:
231 for (x
= 0; run
->name
; run
++)
233 struct timeval start_time
, end_time
;
234 long int load_time
= 0;
236 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
239 fprintf(stderr
, "Testing %s", run
->name
);
241 if (world
.test
.startup
)
243 world
.test
.startup(world_ptr
);
246 if (run
->requires_flush
&& world
.test
.flush
)
248 world
.test
.flush(world_ptr
);
251 if (world
.test
.pre_run
)
253 world
.test
.pre_run(world_ptr
);
260 if (next
->pre
&& world
.runner
->pre
)
262 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
264 if (return_code
!= TEST_SUCCESS
)
271 gettimeofday(&start_time
, NULL
);
272 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
273 gettimeofday(&end_time
, NULL
);
274 load_time
= timedif(end_time
, start_time
);
277 if (next
->post
&& world
.runner
->post
)
279 (void) world
.runner
->post(next
->post
, world_ptr
);
284 if (world
.test
.post_run
)
286 world
.test
.post_run(world_ptr
);
291 fprintf(stderr
, "\t\t\t\t\t");
296 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
301 push_failed_test(next
->name
, run
->name
);
310 case TEST_MEMORY_ALLOCATION_FAILURE
:
311 fprintf(stderr
, "Exhausted memory, quitting\n");
313 case TEST_MAXIMUM_RETURN
:
315 assert(0); // Coding error.
319 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
321 if (world
.test
.on_error
)
324 rc
= world
.test
.on_error(return_code
, world_ptr
);
326 if (rc
!= TEST_SUCCESS
)
331 if (next
->post
&& world
.runner
->post
)
333 (void) world
.runner
->post(next
->post
, world_ptr
);
336 if (! failed
&& ! skipped
)
338 stats
.collection_success
++;
342 world
.collection
.shutdown(world_ptr
);
345 if (stats
.collection_failed
|| stats
.collection_skipped
)
347 fprintf(stderr
, "Some test failures and/or skipped test occurred.\n\n");
354 fprintf(stderr
, "All tests completed successfully\n\n");
360 error
= world
.destroy(world_ptr
);
362 if (error
!= TEST_SUCCESS
)
364 fprintf(stderr
, "Failure during shutdown.\n");
365 stats
.failed
++; // We do this to make our exit code return EXIT_FAILURE
369 world_stats_print(&stats
);
371 return stats
.failed
== 0 ? 0 : 1;