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>
25 static void world_stats_print(world_stats_st
*stats
)
27 fprintf(stderr
, "Total\t\t\t\t%u\n", stats
->total
);
28 fprintf(stderr
, "\tFailed\t\t\t%u\n", stats
->failed
);
29 fprintf(stderr
, "\tSkipped\t\t\t%u\n", stats
->skipped
);
30 fprintf(stderr
, "\tSucceeded\t\t%u\n", stats
->success
);
33 static long int timedif(struct timeval a
, struct timeval b
)
37 us
= (int)(a
.tv_usec
- b
.tv_usec
);
39 s
= (int)(a
.tv_sec
- b
.tv_sec
);
44 const char *test_strerror(test_return_t code
)
51 case TEST_MEMORY_ALLOCATION_FAILURE
:
52 return "memory allocation";
55 case TEST_MAXIMUM_RETURN
:
57 fprintf(stderr
, "Unknown return value\n");
62 void create_core(void)
64 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
74 while (waitpid(pid
, NULL
, 0) != pid
)
83 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
95 static world_runner_st defualt_runners
= {
102 int main(int argc
, char *argv
[])
104 test_return_t return_code
;
106 char *collection_to_run
= NULL
;
107 char *wildcard
= NULL
;
109 collection_st
*collection
;
113 world_stats_st stats
;
115 memset(&stats
, 0, sizeof(stats
));
116 memset(&world
, 0, sizeof(world
));
121 world
.runner
= &defualt_runners
;
124 collection
= world
.collections
;
127 world_ptr
= world
.create();
132 collection_to_run
= argv
[1];
137 for (next
= collection
; next
->name
; next
++)
142 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
145 fprintf(stderr
, "\n%s\n\n", next
->name
);
147 for (x
= 0; run
->name
; run
++)
149 struct timeval start_time
, end_time
;
150 long int load_time
= 0;
152 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
155 fprintf(stderr
, "Testing %s", run
->name
);
157 if (world
.collection_startup
)
159 world
.collection_startup(world_ptr
);
162 if (run
->requires_flush
&& world
.flush
)
164 world
.flush(world_ptr
);
169 world
.pre_run(world_ptr
);
173 if (next
->pre
&& world
.runner
->pre
)
175 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
177 if (return_code
!= TEST_SUCCESS
)
183 gettimeofday(&start_time
, NULL
);
184 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
185 gettimeofday(&end_time
, NULL
);
186 load_time
= timedif(end_time
, start_time
);
188 if (next
->post
&& world
.runner
->post
)
190 (void) world
.runner
->post(next
->post
, world_ptr
);
195 world
.post_run(world_ptr
);
201 fprintf(stderr
, "\t\t\t\t\t");
206 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
215 case TEST_MEMORY_ALLOCATION_FAILURE
:
216 case TEST_MAXIMUM_RETURN
:
221 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
226 rc
= world
.on_error(return_code
, world_ptr
);
228 if (rc
!= TEST_SUCCESS
)
234 fprintf(stderr
, "All tests completed successfully\n\n");
237 world
.destroy(world_ptr
);
239 world_stats_print(&stats
);
241 return stats
.failed
== 0 ? 0 : 1;