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>
24 #include "libmemcached/memcached.h"
27 static void world_stats_print(world_stats_st
*stats
)
30 fprintf(stderr
, "Total Collections\t\t\t\t%u\n", stats
->collection_total
);
31 fprintf(stderr
, "\tFailed Collections\t\t\t%u\n", stats
->collection_failed
);
32 fprintf(stderr
, "\tSkipped Collections\t\t\t%u\n", stats
->collection_skipped
);
33 fprintf(stderr
, "\tSucceeded Collections\t\t%u\n", stats
->collection_success
);
35 fprintf(stderr
, "Total\t\t\t\t%u\n", stats
->total
);
36 fprintf(stderr
, "\tFailed\t\t\t%u\n", stats
->failed
);
37 fprintf(stderr
, "\tSkipped\t\t\t%u\n", stats
->skipped
);
38 fprintf(stderr
, "\tSucceeded\t\t%u\n", stats
->success
);
41 static long int timedif(struct timeval a
, struct timeval b
)
45 us
= (int)(a
.tv_usec
- b
.tv_usec
);
47 s
= (int)(a
.tv_sec
- b
.tv_sec
);
52 const char *test_strerror(test_return_t code
)
59 case TEST_MEMORY_ALLOCATION_FAILURE
:
60 return "memory allocation";
63 case TEST_MAXIMUM_RETURN
:
65 fprintf(stderr
, "Unknown return value\n");
70 void create_core(void)
72 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
82 while (waitpid(pid
, NULL
, 0) != pid
)
91 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
103 static world_runner_st defualt_runners
= {
109 static test_return_t
_default_callback(void *p
)
116 static inline void set_default_fn(test_callback_fn
*fn
)
120 *fn
= _default_callback
;
124 static collection_st
*init_world(world_st
*world
)
128 world
->runner
= &defualt_runners
;
131 set_default_fn(&world
->collection
.startup
);
132 set_default_fn(&world
->collection
.shutdown
);
134 return world
->collections
;
138 int main(int argc
, char *argv
[])
140 test_return_t return_code
;
142 char *collection_to_run
= NULL
;
143 char *wildcard
= NULL
;
145 collection_st
*collection
;
149 world_stats_st stats
;
151 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
152 if (sasl_client_init(NULL
) != SASL_OK
)
154 fprintf(stderr
, "Failed to initialize sasl library!\n");
159 memset(&stats
, 0, sizeof(stats
));
160 memset(&world
, 0, sizeof(world
));
163 collection
= init_world(&world
);
168 world_ptr
= world
.create(&error
);
169 if (error
!= TEST_SUCCESS
)
178 collection_to_run
= argv
[1];
183 for (next
= collection
; next
->name
; next
++)
185 test_return_t collection_rc
= TEST_SUCCESS
;
191 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
194 stats
.collection_total
++;
196 collection_rc
= world
.collection
.startup(world_ptr
);
198 if (collection_rc
!= TEST_SUCCESS
)
203 collection_rc
= world
.runner
->pre(next
->pre
, world_ptr
);
207 switch (collection_rc
)
210 fprintf(stderr
, "\n%s\n\n", next
->name
);
213 fprintf(stderr
, "\n%s [ failed ]\n\n", next
->name
);
214 stats
.collection_failed
++;
217 fprintf(stderr
, "\n%s [ skipping ]\n\n", next
->name
);
218 stats
.collection_skipped
++;
220 case TEST_MEMORY_ALLOCATION_FAILURE
:
221 case TEST_MAXIMUM_RETURN
:
228 for (x
= 0; run
->name
; run
++)
230 struct timeval start_time
, end_time
;
231 long int load_time
= 0;
233 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
236 fprintf(stderr
, "Testing %s", run
->name
);
238 if (world
.test
.startup
)
240 world
.test
.startup(world_ptr
);
243 if (run
->requires_flush
&& world
.test
.flush
)
245 world
.test
.flush(world_ptr
);
248 if (world
.test
.pre_run
)
250 world
.test
.pre_run(world_ptr
);
257 if (next
->pre
&& world
.runner
->pre
)
259 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
261 if (return_code
!= TEST_SUCCESS
)
268 gettimeofday(&start_time
, NULL
);
269 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
270 gettimeofday(&end_time
, NULL
);
271 load_time
= timedif(end_time
, start_time
);
274 if (next
->post
&& world
.runner
->post
)
276 (void) world
.runner
->post(next
->post
, world_ptr
);
281 if (world
.test
.post_run
)
283 world
.test
.post_run(world_ptr
);
288 fprintf(stderr
, "\t\t\t\t\t");
293 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
304 case TEST_MEMORY_ALLOCATION_FAILURE
:
305 fprintf(stderr
, "Exhausted memory, quitting\n");
307 case TEST_MAXIMUM_RETURN
:
309 assert(0); // Coding error.
313 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
315 if (world
.test
.on_error
)
318 rc
= world
.test
.on_error(return_code
, world_ptr
);
320 if (rc
!= TEST_SUCCESS
)
325 if (next
->post
&& world
.runner
->post
)
327 (void) world
.runner
->post(next
->post
, world_ptr
);
330 if (! failed
&& ! skipped
)
332 stats
.collection_success
++;
336 world
.collection
.shutdown(world_ptr
);
339 if (stats
.collection_failed
|| stats
.collection_skipped
)
341 fprintf(stderr
, "Some test failures and/or skipped test occurred.\n\n");
345 fprintf(stderr
, "All tests completed successfully\n\n");
351 error
= world
.destroy(world_ptr
);
353 if (error
!= TEST_SUCCESS
)
355 fprintf(stderr
, "Failure during shutdown.\n");
356 stats
.failed
++; // We do this to make our exit code return 1
360 world_stats_print(&stats
);
362 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
366 return stats
.failed
== 0 ? 0 : 1;