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.
16 #include <sys/types.h>
26 static void world_stats_print(world_stats_st
*stats
)
29 fprintf(stderr
, "Total Collections\t\t\t\t%u\n", stats
->collection_total
);
30 fprintf(stderr
, "\tFailed Collections\t\t\t%u\n", stats
->collection_failed
);
31 fprintf(stderr
, "\tSkipped Collections\t\t\t%u\n", stats
->collection_skipped
);
32 fprintf(stderr
, "\tSucceeded Collections\t\t%u\n", stats
->collection_success
);
34 fprintf(stderr
, "Total\t\t\t\t%u\n", stats
->total
);
35 fprintf(stderr
, "\tFailed\t\t\t%u\n", stats
->failed
);
36 fprintf(stderr
, "\tSkipped\t\t\t%u\n", stats
->skipped
);
37 fprintf(stderr
, "\tSucceeded\t\t%u\n", stats
->success
);
40 static long int timedif(struct timeval a
, struct timeval b
)
44 us
= (int)(a
.tv_usec
- b
.tv_usec
);
46 s
= (int)(a
.tv_sec
- b
.tv_sec
);
51 const char *test_strerror(test_return_t code
)
58 case TEST_MEMORY_ALLOCATION_FAILURE
:
59 return "memory allocation";
62 case TEST_MAXIMUM_RETURN
:
64 fprintf(stderr
, "Unknown return value\n");
69 void create_core(void)
71 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
81 while (waitpid(pid
, NULL
, 0) != pid
)
90 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
102 static world_runner_st defualt_runners
= {
108 static test_return_t
_default_callback(void *p
)
115 static inline void set_default_fn(test_callback_fn
*fn
)
119 *fn
= _default_callback
;
123 static collection_st
*init_world(world_st
*world
)
127 world
->runner
= &defualt_runners
;
130 set_default_fn(&world
->collection
.startup
);
131 set_default_fn(&world
->collection
.shutdown
);
133 return world
->collections
;
137 int main(int argc
, char *argv
[])
139 test_return_t return_code
;
141 char *collection_to_run
= NULL
;
142 char *wildcard
= NULL
;
144 collection_st
*collection
;
148 world_stats_st stats
;
150 memset(&stats
, 0, sizeof(stats
));
151 memset(&world
, 0, sizeof(world
));
154 collection
= init_world(&world
);
159 world_ptr
= world
.create(&error
);
160 if (error
!= TEST_SUCCESS
)
169 collection_to_run
= argv
[1];
174 for (next
= collection
; next
->name
; next
++)
176 test_return_t collection_rc
= TEST_SUCCESS
;
182 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
185 stats
.collection_total
++;
187 collection_rc
= world
.collection
.startup(world_ptr
);
189 if (collection_rc
!= TEST_SUCCESS
)
194 collection_rc
= world
.runner
->pre(next
->pre
, world_ptr
);
198 switch (collection_rc
)
201 fprintf(stderr
, "\n%s\n\n", next
->name
);
204 fprintf(stderr
, "\n%s [ failed ]\n\n", next
->name
);
205 stats
.collection_failed
++;
208 fprintf(stderr
, "\n%s [ skipping ]\n\n", next
->name
);
209 stats
.collection_skipped
++;
211 case TEST_MEMORY_ALLOCATION_FAILURE
:
212 case TEST_MAXIMUM_RETURN
:
219 for (x
= 0; run
->name
; run
++)
221 struct timeval start_time
, end_time
;
222 long int load_time
= 0;
224 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
227 fprintf(stderr
, "Testing %s", run
->name
);
229 if (world
.test
.startup
)
231 world
.test
.startup(world_ptr
);
234 if (run
->requires_flush
&& world
.test
.flush
)
236 world
.test
.flush(world_ptr
);
239 if (world
.test
.pre_run
)
241 world
.test
.pre_run(world_ptr
);
248 if (next
->pre
&& world
.runner
->pre
)
250 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
252 if (return_code
!= TEST_SUCCESS
)
259 gettimeofday(&start_time
, NULL
);
260 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
261 gettimeofday(&end_time
, NULL
);
262 load_time
= timedif(end_time
, start_time
);
265 if (next
->post
&& world
.runner
->post
)
267 (void) world
.runner
->post(next
->post
, world_ptr
);
272 if (world
.test
.post_run
)
274 world
.test
.post_run(world_ptr
);
279 fprintf(stderr
, "\t\t\t\t\t");
284 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
295 case TEST_MEMORY_ALLOCATION_FAILURE
:
296 fprintf(stderr
, "Exhausted memory, quitting\n");
298 case TEST_MAXIMUM_RETURN
:
300 assert(0); // Coding error.
304 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
306 if (world
.test
.on_error
)
309 rc
= world
.test
.on_error(return_code
, world_ptr
);
311 if (rc
!= TEST_SUCCESS
)
316 if (next
->post
&& world
.runner
->post
)
318 (void) world
.runner
->post(next
->post
, world_ptr
);
321 if (! failed
&& ! skipped
)
323 stats
.collection_success
++;
327 world
.collection
.shutdown(world_ptr
);
330 if (stats
.collection_failed
|| stats
.collection_skipped
)
332 fprintf(stderr
, "Some test failures and/or skipped test occurred.\n\n");
336 fprintf(stderr
, "All tests completed successfully\n\n");
342 error
= world
.destroy(world_ptr
);
344 if (error
!= TEST_SUCCESS
)
346 fprintf(stderr
, "Failure during shutdown.\n");
347 stats
.failed
++; // We do this to make our exit code return 1
351 world_stats_print(&stats
);
353 return stats
.failed
== 0 ? 0 : 1;