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("srcdir"))
155 snprintf(buffer
, sizeof(buffer
), "%s/%s", getenv("srcdir"), "tests");
163 world
= new Framework();
170 libtest::SignalThread signal
;
171 if (not signal
.setup())
181 void *creators_ptr
= world
->create(error
);
189 Out
<< "SKIP " << argv
[0];
195 case TEST_MEMORY_ALLOCATION_FAILURE
:
196 Error
<< argv
[0] << "create() failed";
201 char *collection_to_run
= NULL
;
204 collection_to_run
= argv
[1];
206 else if (getenv("TEST_COLLECTION"))
208 collection_to_run
= getenv("TEST_COLLECTION");
211 if (collection_to_run
)
213 Out
<< "Only testing " << collection_to_run
;
216 char *wildcard
= NULL
;
222 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); next
++)
224 test_return_t collection_rc
= TEST_SUCCESS
;
228 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
231 stats
.collection_total
++;
233 collection_rc
= world
->startup(creators_ptr
);
235 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
237 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
240 switch (collection_rc
)
247 Out
<< next
->name
<< " [ failed ]";
249 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
253 Out
<< next
->name
<< " [ skipping ]";
257 case TEST_MEMORY_ALLOCATION_FAILURE
:
258 test_assert(0, "Allocation failure, or unknown return");
261 Out
<< "Collection: " << next
->name
;
263 for (test_st
*run
= next
->tests
; run
->name
; run
++)
265 struct timeval start_time
, end_time
;
266 long int load_time
= 0;
268 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
273 test_return_t return_code
;
274 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
276 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
278 // @note pre will fail is SKIPPED is returned
279 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
282 gettimeofday(&start_time
, NULL
);
283 assert(world
->runner());
284 assert(run
->test_fn
);
285 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
286 gettimeofday(&end_time
, NULL
);
287 load_time
= timedif(end_time
, start_time
);
291 // @todo do something if post fails
292 (void)world
->item
.post(creators_ptr
);
294 else if (return_code
== TEST_SKIPPED
)
296 else if (return_code
== TEST_FAILURE
)
298 Error
<< " item.flush(failure)";
299 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
302 else if (return_code
== TEST_SKIPPED
)
304 else if (return_code
== TEST_FAILURE
)
306 Error
<< " item.startup(failure)";
307 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
315 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
323 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
329 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
332 case TEST_MEMORY_ALLOCATION_FAILURE
:
333 test_assert(0, "Memory Allocation Error");
336 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
338 Error
<< "Failed while running on_error()";
339 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
344 (void) world
->runner()->post(next
->post
, creators_ptr
);
347 if (failed
== false and skipped
== false)
349 stats
.collection_success
++;
354 stats
.collection_failed
++;
359 stats
.collection_skipped
++;
362 world
->shutdown(creators_ptr
);
366 if (not signal
.is_shutdown())
368 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
371 int exit_code
= EXIT_SUCCESS
;
372 shutdown_t status
= signal
.get_shutdown();
373 if (status
== SHUTDOWN_FORCED
)
375 Out
<< "Tests were aborted.";
376 exit_code
= EXIT_FAILURE
;
378 else if (stats
.collection_failed
)
380 Out
<< "Some test failed.";
381 exit_code
= EXIT_FAILURE
;
383 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
385 Out
<< "Some tests were skipped.";
387 else if (stats
.collection_success
and stats
.collection_failed
== 0)
389 Out
<< "All tests completed successfully.";
396 Outn(); // Generate a blank to break up the messages if make check/test has been run