diff options
author | Alan Conway <aconway@apache.org> | 2008-09-25 14:16:51 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2008-09-25 14:16:51 +0000 |
commit | 928b806b8bc238de9222bd81e7fba950494588b7 (patch) | |
tree | 699a4f1c38c0f2df8a3e41cb839b84f4b7ef5bcc | |
parent | 8fca6bbadd52582a9d0e860a29adb0365c1fb71c (diff) | |
download | qpid-python-928b806b8bc238de9222bd81e7fba950494588b7.tar.gz |
Added ScopedSuppressLogging, used to suppress expected error messages in tests.
For examples see src/tests/exception_test.cpp
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@698981 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | cpp/src/qpid/log/Logger.cpp | 2 | ||||
-rw-r--r-- | cpp/src/qpid/log/Logger.h | 17 | ||||
-rw-r--r-- | cpp/src/qpid/log/Options.cpp | 19 | ||||
-rw-r--r-- | cpp/src/qpid/log/Options.h | 4 | ||||
-rw-r--r-- | cpp/src/tests/QueuePolicyTest.cpp | 7 | ||||
-rw-r--r-- | cpp/src/tests/exception_test.cpp | 10 | ||||
-rw-r--r-- | cpp/src/tests/logging.cpp | 53 | ||||
-rw-r--r-- | cpp/src/tests/test_tools.h | 14 |
8 files changed, 69 insertions, 57 deletions
diff --git a/cpp/src/qpid/log/Logger.cpp b/cpp/src/qpid/log/Logger.cpp index 54df54bb94..b4f5bde186 100644 --- a/cpp/src/qpid/log/Logger.cpp +++ b/cpp/src/qpid/log/Logger.cpp @@ -17,7 +17,6 @@ */ #include "Logger.h" -#include "Options.h" #include "qpid/memory.h" #include "qpid/sys/Thread.h" #include <boost/pool/detail/singleton.hpp> @@ -212,6 +211,7 @@ void Logger::add(Statement& s) { } void Logger::configure(const Options& opts) { + options = opts; clear(); Options o(opts); if (o.trace) diff --git a/cpp/src/qpid/log/Logger.h b/cpp/src/qpid/log/Logger.h index fa38678bba..3201eac98f 100644 --- a/cpp/src/qpid/log/Logger.h +++ b/cpp/src/qpid/log/Logger.h @@ -1,5 +1,5 @@ -#ifndef LOGGER_H -#define LOGGER_H +#ifndef QPID_LOG_LOGGER_H +#define QPID_LOG_LOGGER_H /* * http://www.apache.org/licenses/LICENSE-2.0 @@ -13,6 +13,7 @@ */ #include "Selector.h" +#include "Options.h" #include "qpid/sys/Mutex.h" #include <boost/ptr_container/ptr_vector.hpp> #include <boost/noncopyable.hpp> @@ -21,8 +22,6 @@ namespace qpid { namespace log { -class Options; - /** * Central logging agent. * @@ -93,10 +92,13 @@ class Logger : private boost::noncopyable { /** Set a prefix for all messages */ void setPrefix(const std::string& prefix); - /** Reset the logger to it's original state. */ + /** Reset the logger. */ void clear(); - + /** Get the options used to configure the logger. */ + const Options& getOptions() const { return options; } + + private: typedef boost::ptr_vector<Output> Outputs; typedef std::set<Statement*> Statements; @@ -109,9 +111,10 @@ class Logger : private boost::noncopyable { Selector selector; int flags; std::string prefix; + Options options; }; }} // namespace qpid::log -#endif /*!LOGGER_H*/ +#endif /*!QPID_LOG_LOGGER_H*/ diff --git a/cpp/src/qpid/log/Options.cpp b/cpp/src/qpid/log/Options.cpp index e28f82648e..a850ea470d 100644 --- a/cpp/src/qpid/log/Options.cpp +++ b/cpp/src/qpid/log/Options.cpp @@ -146,6 +146,23 @@ Options::Options(const std::string& argv0, const std::string& name) : ("syslog-name", optValue(syslogName, "NAME"), "Name to use in syslog messages") ("syslog-facility", optValue(syslogFacility,"LOG_XXX"), "Facility to use in syslog messages") ; -} +} + +Options& Options::operator=(const Options& x) { + if (this != &x) { + selectors = x.selectors; + outputs = x.outputs; + time = x.time; + level= x.level; + thread = x.thread; + source = x.source; + function = x.function; + trace = x.trace; + syslogName = x.syslogName; + syslogFacility = x.syslogFacility; + prefix = x.prefix; + } + return *this; +} }} // namespace qpid::log diff --git a/cpp/src/qpid/log/Options.h b/cpp/src/qpid/log/Options.h index 13fca38f9d..3d128b5668 100644 --- a/cpp/src/qpid/log/Options.h +++ b/cpp/src/qpid/log/Options.h @@ -36,9 +36,11 @@ std::istream& operator>>(std::istream&, SyslogFacility&); /** Logging options for config parser. */ struct Options : public qpid::Options { /** Pass argv[0] for use in syslog output */ - Options(const std::string& argv0, + Options(const std::string& argv0=std::string(), const std::string& name="Logging options"); + Options& operator=(const Options&); + std::vector<std::string> selectors; std::vector<std::string> outputs; bool time, level, thread, source, function; diff --git a/cpp/src/tests/QueuePolicyTest.cpp b/cpp/src/tests/QueuePolicyTest.cpp index d7f3fa5628..cf45f554df 100644 --- a/cpp/src/tests/QueuePolicyTest.cpp +++ b/cpp/src/tests/QueuePolicyTest.cpp @@ -18,9 +18,11 @@ * under the License. * */ +#include "unit_test.h" +#include "test_tools.h" + #include "qpid/broker/QueuePolicy.h" #include "qpid/sys/Time.h" -#include "unit_test.h" #include "MessageUtils.h" #include "BrokerFixture.h" @@ -174,8 +176,9 @@ QPID_AUTO_TEST_CASE(testStrictRingPolicy) BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%") % "Message" % (i+1)).str()); } try { + ScopedSuppressLogging sl; // Suppress messages for expected errors. f.session.messageTransfer(arg::content=client::Message("Message_6", q)); - BOOST_FAIL("Transfer should have failed as "); + BOOST_FAIL("expecting ResourceLimitExceededException."); } catch (const ResourceLimitExceededException&) {} } diff --git a/cpp/src/tests/exception_test.cpp b/cpp/src/tests/exception_test.cpp index 339881fa9d..b42c1e58e0 100644 --- a/cpp/src/tests/exception_test.cpp +++ b/cpp/src/tests/exception_test.cpp @@ -20,6 +20,7 @@ */ #include "unit_test.h" +#include "test_tools.h" #include "BrokerFixture.h" #include "qpid/client/SubscriptionManager.h" #include "qpid/sys/Runnable.h" @@ -51,7 +52,10 @@ struct Catcher : public Runnable { ~Catcher() { join(); } void run() { - try { f(); } + try { + ScopedSuppressLogging sl; // Suppress messages for expected errors. + f(); + } catch(const Ex& e) { caught=true; BOOST_MESSAGE(string("Caught expected exception: ")+e.what()+"["+typeid(e).name()+"]"); @@ -76,6 +80,7 @@ struct Catcher : public Runnable { QPID_AUTO_TEST_CASE(TestSessionBusy) { SessionFixture f; try { + ScopedSuppressLogging sl; // Suppress messages for expected errors. f.connection.newSession(f.session.getId().getName()); BOOST_FAIL("Expected SessionBusyException for " << f.session.getId().getName()); } catch (const Exception&) {} // FIXME aconway 2008-09-22: client is not throwing correct exception. @@ -99,6 +104,8 @@ QPID_AUTO_TEST_CASE(DisconnectedListen) { ProxyConnection c(fix.broker->getPort()); fix.session.queueDeclare(arg::queue="q"); fix.subs.subscribe(l, "q"); + + ScopedSuppressLogging sl; // Suppress messages for expected errors. Thread t(fix.subs); fix.connection.proxy.close(); t.join(); @@ -107,6 +114,7 @@ QPID_AUTO_TEST_CASE(DisconnectedListen) { QPID_AUTO_TEST_CASE(NoSuchQueueTest) { ProxySessionFixture fix; + ScopedSuppressLogging sl; // Suppress messages for expected errors. BOOST_CHECK_THROW(fix.subs.subscribe(fix.lq, "no such queue"), NotFoundException); } diff --git a/cpp/src/tests/logging.cpp b/cpp/src/tests/logging.cpp index 97bfed2436..32163c0058 100644 --- a/cpp/src/tests/logging.cpp +++ b/cpp/src/tests/logging.cpp @@ -77,6 +77,7 @@ QPID_AUTO_TEST_CASE(testStatementEnabled) { // Verify that the singleton enables and disables static // log statements. Logger& l = Logger::instance(); + ScopedSuppressLogging ls(l); l.select(Selector(debug)); static Statement s=QPID_LOG_STATEMENT_INIT(debug); BOOST_CHECK(!s.enabled); @@ -110,7 +111,7 @@ struct TestOutput : public Logger::Output { using boost::assign::list_of; QPID_AUTO_TEST_CASE(testLoggerOutput) { - Logger l; + Logger l; l.clear(); l.select(Selector(debug)); Statement s=QPID_LOG_STATEMENT_INIT(debug); @@ -133,7 +134,7 @@ QPID_AUTO_TEST_CASE(testLoggerOutput) { QPID_AUTO_TEST_CASE(testMacro) { Logger& l=Logger::instance(); - l.clear(); + ScopedSuppressLogging ls(l); l.select(Selector(info)); TestOutput* out=new TestOutput(l); QPID_LOG(info, "foo"); @@ -152,6 +153,7 @@ QPID_AUTO_TEST_CASE(testMacro) { QPID_AUTO_TEST_CASE(testLoggerFormat) { Logger& l = Logger::instance(); + ScopedSuppressLogging ls(l); l.select(Selector(critical)); TestOutput* out=new TestOutput(l); @@ -165,20 +167,16 @@ QPID_AUTO_TEST_CASE(testLoggerFormat) { l.format(Logger::FUNCTION); QPID_LOG(critical, "foo"); + BOOST_CHECK_REGEX("void .*testLoggerFormat.*\\(\\): foo\n", out->last()); l.format(Logger::LEVEL); QPID_LOG(critical, "foo"); BOOST_CHECK_EQUAL("critical foo\n", out->last()); - - l.format(~0); // Everything - QPID_LOG(critical, "foo"); - string re=".* critical -?\\[[0-9a-f]*] "+string(__FILE__)+":\\d+:void .*testLoggerFormat.*\\(\\): foo\n"; - BOOST_CHECK_REGEX(re, out->last()); } QPID_AUTO_TEST_CASE(testOstreamOutput) { Logger& l=Logger::instance(); - l.clear(); + ScopedSuppressLogging ls(l); l.select(Selector(error)); ostringstream os; l.output(os); @@ -191,6 +189,7 @@ QPID_AUTO_TEST_CASE(testOstreamOutput) { #if 0 // This test requires manual intervention. Normally disabled. QPID_AUTO_TEST_CASE(testSyslogOutput) { Logger& l=Logger::instance(); + Logger::StateSaver ls(l); l.clear(); l.select(Selector(info)); l.syslog("qpid_test"); @@ -306,41 +305,9 @@ QPID_AUTO_TEST_CASE(testSelectorFromOptions) { BOOST_CHECK(s.isEnabled(critical, "foo")); } -QPID_AUTO_TEST_CASE(testOptionsFormat) { - Logger l; - { - Options opts(""); - BOOST_CHECK_EQUAL(Logger::TIME|Logger::LEVEL, l.format(opts)); - const char* argv[]={ - 0, - "--log-time", "no", - "--log-level", "no", - "--log-source", "1", - "--log-thread", "1" - }; - opts.parse(ARGC(argv), const_cast<char**>(argv)); - BOOST_CHECK_EQUAL( - Logger::FILE|Logger::LINE|Logger::THREAD, l.format(opts)); - } - { - Options opts(""); // Clear. - const char* argv[]={ - 0, - "--log-level", "no", - "--log-thread", "true", - "--log-function", "YES", - "--log-time", "YES" - }; - opts.parse(ARGC(argv), const_cast<char**>(argv)); - BOOST_CHECK_EQUAL( - Logger::THREAD|Logger::FUNCTION|Logger::TIME, - l.format(opts)); - } -} - -QPID_AUTO_TEST_CASE(testLoggerConfigure) { +QPID_AUTO_TEST_CASE(testLoggerStateure) { Logger& l=Logger::instance(); - l.clear(); + ScopedSuppressLogging ls(l); Options opts("test"); const char* argv[]={ 0, @@ -363,7 +330,7 @@ QPID_AUTO_TEST_CASE(testLoggerConfigure) { QPID_AUTO_TEST_CASE(testQuoteNonPrintable) { Logger& l=Logger::instance(); - l.clear(); + ScopedSuppressLogging ls(l); Options opts("test"); opts.outputs.clear(); opts.outputs.push_back("logging.tmp"); diff --git a/cpp/src/tests/test_tools.h b/cpp/src/tests/test_tools.h index 32127b0442..37a6594f8a 100644 --- a/cpp/src/tests/test_tools.h +++ b/cpp/src/tests/test_tools.h @@ -18,9 +18,9 @@ * limitations under the License. * */ +#include "qpid/log/Logger.h" #include <limits.h> // Include before boost/test headers. - #include <boost/test/test_tools.hpp> #include <boost/assign/list_of.hpp> #include <boost/regex.hpp> @@ -78,5 +78,17 @@ inline bool regexPredicate(const std::string& re, const std::string& text) { /** Check if types of two objects (as given by typeinfo::name()) match. */ #define BOOST_CHECK_TYPEID_EQUAL(a,b) BOOST_CHECK_EQUAL(typeid(a).name(),typeid(b).name()) +/** + * Supress all logging in a scope, restore to previous configuration in destructor. + */ +struct ScopedSuppressLogging { + typedef qpid::log::Logger Logger; + ScopedSuppressLogging(Logger& l=Logger::instance()) : logger(l), opts(l.getOptions()) { l.clear(); } + ~ScopedSuppressLogging() { logger.configure(opts); } + Logger& logger; + qpid::log::Options opts; +}; + + #endif /*!TEST_TOOLS_H*/ |