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.
38 #include <libtest/common.h>
44 #include <sys/types.h>
54 #include <libtest/stats.h>
55 #include <libtest/signal.h>
57 #ifndef __INTEL_COMPILER
58 #pragma GCC diagnostic ignored "-Wold-style-cast"
61 using namespace libtest
;
63 static in_port_t global_port
= 0;
64 static char global_socket
[1024];
66 in_port_t
default_port()
71 void set_default_port(in_port_t port
)
76 const char *default_socket()
78 assert(global_socket
[0]);
84 return (getenv("LIBTEST_LOCAL"));
87 void set_default_socket(const char *socket
)
91 strncpy(global_socket
, socket
, strlen(socket
));
95 static void stats_print(Stats
*stats
)
97 if (stats
->collection_failed
== 0 and stats
->collection_success
== 0)
102 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
103 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
104 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
105 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
107 Out
<< "Total\t\t\t\t" << stats
->total
;
108 Out
<< "\tFailed\t\t\t" << stats
->failed
;
109 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
110 Out
<< "\tSucceeded\t\t" << stats
->success
;
113 static long int timedif(struct timeval a
, struct timeval b
)
117 us
= (long)(a
.tv_usec
- b
.tv_usec
);
119 s
= (long)(a
.tv_sec
- b
.tv_sec
);
124 const char *test_strerror(test_return_t code
)
133 case TEST_MEMORY_ALLOCATION_FAILURE
:
134 return "memory allocation";
146 void create_core(void)
148 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
158 while (waitpid(pid
, NULL
, 0) != pid
) {};
163 static Framework
*world
= NULL
;
164 int main(int argc
, char *argv
[])
166 srandom((unsigned int)time(NULL
));
168 world
= new Framework();
175 libtest::SignalThread signal
;
176 if (not signal
.setup())
186 void *creators_ptr
= world
->create(error
);
194 Out
<< "SKIP " << argv
[0];
200 case TEST_MEMORY_ALLOCATION_FAILURE
:
201 Error
<< argv
[0] << "create() failed";
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 Out
<< "Only testing " << collection_to_run
;
221 char *wildcard
= NULL
;
227 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); 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
)
252 Out
<< next
->name
<< " [ failed ]";
254 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
258 Out
<< next
->name
<< " [ skipping ]";
262 case TEST_MEMORY_ALLOCATION_FAILURE
:
263 test_assert(0, "Allocation failure, or unknown return");
266 Out
<< "Collection: " << next
->name
;
268 for (test_st
*run
= next
->tests
; run
->name
; run
++)
270 struct timeval start_time
, end_time
;
271 long int load_time
= 0;
273 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
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 assert(world
->runner());
289 assert(run
->test_fn
);
290 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
291 gettimeofday(&end_time
, NULL
);
292 load_time
= timedif(end_time
, start_time
);
296 // @todo do something if post fails
297 (void)world
->item
.post(creators_ptr
);
299 else if (return_code
== TEST_SKIPPED
)
301 else if (return_code
== TEST_FAILURE
)
303 Error
<< " item.flush(failure)";
304 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
307 else if (return_code
== TEST_SKIPPED
)
309 else if (return_code
== TEST_FAILURE
)
311 Error
<< " item.startup(failure)";
312 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
320 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
328 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
334 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
337 case TEST_MEMORY_ALLOCATION_FAILURE
:
338 test_assert(0, "Memory Allocation Error");
341 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
343 Error
<< "Failed while running on_error()";
344 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
349 (void) world
->runner()->post(next
->post
, creators_ptr
);
352 if (failed
== false and skipped
== false)
354 stats
.collection_success
++;
359 stats
.collection_failed
++;
364 stats
.collection_skipped
++;
367 world
->shutdown(creators_ptr
);
371 if (not signal
.is_shutdown())
373 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
376 int exit_code
= EXIT_SUCCESS
;
377 shutdown_t status
= signal
.get_shutdown();
378 if (status
== SHUTDOWN_FORCED
)
380 Out
<< "Tests were aborted.";
381 exit_code
= EXIT_FAILURE
;
383 else if (stats
.collection_failed
)
385 Out
<< "Some test failed.";
386 exit_code
= EXIT_FAILURE
;
388 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
390 Out
<< "Some tests were skipped.";
392 else if (stats
.collection_success
and stats
.collection_failed
== 0)
394 Out
<< "All tests completed successfully.";
401 Outn(); // Generate a blank to break up the messages if make check/test has been run