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
;
129 world_ptr
= world
.create(&error
);
130 if (error
!= TEST_SUCCESS
)
139 collection_to_run
= argv
[1];
144 for (next
= collection
; next
->name
; next
++)
149 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
152 fprintf(stderr
, "\n%s\n\n", next
->name
);
154 for (x
= 0; run
->name
; run
++)
156 struct timeval start_time
, end_time
;
157 long int load_time
= 0;
159 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
162 fprintf(stderr
, "Testing %s", run
->name
);
164 if (world
.collection_startup
)
166 world
.collection_startup(world_ptr
);
169 if (run
->requires_flush
&& world
.flush
)
171 world
.flush(world_ptr
);
176 world
.pre_run(world_ptr
);
180 if (next
->pre
&& world
.runner
->pre
)
182 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
184 if (return_code
!= TEST_SUCCESS
)
190 gettimeofday(&start_time
, NULL
);
191 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
192 gettimeofday(&end_time
, NULL
);
193 load_time
= timedif(end_time
, start_time
);
195 if (next
->post
&& world
.runner
->post
)
197 (void) world
.runner
->post(next
->post
, world_ptr
);
202 world
.post_run(world_ptr
);
208 fprintf(stderr
, "\t\t\t\t\t");
213 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
222 case TEST_MEMORY_ALLOCATION_FAILURE
:
223 case TEST_MAXIMUM_RETURN
:
228 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
233 rc
= world
.on_error(return_code
, world_ptr
);
235 if (rc
!= TEST_SUCCESS
)
241 fprintf(stderr
, "All tests completed successfully\n\n");
246 error
= world
.destroy(world_ptr
);
248 if (error
!= TEST_SUCCESS
)
250 fprintf(stderr
, "Failure during shutdown.\n");
251 stats
.failed
++; // We do this to make our exit code return 1
255 world_stats_print(&stats
);
257 return stats
.failed
== 0 ? 0 : 1;