Browse Source

[2205] introduce DataSrcClientsMgr, tested initial startup.

JINMEI Tatuya 12 years ago
parent
commit
0c6ad26d37

+ 14 - 4
src/bin/auth/datasrc_clients_mgr.h

@@ -15,11 +15,12 @@
 #ifndef DATASRC_CLIENTS_MGR_H
 #define DATASRC_CLIENTS_MGR_H 1
 
-//#include <util/threads/thread.h>
 #include <util/threads/lock.h>
 
 #include <cc/data.h>
 
+#include <boost/bind.hpp>
+
 #include <list>
 #include <utility>
 
@@ -68,18 +69,27 @@ private:
 };
 }
 
-template <typename ThreadType, typename MutexType, typename CondVarType>
+template <typename ThreadType, typename BuilderType, typename MutexType,
+          typename CondVarType>
 class DataSrcClientsMgrBase {
 public:
-    DataSrcClientsMgrBase() : builder_(&command_queue_, &cond_, &queue_mutex_)
+    DataSrcClientsMgrBase() :
+        builder_(&command_queue_, &cond_, &queue_mutex_),
+        builder_thread_(boost::bind(&BuilderType::run, &builder_))
     {}
     ~DataSrcClientsMgrBase() {}
+#ifdef notyet
+    void shutdown() {
+        builder_thread_.wait();
+    }
+#endif
 
 private:
     std::list<internal::Command> command_queue_;
     CondVarType cond_;
     MutexType queue_mutex_;
-    internal::DataSrcClientsBuilderBase<MutexType, CondVarType> builder_;
+    BuilderType builder_;
+    ThreadType builder_thread_;
 };
 
 namespace internal {

+ 1 - 0
src/bin/auth/tests/Makefile.am

@@ -53,6 +53,7 @@ run_unittests_SOURCES += query_unittest.cc
 run_unittests_SOURCES += statistics_unittest.cc
 run_unittests_SOURCES += test_datasrc_clients_mgr.h test_datasrc_clients_mgr.cc
 run_unittests_SOURCES += datasrc_clients_builder_unittest.cc
+run_unittests_SOURCES += datasrc_clients_mgr_unittest.cc
 run_unittests_SOURCES += datasrc_config_unittest.cc
 run_unittests_SOURCES += run_unittests.cc
 

+ 39 - 0
src/bin/auth/tests/datasrc_clients_mgr_unittest.cc

@@ -0,0 +1,39 @@
+// 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.
+
+#include <cc/data.h>
+
+#include <auth/datasrc_clients_mgr.h>
+#include "test_datasrc_clients_mgr.h"
+
+#include <gtest/gtest.h>
+
+#include <boost/function.hpp>
+
+using namespace isc::auth;
+using namespace isc::auth::internal;
+
+namespace {
+class DataSrcClientsMgrTest : public ::testing::Test {
+};
+
+TEST_F(DataSrcClientsMgrTest, start) {
+    // When we create a manager, builder's run() method should be called.
+    FakeDataSrcClientsBuilder::started = false;
+    TestDataSrcClientsMgr mgr;
+    EXPECT_TRUE(FakeDataSrcClientsBuilder::started);
+    EXPECT_TRUE(FakeDataSrcClientsBuilder::command_queue->empty());
+}
+
+} // unnamed namespace

+ 6 - 0
src/bin/auth/tests/test_datasrc_clients_mgr.cc

@@ -16,6 +16,12 @@
 
 namespace isc {
 namespace auth {
+// Define static DataSrcClientsBuilder member variables.
+bool FakeDataSrcClientsBuilder::started = false;
+std::list<internal::Command>* FakeDataSrcClientsBuilder::command_queue = NULL;
+internal::TestCondVar* FakeDataSrcClientsBuilder::cond = NULL;
+internal::TestMutex* FakeDataSrcClientsBuilder::queue_mutex = NULL;
+
 namespace internal {
 template<>
 void

+ 48 - 12
src/bin/auth/tests/test_datasrc_clients_mgr.h

@@ -47,11 +47,14 @@ public:
 
 class TestCondVar {
 public:
+    TestCondVar() : wait_count(0), command_queue_(NULL),
+                    delayed_command_queue_(NULL)
+    {}
     TestCondVar(std::list<Command>& command_queue,
                 std::list<Command>& delayed_command_queue) :
         wait_count(0),
-        command_queue_(command_queue),
-        delayed_command_queue_(delayed_command_queue)
+        command_queue_(&command_queue),
+        delayed_command_queue_(&delayed_command_queue)
     {
     }
     void wait(TestMutex& mutex) {
@@ -61,17 +64,12 @@ public:
         ++mutex.lock_count;
 
         // make the delayed commands available
-        command_queue_.splice(command_queue_.end(), delayed_command_queue_);
+        command_queue_->splice(command_queue_->end(), *delayed_command_queue_);
     }
     size_t wait_count;
 private:
-    std::list<Command>& command_queue_;
-    std::list<Command>& delayed_command_queue_;
-};
-
-class TestThread {
-public:
-    TestThread(const boost::function<void()>& /*main*/);
+    std::list<Command>* command_queue_;
+    std::list<Command>* delayed_command_queue_;
 };
 
 // Convenient shortcut
@@ -85,8 +83,46 @@ void
 TestDataSrcClientsBuilder::doNoop();
 
 } // namespace internal
-}
-}
+
+// A specialization of DataSrcClientsBuilder that allows tests to inspect
+// its internal states via static class variables.  Using static is suboptimal,
+// but DataSrcClientsMgr is highly encapsulated, this seems to be the best
+// possible compromise.
+class FakeDataSrcClientsBuilder {
+public:
+    FakeDataSrcClientsBuilder(std::list<internal::Command>* command_queue,
+                              internal::TestCondVar* cond,
+                              internal::TestMutex* queue_mutex)
+    {
+        FakeDataSrcClientsBuilder::started = false;
+        FakeDataSrcClientsBuilder::command_queue = command_queue;
+        FakeDataSrcClientsBuilder::cond = cond;
+        FakeDataSrcClientsBuilder::queue_mutex = queue_mutex;
+    }
+    void run() {
+        FakeDataSrcClientsBuilder::started = true;
+    }
+
+    static bool started;
+    static std::list<internal::Command>* command_queue;
+    static internal::TestCondVar* cond;
+    static internal::TestMutex* queue_mutex;
+};
+
+class TestThread {
+public:
+    TestThread(const boost::function<void()>& main) {
+        main();
+    }
+};
+
+// Convenient shortcut
+typedef DataSrcClientsMgrBase<TestThread, FakeDataSrcClientsBuilder,
+                              internal::TestMutex, internal::TestCondVar>
+TestDataSrcClientsMgr;
+
+} // namespace auth
+} // namespace isc
 
 #endif  // TEST_DATASRC_CLIENTS_MGR_H