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>
30 #include <libtest/failed.h>
32 static void world_stats_print(world_stats_st
*stats
)
35 fprintf(stderr
, "Total Collections\t\t\t\t%u\n", stats
->collection_total
);
36 fprintf(stderr
, "\tFailed Collections\t\t\t%u\n", stats
->collection_failed
);
37 fprintf(stderr
, "\tSkipped Collections\t\t\t%u\n", stats
->collection_skipped
);
38 fprintf(stderr
, "\tSucceeded Collections\t\t%u\n", stats
->collection_success
);
40 fprintf(stderr
, "Total\t\t\t\t%u\n", stats
->total
);
41 fprintf(stderr
, "\tFailed\t\t\t%u\n", stats
->failed
);
42 fprintf(stderr
, "\tSkipped\t\t\t%u\n", stats
->skipped
);
43 fprintf(stderr
, "\tSucceeded\t\t%u\n", stats
->success
);
46 long int timedif(struct timeval a
, struct timeval b
)
50 us
= (int)(a
.tv_usec
- b
.tv_usec
);
52 s
= (int)(a
.tv_sec
- b
.tv_sec
);
57 const char *test_strerror(test_return_t code
)
64 case TEST_MEMORY_ALLOCATION_FAILURE
:
65 return "memory allocation";
68 case TEST_MAXIMUM_RETURN
:
70 fprintf(stderr
, "Unknown return value\n");
75 void create_core(void)
77 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
87 while (waitpid(pid
, NULL
, 0) != pid
)
96 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
108 static world_runner_st defualt_runners
= {
114 static test_return_t
_default_callback(void *p
)
121 static inline void set_default_fn(test_callback_fn
*fn
)
125 *fn
= _default_callback
;
129 static collection_st
*init_world(world_st
*world
)
133 world
->runner
= &defualt_runners
;
136 set_default_fn(&world
->collection
.startup
);
137 set_default_fn(&world
->collection
.shutdown
);
139 return world
->collections
;
143 int main(int argc
, char *argv
[])
145 test_return_t return_code
;
147 char *collection_to_run
= NULL
;
148 char *wildcard
= NULL
;
150 collection_st
*collection
;
154 world_stats_st stats
;
156 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
157 if (sasl_client_init(NULL
) != SASL_OK
)
159 fprintf(stderr
, "Failed to initialize sasl library!\n");
164 memset(&stats
, 0, sizeof(stats
));
165 memset(&world
, 0, sizeof(world
));
168 collection
= init_world(&world
);
173 world_ptr
= world
.create(&error
);
174 if (error
!= TEST_SUCCESS
)
183 collection_to_run
= argv
[1];
188 for (next
= collection
; next
->name
; next
++)
190 test_return_t collection_rc
= TEST_SUCCESS
;
196 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
199 stats
.collection_total
++;
201 collection_rc
= world
.collection
.startup(world_ptr
);
203 if (collection_rc
!= TEST_SUCCESS
)
208 collection_rc
= world
.runner
->pre(next
->pre
, world_ptr
);
212 switch (collection_rc
)
215 fprintf(stderr
, "\n%s\n\n", next
->name
);
218 fprintf(stderr
, "\n%s [ failed ]\n\n", next
->name
);
219 stats
.collection_failed
++;
222 fprintf(stderr
, "\n%s [ skipping ]\n\n", next
->name
);
223 stats
.collection_skipped
++;
225 case TEST_MEMORY_ALLOCATION_FAILURE
:
226 case TEST_MAXIMUM_RETURN
:
233 for (x
= 0; run
->name
; run
++)
235 struct timeval start_time
, end_time
;
236 long int load_time
= 0;
238 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
241 fprintf(stderr
, "Testing %s", run
->name
);
243 if (world
.test
.startup
)
245 world
.test
.startup(world_ptr
);
248 if (run
->requires_flush
&& world
.test
.flush
)
250 world
.test
.flush(world_ptr
);
253 if (world
.test
.pre_run
)
255 world
.test
.pre_run(world_ptr
);
262 if (next
->pre
&& world
.runner
->pre
)
264 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
266 if (return_code
!= TEST_SUCCESS
)
273 gettimeofday(&start_time
, NULL
);
274 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
275 gettimeofday(&end_time
, NULL
);
276 load_time
= timedif(end_time
, start_time
);
279 if (next
->post
&& world
.runner
->post
)
281 (void) world
.runner
->post(next
->post
, world_ptr
);
286 if (world
.test
.post_run
)
288 world
.test
.post_run(world_ptr
);
293 fprintf(stderr
, "\t\t\t\t\t");
298 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
303 push_failed_test(next
->name
, run
->name
);
312 case TEST_MEMORY_ALLOCATION_FAILURE
:
313 fprintf(stderr
, "Exhausted memory, quitting\n");
315 case TEST_MAXIMUM_RETURN
:
317 assert(0); // Coding error.
321 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
323 if (world
.test
.on_error
)
326 rc
= world
.test
.on_error(return_code
, world_ptr
);
328 if (rc
!= TEST_SUCCESS
)
333 if (next
->post
&& world
.runner
->post
)
335 (void) world
.runner
->post(next
->post
, world_ptr
);
338 if (! failed
&& ! skipped
)
340 stats
.collection_success
++;
344 world
.collection
.shutdown(world_ptr
);
347 if (stats
.collection_failed
|| stats
.collection_skipped
)
349 fprintf(stderr
, "Some test failures and/or skipped test occurred.\n\n");
356 fprintf(stderr
, "All tests completed successfully\n\n");
362 error
= world
.destroy(world_ptr
);
364 if (error
!= TEST_SUCCESS
)
366 fprintf(stderr
, "Failure during shutdown.\n");
367 stats
.failed
++; // We do this to make our exit code return EXIT_FAILURE
371 world_stats_print(&stats
);
373 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
377 return stats
.failed
== 0 ? 0 : 1;