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 world
= new Framework();
159 libtest::SignalThread signal
;
160 if (not signal
.setup())
170 void *creators_ptr
= world
->create(error
);
178 Out
<< "SKIP " << argv
[0];
184 case TEST_MEMORY_ALLOCATION_FAILURE
:
185 Error
<< argv
[0] << "create() failed";
190 char *collection_to_run
= NULL
;
193 collection_to_run
= argv
[1];
195 else if (getenv("TEST_COLLECTION"))
197 collection_to_run
= getenv("TEST_COLLECTION");
200 if (collection_to_run
)
202 Out
<< "Only testing " << collection_to_run
;
205 char *wildcard
= NULL
;
211 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); next
++)
213 test_return_t collection_rc
= TEST_SUCCESS
;
217 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
220 stats
.collection_total
++;
222 collection_rc
= world
->startup(creators_ptr
);
224 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
226 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
229 switch (collection_rc
)
236 Out
<< next
->name
<< " [ failed ]";
238 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
242 Out
<< next
->name
<< " [ skipping ]";
246 case TEST_MEMORY_ALLOCATION_FAILURE
:
247 test_assert(0, "Allocation failure, or unknown return");
250 Out
<< "Collection: " << next
->name
;
252 for (test_st
*run
= next
->tests
; run
->name
; run
++)
254 struct timeval start_time
, end_time
;
255 long int load_time
= 0;
257 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
262 test_return_t return_code
;
263 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
265 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
267 // @note pre will fail is SKIPPED is returned
268 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
271 gettimeofday(&start_time
, NULL
);
272 assert(world
->runner());
273 assert(run
->test_fn
);
274 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
275 gettimeofday(&end_time
, NULL
);
276 load_time
= timedif(end_time
, start_time
);
280 // @todo do something if post fails
281 (void)world
->item
.post(creators_ptr
);
283 else if (return_code
== TEST_SKIPPED
)
285 else if (return_code
== TEST_FAILURE
)
287 Error
<< " item.flush(failure)";
288 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
291 else if (return_code
== TEST_SKIPPED
)
293 else if (return_code
== TEST_FAILURE
)
295 Error
<< " item.startup(failure)";
296 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
304 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
312 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
318 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
321 case TEST_MEMORY_ALLOCATION_FAILURE
:
322 test_assert(0, "Memory Allocation Error");
325 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
327 Error
<< "Failed while running on_error()";
328 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
333 (void) world
->runner()->post(next
->post
, creators_ptr
);
336 if (failed
== false and skipped
== false)
338 stats
.collection_success
++;
343 stats
.collection_failed
++;
348 stats
.collection_skipped
++;
351 world
->shutdown(creators_ptr
);
355 if (not signal
.is_shutdown())
357 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
360 int exit_code
= EXIT_SUCCESS
;
361 shutdown_t status
= signal
.get_shutdown();
362 if (status
== SHUTDOWN_FORCED
)
364 Out
<< "Tests were aborted.";
365 exit_code
= EXIT_FAILURE
;
367 else if (stats
.collection_failed
)
369 Out
<< "Some test failed.";
370 exit_code
= EXIT_FAILURE
;
372 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
374 Out
<< "Some tests were skipped.";
376 else if (stats
.collection_success
and stats
.collection_failed
== 0)
378 Out
<< "All tests completed successfully.";
385 Outn(); // Generate a blank to break up the messages if make check/test has been run