2 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
11 #include <libtest/common.h>
17 #include <sys/types.h>
28 #include <libtest/stats.h>
30 #ifndef __INTEL_COMPILER
31 #pragma GCC diagnostic ignored "-Wold-style-cast"
34 static in_port_t global_port
= 0;
35 static char global_socket
[1024];
37 in_port_t
default_port()
43 void set_default_port(in_port_t port
)
48 const char *default_socket()
50 assert(global_socket
[0]);
54 void set_default_socket(const char *socket
)
56 strncpy(global_socket
, socket
, strlen(socket
));
59 static void stats_print(Stats
*stats
)
61 std::cout
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
<< std::endl
;
62 std::cout
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
<< std::endl
;
63 std::cout
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
<< std::endl
;
64 std::cout
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
<< std::endl
;
65 std::cout
<< std::endl
;
66 std::cout
<< "Total\t\t\t\t" << stats
->total
<< std::endl
;
67 std::cout
<< "\tFailed\t\t\t" << stats
->failed
<< std::endl
;
68 std::cout
<< "\tSkipped\t\t\t" << stats
->skipped
<< std::endl
;
69 std::cout
<< "\tSucceeded\t\t" << stats
->success
<< std::endl
;
72 static long int timedif(struct timeval a
, struct timeval b
)
76 us
= (long)(a
.tv_usec
- b
.tv_usec
);
78 s
= (long)(a
.tv_sec
- b
.tv_sec
);
83 const char *test_strerror(test_return_t code
)
92 case TEST_MEMORY_ALLOCATION_FAILURE
:
93 return "memory allocation";
105 void create_core(void)
107 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
117 while (waitpid(pid
, NULL
, 0) != pid
) {};
128 static Framework
*world
= NULL
;
129 static volatile shutdown_t __shutdown
= SHUTDOWN_RUNNING
;
131 static void *sig_thread(void *arg
)
133 sigset_t
*set
= (sigset_t
*) arg
;
135 for (;__shutdown
== SHUTDOWN_RUNNING
;)
139 while ((error
= sigwait(set
, &sig
)) == EINTR
) ;
141 std::cerr
<< std::endl
<< "Signal handling thread got signal " << strsignal(sig
) << std::endl
;
147 __shutdown
= SHUTDOWN_FORCED
;
158 static void setup_signals(pthread_t
& thread
)
163 sigaddset(&set
, SIGSEGV
);
164 sigaddset(&set
, SIGABRT
);
165 sigaddset(&set
, SIGINT
);
168 if ((error
= pthread_sigmask(SIG_BLOCK
, &set
, NULL
)) != 0)
170 std::cerr
<< __FILE__
<< ":" << __LINE__
<< " died during pthread_sigmask(" << strerror(error
) << ")" << std::endl
;
174 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, (void *) &set
)) != 0)
176 std::cerr
<< __FILE__
<< ":" << __LINE__
<< " died during pthread_create(" << strerror(error
) << ")" << std::endl
;
182 int main(int argc
, char *argv
[])
184 world
= new Framework();
192 setup_signals(thread
);
199 void *creators_ptr
= world
->create(&error
);
200 if (test_failed(error
))
202 std::cerr
<< "create() failed" << std::endl
;
206 char *collection_to_run
= NULL
;
209 collection_to_run
= argv
[1];
211 else if (getenv("TEST_COLLECTION"))
213 collection_to_run
= getenv("TEST_COLLECTION");
216 if (collection_to_run
)
218 std::cout
<< "Only testing " << collection_to_run
<< std::endl
;
221 char *wildcard
= NULL
;
227 for (collection_st
*next
= world
->collections
; next
->name
and __shutdown
== SHUTDOWN_RUNNING
; next
++)
229 test_return_t collection_rc
= TEST_SUCCESS
;
233 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
236 stats
.collection_total
++;
238 collection_rc
= world
->startup(creators_ptr
);
240 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
242 collection_rc
= world
->runner
->pre(next
->pre
, creators_ptr
);
245 switch (collection_rc
)
248 std::cerr
<< std::endl
<< next
->name
<< std::endl
<< std::endl
;
253 std::cerr
<< std::endl
<< next
->name
<< " [ failed ]" << std::endl
<< std::endl
;
254 stats
.collection_failed
++;
258 std::cerr
<< std::endl
<< next
->name
<< " [ skipping ]" << std::endl
<< std::endl
;
259 stats
.collection_skipped
++;
262 case TEST_MEMORY_ALLOCATION_FAILURE
:
263 test_assert(0, "Allocation failure, or unknown return");
266 for (test_st
*run
= next
->tests
; run
->name
; run
++)
268 struct timeval start_time
, end_time
;
269 long int load_time
= 0;
271 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
276 std::cerr
<< "\tTesting " << run
->name
;
278 test_return_t return_code
;
279 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
281 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
283 // @note pre will fail is SKIPPED is returned
284 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
287 gettimeofday(&start_time
, NULL
);
288 return_code
= world
->runner
->run(run
->test_fn
, creators_ptr
);
289 gettimeofday(&end_time
, NULL
);
290 load_time
= timedif(end_time
, start_time
);
294 // @todo do something if post fails
295 (void)world
->item
.post(creators_ptr
);
299 std::cerr
<< __FILE__
<< ":" << __LINE__
<< " item.flush(failure)" << std::endl
;
304 std::cerr
<< __FILE__
<< ":" << __LINE__
<< " item.startup(failure)" << std::endl
;
309 std::cerr
<< "\t\t\t\t\t";
314 std::cerr
<< load_time
/ 1000 << "." << load_time
% 1000;
329 case TEST_MEMORY_ALLOCATION_FAILURE
:
330 test_assert(0, "Memory Allocation Error");
333 std::cerr
<< "[ " << test_strerror(return_code
) << " ]" << std::endl
;
335 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
341 if (next
->post
and world
->runner
->post
)
343 (void) world
->runner
->post(next
->post
, creators_ptr
);
346 if (failed
== 0 and skipped
== 0)
348 stats
.collection_success
++;
352 world
->shutdown(creators_ptr
);
355 if (__shutdown
== SHUTDOWN_RUNNING
)
357 __shutdown
= SHUTDOWN_GRACEFUL
;
360 if (__shutdown
== SHUTDOWN_FORCED
)
362 std::cerr
<< std::endl
<< std::endl
<< "Tests were aborted." << std::endl
<< std::endl
;
364 else if (stats
.collection_failed
or stats
.collection_skipped
)
366 std::cerr
<< std::endl
<< std::endl
<< "Some test failures and/or skipped test occurred." << std::endl
<< std::endl
;
370 std::cout
<< std::endl
<< std::endl
<< "All tests completed successfully." << std::endl
<< std::endl
;
377 return stats
.failed
== 0 and __shutdown
== SHUTDOWN_GRACEFUL
? EXIT_SUCCESS
: EXIT_FAILURE
;