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
)
182 collection_to_run
= argv
[1];
184 else if (getenv("TEST_COLLECTION"))
186 collection_to_run
= getenv("TEST_COLLECTION");
189 if (collection_to_run
)
190 printf("Only testing %s\n", collection_to_run
);
197 for (next
= collection
; next
->name
; next
++)
199 test_return_t collection_rc
= TEST_SUCCESS
;
205 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
208 stats
.collection_total
++;
210 collection_rc
= world
.collection
.startup(world_ptr
);
212 if (collection_rc
!= TEST_SUCCESS
)
217 collection_rc
= world
.runner
->pre(next
->pre
, world_ptr
);
221 switch (collection_rc
)
224 fprintf(stderr
, "\n%s\n\n", next
->name
);
227 fprintf(stderr
, "\n%s [ failed ]\n\n", next
->name
);
228 stats
.collection_failed
++;
231 fprintf(stderr
, "\n%s [ skipping ]\n\n", next
->name
);
232 stats
.collection_skipped
++;
234 case TEST_MEMORY_ALLOCATION_FAILURE
:
235 case TEST_MAXIMUM_RETURN
:
242 for (x
= 0; run
->name
; run
++)
244 struct timeval start_time
, end_time
;
245 long int load_time
= 0;
247 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
250 fprintf(stderr
, "Testing %s", run
->name
);
252 if (world
.test
.startup
)
254 world
.test
.startup(world_ptr
);
257 if (run
->requires_flush
&& world
.test
.flush
)
259 world
.test
.flush(world_ptr
);
262 if (world
.test
.pre_run
)
264 world
.test
.pre_run(world_ptr
);
271 if (next
->pre
&& world
.runner
->pre
)
273 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
275 if (return_code
!= TEST_SUCCESS
)
282 gettimeofday(&start_time
, NULL
);
283 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
284 gettimeofday(&end_time
, NULL
);
285 load_time
= timedif(end_time
, start_time
);
288 if (next
->post
&& world
.runner
->post
)
290 (void) world
.runner
->post(next
->post
, world_ptr
);
295 if (world
.test
.post_run
)
297 world
.test
.post_run(world_ptr
);
302 fprintf(stderr
, "\t\t\t\t\t");
307 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
312 push_failed_test(next
->name
, run
->name
);
321 case TEST_MEMORY_ALLOCATION_FAILURE
:
322 fprintf(stderr
, "Exhausted memory, quitting\n");
324 case TEST_MAXIMUM_RETURN
:
326 assert(0); // Coding error.
330 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
332 if (world
.test
.on_error
)
335 rc
= world
.test
.on_error(return_code
, world_ptr
);
337 if (rc
!= TEST_SUCCESS
)
342 if (next
->post
&& world
.runner
->post
)
344 (void) world
.runner
->post(next
->post
, world_ptr
);
347 if (! failed
&& ! skipped
)
349 stats
.collection_success
++;
353 world
.collection
.shutdown(world_ptr
);
356 if (stats
.collection_failed
|| stats
.collection_skipped
)
358 fprintf(stderr
, "Some test failures and/or skipped test occurred.\n\n");
365 fprintf(stderr
, "All tests completed successfully\n\n");
371 error
= world
.destroy(world_ptr
);
373 if (error
!= TEST_SUCCESS
)
375 fprintf(stderr
, "Failure during shutdown.\n");
376 stats
.failed
++; // We do this to make our exit code return EXIT_FAILURE
380 world_stats_print(&stats
);
382 return stats
.failed
== 0 ? 0 : 1;