From bed5d0baa746d10fd3899be96a6ab8673daa7e03 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Wed, 6 Jul 2011 18:39:44 -0700 Subject: [PATCH] Adding custom IOSTREAM for libtest. --- libtest/common.h | 5 ++ libtest/failed.cc | 5 +- libtest/framework.cc | 41 +++++++++-- libtest/framework.h | 37 +++++++++- libtest/killpid.cc | 14 ++-- libtest/memcached.cc | 30 ++++---- libtest/stream.h | 158 +++++++++++++++++++++++++++++++++++++++++++ libtest/test.cc | 134 ++++++++++++++++++++++++------------ libtest/test.hpp | 42 ++++++++++-- tests/cycle.cc | 19 ++++++ 10 files changed, 405 insertions(+), 80 deletions(-) create mode 100644 libtest/stream.h diff --git a/libtest/common.h b/libtest/common.h index 9b00f6de..3874dae5 100644 --- a/libtest/common.h +++ b/libtest/common.h @@ -47,4 +47,9 @@ #include #include +#include +#include + #include + +using namespace libtest; diff --git a/libtest/failed.cc b/libtest/failed.cc index df022467..5ed8020a 100644 --- a/libtest/failed.cc +++ b/libtest/failed.cc @@ -35,7 +35,7 @@ * */ -#include +#include #include @@ -68,8 +68,7 @@ void print_failed_test(void) { for (Failures::iterator iter= failures.begin(); iter != failures.end(); iter++) { - std::cerr << "\t" << (*iter).collection << " " << (*iter).test << std::endl; + Error << "\t" << (*iter).collection << " " << (*iter).test; } - std::cerr << std::endl; } diff --git a/libtest/framework.cc b/libtest/framework.cc index fab8a80a..dc9bddd9 100644 --- a/libtest/framework.cc +++ b/libtest/framework.cc @@ -1,12 +1,45 @@ -/* uTest Copyright (C) 2011 Data Differential, http://datadifferential.com/ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * uTest, libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. */ + #include #include +using namespace libtest; + static test_return_t _runner_default(test_callback_fn func, void *p) { if (func) @@ -48,7 +81,7 @@ Framework::~Framework() { if (test_failed(_destroy(_creators_ptr))) { - std::cerr << "Failure in _destroy(), some resources may not have been cleaned up." << std::endl; + Error << "Failure in _destroy(), some resources may not have been cleaned up."; } } } diff --git a/libtest/framework.h b/libtest/framework.h index 6cc2a5d1..1c9a1c1d 100644 --- a/libtest/framework.h +++ b/libtest/framework.h @@ -1,9 +1,40 @@ -/* uTest Copyright (C) 2011 Data Differential, http://datadifferential.com/ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * uTest, libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. */ + #pragma once /** diff --git a/libtest/killpid.cc b/libtest/killpid.cc index ca8a659b..bacec8f1 100644 --- a/libtest/killpid.cc +++ b/libtest/killpid.cc @@ -34,14 +34,16 @@ * */ -#include +#include -#include #include #include #include #include +#include + +using namespace libtest; bool kill_pid(pid_t pid_arg) { @@ -54,12 +56,12 @@ bool kill_pid(pid_t pid_arg) { case EPERM: perror(__func__); - std::cerr << __func__ << " -> Does someone else have a process running locally for " << int(pid_arg) << "?" << std::endl; + Error << __func__ << " -> Does someone else have a process running locally for " << int(pid_arg) << "?"; return false; case ESRCH: perror(__func__); - std::cerr << "Process " << int(pid_arg) << " not found." << std::endl; + Error << "Process " << int(pid_arg) << " not found."; return false; default: @@ -78,7 +80,9 @@ bool kill_pid(pid_t pid_arg) case ECHILD: return true; } - std::cerr << std::endl << "Error occured while waitpid(" << strerror(errno) << ") on pid " << int(pid_arg) << std::endl; + + Error << "Error occured while waitpid(" << strerror(errno) << ") on pid " << int(pid_arg); + return false; } diff --git a/libtest/memcached.cc b/libtest/memcached.cc index 04e69860..c3f40fa8 100644 --- a/libtest/memcached.cc +++ b/libtest/memcached.cc @@ -44,8 +44,6 @@ #include -#include -#include #include #include #include @@ -63,8 +61,6 @@ #include #include -#define CERR_PREFIX std::endl << __FILE__ << ":" << __LINE__ << " " - #define SOCKET_FILE "/tmp/memcached.socket" static pid_t __getpid(server_st& server) @@ -92,12 +88,12 @@ static bool cycle_server(server_st *server) if (pid > 0 and kill_pid(pid)) { - std::cerr << CERR_PREFIX << "Killed existing server," << *server << " with pid:" << pid << std::endl; + Error << "Killed existing server," << *server << " with pid:" << pid; continue; } else if (libmemcached_util_flush(server->hostname(), server->port(), NULL)) // If we can flush it, we will just use it { - std::cerr << CERR_PREFIX << "Found server on port " << int(server->port()) << ", flushed it!" << std::endl; + Error << "Found server on port " << int(server->port()) << ", flushed it!"; server->set_used(); return true; } // No idea what is wrong here, so we need to find a different port @@ -115,10 +111,11 @@ static bool cycle_server(server_st *server) bool server_startup(server_startup_st *construct) { + Log; if (getenv(((char *)"MEMCACHED_SERVERS"))) { construct->server_list= getenv(((char *)"MEMCACHED_SERVERS")); - printf("servers %s\n", construct->server_list.c_str()); + Log << "MEMCACHED_SERVERS " << construct->server_list; construct->count= 0; } else @@ -146,7 +143,7 @@ bool server_startup(server_startup_st *construct) while (not cycle_server(server)) { - std::cerr << CERR_PREFIX << "Found server " << *server << ", could not flush it, so trying next port." << std::endl; + Error << "Found server " << *server << ", could not flush it, so trying next port."; port_base++; server->set_port(in_port_t(x +TEST_PORT_BASE +port_base)); } @@ -155,7 +152,7 @@ bool server_startup(server_startup_st *construct) if (server->is_used()) { - std::cerr << std::endl << "Using server at : " << server << std::endl; + Log << "Using server at : " << server; } else { @@ -174,11 +171,11 @@ bool server_startup(server_startup_st *construct) if (not server->start()) { - std::cerr << CERR_PREFIX << "Failed system(" << buffer << ")" << std::endl; + Error << "Failed system(" << buffer << ")"; delete server; return false; } - std::cerr << "STARTING SERVER: " << buffer << " pid:" << server->pid() << std::endl; + Log << "STARTING SERVER: " << buffer << " pid:" << server->pid(); } construct->push_server(server); @@ -213,13 +210,13 @@ bool server_startup(server_startup_st *construct) if (not cycle_server(server)) { - std::cerr << CERR_PREFIX << "Found server " << server << ", could not flush it, failing since socket file is not available." << std::endl; + Error << "Found server " << server << ", could not flush it, failing since socket file is not available."; return false; } if (server->is_used()) { - std::cerr << std::endl << "Using server at : " << *server << std::endl; + Log << "Using server at : " << *server; } else { @@ -229,11 +226,11 @@ bool server_startup(server_startup_st *construct) if (not server->start()) { - std::cerr << CERR_PREFIX << "Failed system(" << buffer << ")" << std::endl; + Error << "Failed system(" << buffer << ")"; delete server; return false; } - std::cerr << "STARTING SERVER: " << buffer << " pid:" << server->pid() << std::endl; + Log << "STARTING SERVER: " << buffer << " pid:" << server->pid(); } set_default_socket(server->hostname()); construct->push_server(server); @@ -249,9 +246,10 @@ bool server_startup(server_startup_st *construct) construct->server_list= server_config_string; } + Log; + srandom((unsigned int)time(NULL)); - std::cerr << std::endl; return true; } diff --git a/libtest/stream.h b/libtest/stream.h new file mode 100644 index 00000000..3ad71b1f --- /dev/null +++ b/libtest/stream.h @@ -0,0 +1,158 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * uTest, libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace libtest { +namespace stream { + +namespace detail { + +template + class cerr { + private: + + public: + typedef std::basic_ostringstream stream_buffer; + + public: + void operator()(const stream_buffer &s) + { + std::cerr << std::endl << s.str() << std::endl; + } + }; + +template + class cout { + private: + + public: + typedef std::basic_ostringstream stream_buffer; + + public: + void operator()(const stream_buffer &s) + { + std::cout << s.str() << std::endl; + } + }; + +template + class clog { + private: + + public: + typedef std::basic_ostringstream stream_buffer; + + public: + void operator()(const stream_buffer &s) + { + std::cerr << s.str() << std::endl; + } + }; + +template