Browse Source

[1704] Unit test the logger for the lock

Mukund Sivaraman 13 years ago
parent
commit
c0a328b2d9

+ 7 - 0
src/lib/log/logger.cc

@@ -73,6 +73,13 @@ Logger::getEffectiveSeverity() {
     return (getLoggerPtr()->getEffectiveSeverity());
 }
 
+// Replace the interprocess synchronization object
+
+void
+Logger::setInterprocessSync(isc::util::InterprocessSync* sync) {
+    getLoggerPtr()->setInterprocessSync(sync);
+}
+
 // Debug level (only relevant if messages of severity DEBUG are being logged).
 
 int

+ 7 - 0
src/lib/log/logger.h

@@ -25,6 +25,7 @@
 #include <log/message_types.h>
 #include <log/log_formatter.h>
 
+#include <util/interprocess_sync.h>
 
 namespace isc {
 namespace log {
@@ -178,6 +179,12 @@ public:
     /// is the severity of the parent.
     virtual isc::log::Severity getEffectiveSeverity();
 
+    /// \brief Replace the interprocess synchronization object
+    ///
+    /// \param sync The logger uses this synchronization object for
+    /// synchronizing output of log messages.
+    void setInterprocessSync(isc::util::InterprocessSync* sync);
+
     /// \brief Return DEBUG Level
     ///
     /// \return Current setting of debug level.  This is returned regardless of

+ 9 - 0
src/lib/log/logger_impl.cc

@@ -33,6 +33,7 @@
 #include <log/message_types.h>
 
 #include <util/strutil.h>
+#include <util/interprocess_sync_file.h>
 
 // Note: as log4cplus and the BIND 10 logger have many concepts in common, and
 // thus many similar names, to disambiguate types we don't "use" the log4cplus
@@ -75,6 +76,14 @@ LoggerImpl::getSeverity() {
     return level.severity;
 }
 
+// Replace the interprocess synchronization object
+
+void
+LoggerImpl::setInterprocessSync(isc::util::InterprocessSync* sync) {
+    delete sync_;
+    sync_ = sync;
+}
+
 // Return current debug level (only valid if current severity level is DEBUG).
 int
 LoggerImpl::getDebugLevel() {

+ 7 - 1
src/lib/log/logger_impl.h

@@ -32,7 +32,7 @@
 #include <log/logger_level_impl.h>
 #include <log/message_types.h>
 
-#include <util/interprocess_sync_file.h>
+#include <util/interprocess_sync.h>
 
 namespace isc {
 namespace log {
@@ -110,6 +110,12 @@ public:
     virtual Severity getEffectiveSeverity();
 
 
+    /// \brief Replace the interprocess synchronization object
+    ///
+    /// \param sync The logger uses this synchronization object for
+    /// synchronizing output of log messages.
+    void setInterprocessSync(isc::util::InterprocessSync* sync);
+
     /// \brief Return debug level
     ///
     /// \return Current setting of debug level.  This will be zero if the

+ 3 - 0
src/lib/log/tests/Makefile.am

@@ -13,6 +13,8 @@ endif
 
 CLEANFILES = *.gcno *.gcda
 
+EXTRA_DIST = log_test_messages.mes
+
 noinst_PROGRAMS = logger_example
 logger_example_SOURCES = logger_example.cc
 logger_example_CPPFLAGS = $(AM_CPPFLAGS)
@@ -59,6 +61,7 @@ run_unittests_SOURCES += logger_manager_unittest.cc
 run_unittests_SOURCES += logger_name_unittest.cc
 run_unittests_SOURCES += logger_support_unittest.cc
 run_unittests_SOURCES += logger_unittest.cc
+run_unittests_SOURCES += log_test_messages.cc log_test_messages.h
 run_unittests_SOURCES += logger_specification_unittest.cc
 run_unittests_SOURCES += message_dictionary_unittest.cc
 run_unittests_SOURCES += message_reader_unittest.cc

+ 25 - 0
src/lib/log/tests/log_test_messages.cc

@@ -0,0 +1,25 @@
+// File created from log_test_messages.mes on Thu May 24 12:52:20 2012
+
+#include <cstddef>
+#include <log/message_types.h>
+#include <log/message_initializer.h>
+
+namespace isc {
+namespace log {
+
+extern const isc::log::MessageID LOG_LOCK_TEST_MESSAGE = "LOG_LOCK_TEST_MESSAGE";
+
+} // namespace log
+} // namespace isc
+
+namespace {
+
+const char* values[] = {
+    "LOG_LOCK_TEST_MESSAGE", "this is a test message.",
+    NULL
+};
+
+const isc::log::MessageInitializer initializer(values);
+
+} // Anonymous namespace
+

+ 16 - 0
src/lib/log/tests/log_test_messages.h

@@ -0,0 +1,16 @@
+// File created from log_test_messages.mes on Thu May 24 12:52:20 2012
+
+#ifndef __LOG_TEST_MESSAGES_H
+#define __LOG_TEST_MESSAGES_H
+
+#include <log/message_types.h>
+
+namespace isc {
+namespace log {
+
+extern const isc::log::MessageID LOG_LOCK_TEST_MESSAGE;
+
+} // namespace log
+} // namespace isc
+
+#endif // __LOG_TEST_MESSAGES_H

+ 26 - 0
src/lib/log/tests/log_test_messages.mes

@@ -0,0 +1,26 @@
+# Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+# \brief Message Utility Message File
+#
+# This is the source of the set of messages generated by the message and
+# logging components.  The associated .h and .cc files are created by hand from
+# this file though and are not built during the build process; this is to avoid
+# the chicken-and-egg situation where we need the files to build the message
+# compiler, yet we need the compiler to build the files.
+
+$NAMESPACE isc::log
+
+% LOG_LOCK_TEST_MESSAGE this is a test message.
+This is a log message used in testing.

+ 58 - 0
src/lib/log/tests/logger_unittest.cc

@@ -23,6 +23,9 @@
 #include <log/logger_manager.h>
 #include <log/logger_name.h>
 #include <log/log_messages.h>
+#include "log/tests/log_test_messages.h"
+
+#include <util/interprocess_sync_file.h>
 
 using namespace isc;
 using namespace isc::log;
@@ -379,3 +382,58 @@ TEST_F(LoggerTest, LoggerNameLength) {
     }, ".*");
 #endif
 }
+
+class MockSync : public isc::util::InterprocessSync {
+public:
+    /// \brief Constructor
+    MockSync(const std::string component_name) :
+        InterprocessSync(component_name), was_locked_(false), was_unlocked_(false)
+    {}
+
+    bool wasLocked() const {
+        return was_locked_;
+    }
+
+    bool wasUnlocked() const {
+        return was_unlocked_;
+    }
+
+protected:
+    bool lock() {
+        was_locked_ = true;
+        return true;
+    }
+
+    bool tryLock() {
+        return true;
+    }
+
+    bool unlock() {
+        was_unlocked_ = true;
+        return true;
+    }
+
+private:
+    bool was_locked_;
+    bool was_unlocked_;
+};
+
+// Checks that the logger logs exclusively and other BIND 10 components
+// are locked out.
+
+TEST_F(LoggerTest, Lock) {
+    // Create a logger
+    Logger logger("alpha");
+
+    // Setup our own mock sync object so that we can intercept the lock
+    // call and check if a lock has been taken.
+    MockSync *sync = new MockSync("logger");
+    logger.setInterprocessSync(sync);
+
+    // Log a message and put things into play.
+    logger.setSeverity(isc::log::INFO, 100);
+    logger.info(LOG_LOCK_TEST_MESSAGE);
+
+    EXPECT_TRUE(sync->wasLocked());
+    EXPECT_TRUE(sync->wasUnlocked());
+}