datasrc_clients_mgr_unittest.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <exceptions/exceptions.h>
  15. #include <dns/rrclass.h>
  16. #include <cc/data.h>
  17. #include <datasrc/client_list.h>
  18. #include <auth/datasrc_clients_mgr.h>
  19. #include "test_datasrc_clients_mgr.h"
  20. #include <gtest/gtest.h>
  21. #include <boost/function.hpp>
  22. using namespace isc::dns;
  23. using namespace isc::data;
  24. using namespace isc::datasrc;
  25. using namespace isc::auth;
  26. using namespace isc::auth::datasrc_clientmgr_internal;
  27. namespace {
  28. void
  29. shutdownCheck() {
  30. // Check for common points on shutdown. The manager should have acquired
  31. // the lock, put a SHUTDOWN command to the queue, and should have signaled
  32. // the builder.
  33. EXPECT_EQ(1, FakeDataSrcClientsBuilder::queue_mutex->lock_count);
  34. EXPECT_EQ(1, FakeDataSrcClientsBuilder::cond->signal_count);
  35. EXPECT_EQ(1, FakeDataSrcClientsBuilder::command_queue->size());
  36. const Command& cmd = FakeDataSrcClientsBuilder::command_queue->front();
  37. EXPECT_EQ(SHUTDOWN, cmd.first);
  38. EXPECT_FALSE(cmd.second); // no argument
  39. // Finally, the manager should wait for the thread to terminate.
  40. EXPECT_TRUE(FakeDataSrcClientsBuilder::thread_waited);
  41. }
  42. // Commonly used pattern of checking member variables shared between the
  43. // manager and builder.
  44. void
  45. checkSharedMembers(size_t expected_queue_lock_count,
  46. size_t expected_queue_unlock_count,
  47. size_t expected_map_lock_count,
  48. size_t expected_map_unlock_count,
  49. size_t expected_cond_signal_count,
  50. size_t expected_command_queue_size)
  51. {
  52. EXPECT_EQ(expected_queue_lock_count,
  53. FakeDataSrcClientsBuilder::queue_mutex->lock_count);
  54. EXPECT_EQ(expected_queue_unlock_count,
  55. FakeDataSrcClientsBuilder::queue_mutex->unlock_count);
  56. EXPECT_EQ(expected_map_lock_count,
  57. FakeDataSrcClientsBuilder::map_mutex->lock_count);
  58. EXPECT_EQ(expected_map_unlock_count,
  59. FakeDataSrcClientsBuilder::map_mutex->unlock_count);
  60. EXPECT_EQ(expected_cond_signal_count,
  61. FakeDataSrcClientsBuilder::cond->signal_count);
  62. EXPECT_EQ(expected_command_queue_size,
  63. FakeDataSrcClientsBuilder::command_queue->size());
  64. }
  65. TEST(DataSrcClientsMgrTest, start) {
  66. // When we create a manager, builder's run() method should be called.
  67. FakeDataSrcClientsBuilder::started = false;
  68. {
  69. TestDataSrcClientsMgr mgr;
  70. EXPECT_TRUE(FakeDataSrcClientsBuilder::started);
  71. EXPECT_TRUE(FakeDataSrcClientsBuilder::command_queue->empty());
  72. // Check pre-destroy conditions
  73. EXPECT_EQ(0, FakeDataSrcClientsBuilder::cond->signal_count);
  74. EXPECT_FALSE(FakeDataSrcClientsBuilder::thread_waited);
  75. } // mgr and builder have been destroyed by this point.
  76. // We stopped the manager implicitly (without shutdown()). The manager
  77. // will internally notify it
  78. shutdownCheck();
  79. }
  80. TEST(DataSrcClientsMgrTest, shutdownWithUncaughtException) {
  81. // Emulating the case when the builder exists on exception. shutdown()
  82. // will encounter UncaughtException exception and catch it.
  83. EXPECT_NO_THROW({
  84. TestDataSrcClientsMgr mgr;
  85. FakeDataSrcClientsBuilder::thread_throw_on_wait =
  86. FakeDataSrcClientsBuilder::THROW_UNCAUGHT_EX;
  87. });
  88. }
  89. TEST(DataSrcClientsMgrTest, shutdownWithException) {
  90. EXPECT_NO_THROW({
  91. TestDataSrcClientsMgr mgr;
  92. FakeDataSrcClientsBuilder::thread_throw_on_wait =
  93. FakeDataSrcClientsBuilder::THROW_OTHER;
  94. });
  95. }
  96. TEST(DataSrcClientsMgrTest, reconfigure) {
  97. TestDataSrcClientsMgr mgr;
  98. // Check pre-command condition
  99. checkSharedMembers(0, 0, 0, 0, 0, 0);
  100. // A valid reconfigure argument
  101. ConstElementPtr reconfigure_arg = Element::fromJSON(
  102. "{" "\"IN\": [{\"type\": \"MasterFiles\", \"params\": {},"
  103. " \"cache-enable\": true}]}");
  104. // On reconfigure(), it just send the RECONFIGURE command to the builder
  105. // thread with the given argument intact.
  106. mgr.reconfigure(reconfigure_arg);
  107. // The manager should have acquired the queue lock, send RECONFIGURE
  108. // command with the arg, wake up the builder thread by signal. It doesn't
  109. // touch or refer to the map, so it shouldn't acquire the map lock.
  110. checkSharedMembers(1, 1, 0, 0, 1, 1);
  111. const Command& cmd1 = FakeDataSrcClientsBuilder::command_queue->front();
  112. EXPECT_EQ(RECONFIGURE, cmd1.first);
  113. EXPECT_EQ(reconfigure_arg, cmd1.second);
  114. // Non-null, but semantically invalid argument. The manager doesn't do
  115. // this check, so it should result in the same effect.
  116. FakeDataSrcClientsBuilder::command_queue->clear();
  117. reconfigure_arg = isc::data::Element::create("{ \"foo\": \"bar\" }");
  118. mgr.reconfigure(reconfigure_arg);
  119. checkSharedMembers(2, 2, 0, 0, 2, 1);
  120. const Command& cmd2 = FakeDataSrcClientsBuilder::command_queue->front();
  121. EXPECT_EQ(RECONFIGURE, cmd2.first);
  122. EXPECT_EQ(reconfigure_arg, cmd2.second);
  123. // Passing NULL argument is immediately rejected
  124. EXPECT_THROW(mgr.reconfigure(ConstElementPtr()), isc::InvalidParameter);
  125. checkSharedMembers(2, 2, 0, 0, 2, 1); // no state change
  126. }
  127. TEST(DataSrcClientsMgrTest, holder) {
  128. TestDataSrcClientsMgr mgr;
  129. {
  130. // Initially it's empty, so findClientList() will always return NULL
  131. TestDataSrcClientsMgr::Holder holder(mgr);
  132. EXPECT_FALSE(holder.findClientList(RRClass::IN()));
  133. EXPECT_FALSE(holder.findClientList(RRClass::CH()));
  134. // map should be protected here
  135. EXPECT_EQ(1, FakeDataSrcClientsBuilder::map_mutex->lock_count);
  136. EXPECT_EQ(0, FakeDataSrcClientsBuilder::map_mutex->unlock_count);
  137. }
  138. // map lock has been released
  139. EXPECT_EQ(1, FakeDataSrcClientsBuilder::map_mutex->unlock_count);
  140. // Put something in, that should become visible.
  141. ConstElementPtr reconfigure_arg = Element::fromJSON(
  142. "{" "\"IN\": [{\"type\": \"MasterFiles\", \"params\": {},"
  143. " \"cache-enable\": true}],"
  144. "\"CH\": [{\"type\": \"MasterFiles\", \"params\": {},"
  145. " \"cache-enable\": true}]}");
  146. mgr.reconfigure(reconfigure_arg);
  147. {
  148. TestDataSrcClientsMgr::Holder holder(mgr);
  149. EXPECT_TRUE(holder.findClientList(RRClass::IN()));
  150. EXPECT_TRUE(holder.findClientList(RRClass::CH()));
  151. }
  152. // We need to clear command queue by hand
  153. FakeDataSrcClientsBuilder::command_queue->clear();
  154. // Replace the lists with new lists containing only one list.
  155. // The CH will disappear again.
  156. reconfigure_arg = Element::fromJSON(
  157. "{" "\"IN\": [{\"type\": \"MasterFiles\", \"params\": {},"
  158. " \"cache-enable\": true}]}");
  159. mgr.reconfigure(reconfigure_arg);
  160. {
  161. TestDataSrcClientsMgr::Holder holder(mgr);
  162. EXPECT_TRUE(holder.findClientList(RRClass::IN()));
  163. EXPECT_FALSE(holder.findClientList(RRClass::CH()));
  164. }
  165. // Duplicate lock acquisition is prohibited (only test mgr can detect
  166. // this reliably, so this test may not be that useful)
  167. TestDataSrcClientsMgr::Holder holder1(mgr);
  168. EXPECT_THROW(TestDataSrcClientsMgr::Holder holder2(mgr), isc::Unexpected);
  169. }
  170. TEST(DataSrcClientsMgrTest, realThread) {
  171. // Using the non-test definition with a real thread. Just checking
  172. // no disruption happens.
  173. DataSrcClientsMgr mgr;
  174. }
  175. } // unnamed namespace