datasrc_clients_mgr_unittest.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <cc/data.h>
  15. #include <auth/datasrc_clients_mgr.h>
  16. #include "test_datasrc_clients_mgr.h"
  17. #include <gtest/gtest.h>
  18. #include <boost/function.hpp>
  19. using namespace isc::auth;
  20. using namespace isc::auth::internal;
  21. namespace {
  22. void
  23. shutdownCheck() {
  24. // Check for common points on shutdown. The manager should have acquired
  25. // the lock, put a SHUTDOWN command to the queue, and should have signaled
  26. // the builder.
  27. EXPECT_EQ(1, FakeDataSrcClientsBuilder::queue_mutex->lock_count);
  28. EXPECT_EQ(1, FakeDataSrcClientsBuilder::cond->signal_count);
  29. EXPECT_EQ(1, FakeDataSrcClientsBuilder::command_queue->size());
  30. const Command& cmd = FakeDataSrcClientsBuilder::command_queue->front();
  31. EXPECT_EQ(SHUTDOWN, cmd.first);
  32. EXPECT_FALSE(cmd.second); // no argument
  33. // Finally, the manager should wait for the thread to terminate.
  34. EXPECT_TRUE(FakeDataSrcClientsBuilder::thread_waited);
  35. }
  36. TEST(DataSrcClientsMgrTest, start) {
  37. // When we create a manager, builder's run() method should be called.
  38. FakeDataSrcClientsBuilder::started = false;
  39. {
  40. TestDataSrcClientsMgr mgr;
  41. EXPECT_TRUE(FakeDataSrcClientsBuilder::started);
  42. EXPECT_TRUE(FakeDataSrcClientsBuilder::command_queue->empty());
  43. // Check pre-destroy conditions
  44. EXPECT_EQ(0, FakeDataSrcClientsBuilder::cond->signal_count);
  45. EXPECT_FALSE(FakeDataSrcClientsBuilder::thread_waited);
  46. } // mgr and builder have been destroyed by this point.
  47. // We stopped the manager implicitly (without shutdown()). The manager
  48. // will internally notify it
  49. shutdownCheck();
  50. }
  51. TEST(DataSrcClientsMgrTest, shutdownWithUncaughtException) {
  52. // Emulating the case when the builder exists on exception. shutdown()
  53. // will encounter UncaughtException exception and catch it.
  54. EXPECT_NO_THROW({
  55. TestDataSrcClientsMgr mgr;
  56. FakeDataSrcClientsBuilder::thread_throw_on_wait =
  57. FakeDataSrcClientsBuilder::THROW_UNCAUGHT_EX;
  58. });
  59. }
  60. TEST(DataSrcClientsMgrTest, shutdownWithException) {
  61. EXPECT_NO_THROW({
  62. TestDataSrcClientsMgr mgr;
  63. FakeDataSrcClientsBuilder::thread_throw_on_wait =
  64. FakeDataSrcClientsBuilder::THROW_OTHER;
  65. });
  66. }
  67. TEST(DataSrcClientsMgrTest, realThread) {
  68. // Using the non-test definition with a real thread. Just checking
  69. // no disruption happens.
  70. DataSrcClientsMgr mgr;
  71. }
  72. } // unnamed namespace