1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <libtest/common.h>
28 #include <sys/types.h>
38 #include <libtest/stats.h>
39 #include <libtest/signal.h>
41 #ifndef __INTEL_COMPILER
42 #pragma GCC diagnostic ignored "-Wold-style-cast"
45 using namespace libtest
;
47 static char global_socket
[1024];
49 const char *default_socket()
51 assert(global_socket
[0]);
57 return (getenv("LIBTEST_LOCAL"));
60 void set_default_socket(const char *socket
)
64 strncpy(global_socket
, socket
, strlen(socket
));
68 static void stats_print(Stats
*stats
)
70 if (stats
->collection_failed
== 0 and stats
->collection_success
== 0)
75 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
76 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
77 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
78 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
80 Out
<< "Total\t\t\t\t" << stats
->total
;
81 Out
<< "\tFailed\t\t\t" << stats
->failed
;
82 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
83 Out
<< "\tSucceeded\t\t" << stats
->success
;
86 static long int timedif(struct timeval a
, struct timeval b
)
90 us
= (long)(a
.tv_usec
- b
.tv_usec
);
92 s
= (long)(a
.tv_sec
- b
.tv_sec
);
97 const char *test_strerror(test_return_t code
)
106 case TEST_MEMORY_ALLOCATION_FAILURE
:
107 return "memory allocation";
119 void create_core(void)
121 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
131 while (waitpid(pid
, NULL
, 0) != pid
) {};
136 static Framework
*world
= NULL
;
137 int main(int argc
, char *argv
[])
139 srandom((unsigned int)time(NULL
));
141 if (getenv("LIBTEST_QUIET"))
143 close(STDOUT_FILENO
);
147 if (getenv("LIBTEST_TMP"))
149 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
153 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
156 if (chdir(buffer
) == -1)
158 char getcwd_buffer
[1024];
159 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
161 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
165 if (libtest::libtool() == NULL
)
167 Error
<< "Failed to locate libtool";
171 world
= new Framework();
175 Error
<< "Failed to create Framework()";
179 libtest::SignalThread signal
;
180 if (not signal
.setup())
190 void *creators_ptr
= world
->create(error
);
198 Out
<< "SKIP " << argv
[0];
204 case TEST_MEMORY_ALLOCATION_FAILURE
:
205 Error
<< argv
[0] << " failed in Framework::create()";
210 char *collection_to_run
= NULL
;
213 collection_to_run
= argv
[1];
215 else if (getenv("TEST_COLLECTION"))
217 collection_to_run
= getenv("TEST_COLLECTION");
220 if (collection_to_run
)
222 Out
<< "Only testing " << collection_to_run
;
225 char *wildcard
= NULL
;
231 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); next
++)
233 test_return_t collection_rc
= TEST_SUCCESS
;
237 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
240 stats
.collection_total
++;
242 collection_rc
= world
->startup(creators_ptr
);
244 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
246 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
249 switch (collection_rc
)
256 Out
<< next
->name
<< " [ failed ]";
258 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
262 Out
<< next
->name
<< " [ skipping ]";
266 case TEST_MEMORY_ALLOCATION_FAILURE
:
267 test_assert(0, "Allocation failure, or unknown return");
270 Out
<< "Collection: " << next
->name
;
272 for (test_st
*run
= next
->tests
; run
->name
; run
++)
274 struct timeval start_time
, end_time
;
275 long int load_time
= 0;
277 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
282 test_return_t return_code
;
283 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
285 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
287 // @note pre will fail is SKIPPED is returned
288 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
291 gettimeofday(&start_time
, NULL
);
292 assert(world
->runner());
293 assert(run
->test_fn
);
294 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
295 gettimeofday(&end_time
, NULL
);
296 load_time
= timedif(end_time
, start_time
);
300 // @todo do something if post fails
301 (void)world
->item
.post(creators_ptr
);
303 else if (return_code
== TEST_SKIPPED
)
305 else if (return_code
== TEST_FAILURE
)
307 Error
<< " item.flush(failure)";
308 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
311 else if (return_code
== TEST_SKIPPED
)
313 else if (return_code
== TEST_FAILURE
)
315 Error
<< " item.startup(failure)";
316 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
324 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
332 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
338 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
341 case TEST_MEMORY_ALLOCATION_FAILURE
:
342 test_assert(0, "Memory Allocation Error");
345 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
347 Error
<< "Failed while running on_error()";
348 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
353 (void) world
->runner()->post(next
->post
, creators_ptr
);
356 if (failed
== false and skipped
== false)
358 stats
.collection_success
++;
363 stats
.collection_failed
++;
368 stats
.collection_skipped
++;
371 world
->shutdown(creators_ptr
);
375 if (not signal
.is_shutdown())
377 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
380 int exit_code
= EXIT_SUCCESS
;
381 shutdown_t status
= signal
.get_shutdown();
382 if (status
== SHUTDOWN_FORCED
)
384 Out
<< "Tests were aborted.";
385 exit_code
= EXIT_FAILURE
;
387 else if (stats
.collection_failed
)
389 Out
<< "Some test failed.";
390 exit_code
= EXIT_FAILURE
;
392 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
394 Out
<< "Some tests were skipped.";
396 else if (stats
.collection_success
and stats
.collection_failed
== 0)
398 Out
<< "All tests completed successfully.";
405 Outn(); // Generate a blank to break up the messages if make check/test has been run