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 in_port_t global_port
= 0;
48 static char global_socket
[1024];
50 in_port_t
default_port()
55 void set_default_port(in_port_t port
)
60 const char *default_socket()
62 assert(global_socket
[0]);
68 return (getenv("LIBTEST_LOCAL"));
71 void set_default_socket(const char *socket
)
75 strncpy(global_socket
, socket
, strlen(socket
));
79 static void stats_print(Stats
*stats
)
81 if (stats
->collection_failed
== 0 and stats
->collection_success
== 0)
86 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
87 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
88 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
89 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
91 Out
<< "Total\t\t\t\t" << stats
->total
;
92 Out
<< "\tFailed\t\t\t" << stats
->failed
;
93 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
94 Out
<< "\tSucceeded\t\t" << stats
->success
;
97 static long int timedif(struct timeval a
, struct timeval b
)
101 us
= (long)(a
.tv_usec
- b
.tv_usec
);
103 s
= (long)(a
.tv_sec
- b
.tv_sec
);
108 const char *test_strerror(test_return_t code
)
117 case TEST_MEMORY_ALLOCATION_FAILURE
:
118 return "memory allocation";
130 void create_core(void)
132 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
142 while (waitpid(pid
, NULL
, 0) != pid
) {};
147 static Framework
*world
= NULL
;
148 int main(int argc
, char *argv
[])
150 srandom((unsigned int)time(NULL
));
152 if (getenv("LIBTEST_QUIET"))
154 close(STDOUT_FILENO
);
158 if (getenv("LIBTEST_TMP"))
160 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
164 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
167 if (chdir(buffer
) == -1)
169 char getcwd_buffer
[1024];
170 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
172 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
176 if (libtest::libtool() == NULL
)
178 Error
<< "Failed to locate libtool";
182 world
= new Framework();
186 Error
<< "Failed to create Framework()";
190 libtest::SignalThread signal
;
191 if (not signal
.setup())
201 void *creators_ptr
= world
->create(error
);
209 Out
<< "SKIP " << argv
[0];
215 case TEST_MEMORY_ALLOCATION_FAILURE
:
216 Error
<< argv
[0] << " failed in Framework::create()";
221 char *collection_to_run
= NULL
;
224 collection_to_run
= argv
[1];
226 else if (getenv("TEST_COLLECTION"))
228 collection_to_run
= getenv("TEST_COLLECTION");
231 if (collection_to_run
)
233 Out
<< "Only testing " << collection_to_run
;
236 char *wildcard
= NULL
;
242 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); next
++)
244 test_return_t collection_rc
= TEST_SUCCESS
;
248 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
251 stats
.collection_total
++;
253 collection_rc
= world
->startup(creators_ptr
);
255 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
257 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
260 switch (collection_rc
)
267 Out
<< next
->name
<< " [ failed ]";
269 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
273 Out
<< next
->name
<< " [ skipping ]";
277 case TEST_MEMORY_ALLOCATION_FAILURE
:
278 test_assert(0, "Allocation failure, or unknown return");
281 Out
<< "Collection: " << next
->name
;
283 for (test_st
*run
= next
->tests
; run
->name
; run
++)
285 struct timeval start_time
, end_time
;
286 long int load_time
= 0;
288 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
293 test_return_t return_code
;
294 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
296 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
298 // @note pre will fail is SKIPPED is returned
299 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
302 gettimeofday(&start_time
, NULL
);
303 assert(world
->runner());
304 assert(run
->test_fn
);
305 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
306 gettimeofday(&end_time
, NULL
);
307 load_time
= timedif(end_time
, start_time
);
311 // @todo do something if post fails
312 (void)world
->item
.post(creators_ptr
);
314 else if (return_code
== TEST_SKIPPED
)
316 else if (return_code
== TEST_FAILURE
)
318 Error
<< " item.flush(failure)";
319 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
322 else if (return_code
== TEST_SKIPPED
)
324 else if (return_code
== TEST_FAILURE
)
326 Error
<< " item.startup(failure)";
327 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
335 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
343 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
349 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
352 case TEST_MEMORY_ALLOCATION_FAILURE
:
353 test_assert(0, "Memory Allocation Error");
356 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
358 Error
<< "Failed while running on_error()";
359 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
364 (void) world
->runner()->post(next
->post
, creators_ptr
);
367 if (failed
== false and skipped
== false)
369 stats
.collection_success
++;
374 stats
.collection_failed
++;
379 stats
.collection_skipped
++;
382 world
->shutdown(creators_ptr
);
386 if (not signal
.is_shutdown())
388 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
391 int exit_code
= EXIT_SUCCESS
;
392 shutdown_t status
= signal
.get_shutdown();
393 if (status
== SHUTDOWN_FORCED
)
395 Out
<< "Tests were aborted.";
396 exit_code
= EXIT_FAILURE
;
398 else if (stats
.collection_failed
)
400 Out
<< "Some test failed.";
401 exit_code
= EXIT_FAILURE
;
403 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
405 Out
<< "Some tests were skipped.";
407 else if (stats
.collection_success
and stats
.collection_failed
== 0)
409 Out
<< "All tests completed successfully.";
416 Outn(); // Generate a blank to break up the messages if make check/test has been run