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 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
98 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
99 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
100 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
102 Out
<< "Total\t\t\t\t" << stats
->total
;
103 Out
<< "\tFailed\t\t\t" << stats
->failed
;
104 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
105 Out
<< "\tSucceeded\t\t" << stats
->success
;
108 static long int timedif(struct timeval a
, struct timeval b
)
112 us
= (long)(a
.tv_usec
- b
.tv_usec
);
114 s
= (long)(a
.tv_sec
- b
.tv_sec
);
119 const char *test_strerror(test_return_t code
)
128 case TEST_MEMORY_ALLOCATION_FAILURE
:
129 return "memory allocation";
141 void create_core(void)
143 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
153 while (waitpid(pid
, NULL
, 0) != pid
) {};
158 static Framework
*world
= NULL
;
159 int main(int argc
, char *argv
[])
161 srandom((unsigned int)time(NULL
));
163 world
= new Framework();
177 void *creators_ptr
= world
->create(error
);
178 if (test_failed(error
))
180 Error
<< "create() failed";
185 char *collection_to_run
= NULL
;
188 collection_to_run
= argv
[1];
190 else if (getenv("TEST_COLLECTION"))
192 collection_to_run
= getenv("TEST_COLLECTION");
195 if (collection_to_run
)
197 Out
<< "Only testing " << collection_to_run
;
200 char *wildcard
= NULL
;
206 for (collection_st
*next
= world
->collections
; next
->name
and (not is_shutdown()); next
++)
208 test_return_t collection_rc
= TEST_SUCCESS
;
212 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
215 stats
.collection_total
++;
217 collection_rc
= world
->startup(creators_ptr
);
219 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
221 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
224 switch (collection_rc
)
231 Out
<< next
->name
<< " [ failed ]";
233 set_shutdown(SHUTDOWN_GRACEFUL
);
237 Out
<< next
->name
<< " [ skipping ]";
241 case TEST_MEMORY_ALLOCATION_FAILURE
:
242 test_assert(0, "Allocation failure, or unknown return");
245 Out
<< "Collection: " << next
->name
;
247 for (test_st
*run
= next
->tests
; run
->name
; run
++)
249 struct timeval start_time
, end_time
;
250 long int load_time
= 0;
252 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
257 test_return_t return_code
;
258 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
260 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
262 // @note pre will fail is SKIPPED is returned
263 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
266 gettimeofday(&start_time
, NULL
);
267 assert(world
->runner());
268 assert(run
->test_fn
);
269 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
270 gettimeofday(&end_time
, NULL
);
271 load_time
= timedif(end_time
, start_time
);
275 // @todo do something if post fails
276 (void)world
->item
.post(creators_ptr
);
278 else if (return_code
== TEST_SKIPPED
)
280 else if (return_code
== TEST_FAILURE
)
282 Error
<< " item.flush(failure)";
283 set_shutdown(SHUTDOWN_GRACEFUL
);
286 else if (return_code
== TEST_SKIPPED
)
288 else if (return_code
== TEST_FAILURE
)
290 Error
<< " item.startup(failure)";
291 set_shutdown(SHUTDOWN_GRACEFUL
);
299 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
307 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
313 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
316 case TEST_MEMORY_ALLOCATION_FAILURE
:
317 test_assert(0, "Memory Allocation Error");
320 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
322 Error
<< "Failed while running on_error()";
323 set_shutdown(SHUTDOWN_GRACEFUL
);
328 (void) world
->runner()->post(next
->post
, creators_ptr
);
331 if (failed
== false and skipped
== false)
333 stats
.collection_success
++;
338 stats
.collection_failed
++;
343 stats
.collection_skipped
++;
346 world
->shutdown(creators_ptr
);
350 if (not is_shutdown())
352 set_shutdown(SHUTDOWN_GRACEFUL
);
355 int exit_code
= EXIT_SUCCESS
;
356 shutdown_t status
= get_shutdown();
357 if (status
== SHUTDOWN_FORCED
)
359 Out
<< "Tests were aborted.";
360 exit_code
= EXIT_FAILURE
;
362 else if (stats
.collection_failed
)
364 Out
<< "Some test failed.";
365 exit_code
= EXIT_FAILURE
;
367 else if (stats
.collection_skipped
)
369 Out
<< "Some tests were skipped.";
373 Out
<< "All tests completed successfully.";
380 Outn(); // Generate a blank to break up the messages if make check/test has been run