1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <libtest/common.h>
47 #include <sys/types.h>
57 #include <libtest/stats.h>
59 #ifndef __INTEL_COMPILER
60 #pragma GCC diagnostic ignored "-Wold-style-cast"
63 static in_port_t global_port
= 0;
64 static char global_socket
[1024];
66 in_port_t
default_port()
72 void set_default_port(in_port_t port
)
77 const char *default_socket()
79 assert(global_socket
[0]);
85 return (getenv("LIBTEST_LOCAL"));
88 void set_default_socket(const char *socket
)
90 strncpy(global_socket
, socket
, strlen(socket
));
93 static void stats_print(Stats
*stats
)
95 std::cout
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
<< std::endl
;
96 std::cout
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
<< std::endl
;
97 std::cout
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
<< std::endl
;
98 std::cout
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
<< std::endl
;
99 std::cout
<< std::endl
;
100 std::cout
<< "Total\t\t\t\t" << stats
->total
<< std::endl
;
101 std::cout
<< "\tFailed\t\t\t" << stats
->failed
<< std::endl
;
102 std::cout
<< "\tSkipped\t\t\t" << stats
->skipped
<< std::endl
;
103 std::cout
<< "\tSucceeded\t\t" << stats
->success
<< std::endl
;
106 static long int timedif(struct timeval a
, struct timeval b
)
110 us
= (long)(a
.tv_usec
- b
.tv_usec
);
112 s
= (long)(a
.tv_sec
- b
.tv_sec
);
117 const char *test_strerror(test_return_t code
)
126 case TEST_MEMORY_ALLOCATION_FAILURE
:
127 return "memory allocation";
139 void create_core(void)
141 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
151 while (waitpid(pid
, NULL
, 0) != pid
) {};
162 static Framework
*world
= NULL
;
163 static volatile shutdown_t __shutdown
= SHUTDOWN_RUNNING
;
164 pthread_mutex_t shutdown_mutex
= PTHREAD_MUTEX_INITIALIZER
;
166 static bool is_shutdown()
169 pthread_mutex_lock(&shutdown_mutex
);
170 ret
= bool(__shutdown
!= SHUTDOWN_RUNNING
);
171 pthread_mutex_unlock(&shutdown_mutex
);
176 static void set_shutdown(shutdown_t arg
)
178 pthread_mutex_lock(&shutdown_mutex
);
180 pthread_mutex_unlock(&shutdown_mutex
);
183 static void *sig_thread(void *arg
)
185 sigset_t
*set
= (sigset_t
*) arg
;
187 while (is_shutdown())
191 while ((error
= sigwait(set
, &sig
)) == EINTR
) ;
198 Error
<< "Signal handling thread got signal " << strsignal(sig
);
199 set_shutdown(SHUTDOWN_FORCED
);
203 Error
<< "Signal handling thread got unexpected signal " << strsignal(sig
);
213 static void setup_signals(pthread_t
& thread
)
218 sigaddset(&set
, SIGSEGV
);
219 sigaddset(&set
, SIGABRT
);
220 sigaddset(&set
, SIGINT
);
221 sigaddset(&set
, SIGUSR1
);
224 if ((error
= pthread_sigmask(SIG_BLOCK
, &set
, NULL
)) != 0)
226 Error
<< " died during pthread_sigmask(" << strerror(error
) << ")";
230 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, (void *) &set
)) != 0)
232 Error
<< " died during pthread_create(" << strerror(error
) << ")";
238 int main(int argc
, char *argv
[])
240 world
= new Framework();
248 setup_signals(thread
);
255 void *creators_ptr
= world
->create(error
);
256 if (test_failed(error
))
258 Error
<< "create() failed";
262 char *collection_to_run
= NULL
;
265 collection_to_run
= argv
[1];
267 else if (getenv("TEST_COLLECTION"))
269 collection_to_run
= getenv("TEST_COLLECTION");
272 if (collection_to_run
)
274 std::cout
<< "Only testing " << collection_to_run
<< std::endl
;
277 char *wildcard
= NULL
;
283 for (collection_st
*next
= world
->collections
; next
->name
and (not is_shutdown()); next
++)
285 test_return_t collection_rc
= TEST_SUCCESS
;
289 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
292 stats
.collection_total
++;
294 collection_rc
= world
->startup(creators_ptr
);
296 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
298 collection_rc
= world
->runner
->pre(next
->pre
, creators_ptr
);
301 switch (collection_rc
)
308 Error
<< next
->name
<< " [ failed ]";
309 stats
.collection_failed
++;
313 Log
<< next
->name
<< " [ skipping ]";
314 stats
.collection_skipped
++;
317 case TEST_MEMORY_ALLOCATION_FAILURE
:
318 test_assert(0, "Allocation failure, or unknown return");
321 Log
<< "Collection: " << next
->name
;
323 for (test_st
*run
= next
->tests
; run
->name
; run
++)
325 struct timeval start_time
, end_time
;
326 long int load_time
= 0;
328 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
333 test_return_t return_code
;
334 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
336 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
338 // @note pre will fail is SKIPPED is returned
339 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
342 gettimeofday(&start_time
, NULL
);
343 return_code
= world
->runner
->run(run
->test_fn
, creators_ptr
);
344 gettimeofday(&end_time
, NULL
);
345 load_time
= timedif(end_time
, start_time
);
349 // @todo do something if post fails
350 (void)world
->item
.post(creators_ptr
);
354 Error
<< " item.flush(failure)";
359 Error
<< " item.startup(failure)";
367 Log
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
375 Error
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
381 Log
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
384 case TEST_MEMORY_ALLOCATION_FAILURE
:
385 test_assert(0, "Memory Allocation Error");
388 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
390 Error
<< "Failed while running on_error()";
397 if (next
->post
and world
->runner
->post
)
399 (void) world
->runner
->post(next
->post
, creators_ptr
);
402 if (failed
== 0 and skipped
== 0)
404 stats
.collection_success
++;
408 world
->shutdown(creators_ptr
);
411 if (not is_shutdown())
413 set_shutdown(SHUTDOWN_GRACEFUL
);
414 pthread_kill(thread
, SIGUSR1
);
417 if (__shutdown
== SHUTDOWN_FORCED
)
419 Error
<< "Tests were aborted.";
421 else if (stats
.collection_failed
or stats
.collection_skipped
)
423 Error
<< "Some test failures and/or skipped test occurred.";
427 Log
<< "All tests completed successfully.";
433 pthread_join(thread
, &retval
);
437 return stats
.failed
== 0 and __shutdown
== SHUTDOWN_GRACEFUL
? EXIT_SUCCESS
: EXIT_FAILURE
;