zone_entry_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (C) 2010 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. // $Id$
  15. #include <gtest/gtest.h>
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/thread.hpp>
  18. #include <dns/rrclass.h>
  19. #include "../asiolink.h"
  20. #include "../zone_entry.h"
  21. #include "../nameserver_entry.h"
  22. #include "../address_request_callback.h"
  23. #include "nsas_test.h"
  24. using namespace asiolink;
  25. using namespace std;
  26. using namespace boost;
  27. using namespace isc::dns;
  28. namespace isc {
  29. namespace nsas {
  30. // String constants. These should end in a dot.
  31. static const std::string EXAMPLE_CO_UK("example.co.uk.");
  32. static const std::string EXAMPLE_NET("example.net.");
  33. /// \brief Test Fixture Class
  34. class ZoneEntryTest : public ::testing::Test {
  35. protected:
  36. };
  37. /// Tests of the default constructor
  38. TEST_F(ZoneEntryTest, DefaultConstructor) {
  39. // Default constructor should not create any RRsets
  40. ZoneEntry alpha(EXAMPLE_CO_UK, RRClass::IN().getCode());
  41. EXPECT_EQ(EXAMPLE_CO_UK, alpha.getName());
  42. EXPECT_EQ(RRClass::IN().getCode(), alpha.getClass());
  43. }
  44. namespace {
  45. // Just something that can be created and passed
  46. class Callback : public AddressRequestCallback {
  47. public:
  48. void success(const asiolink::IOAddress&) { };
  49. void unreachable() { };
  50. };
  51. }
  52. TEST_F(ZoneEntryTest, Callbacks) {
  53. const size_t count(3);
  54. shared_ptr<AddressRequestCallback> callbacks[count];
  55. ZoneEntry zone(EXAMPLE_CO_UK, RRClass::IN().getCode());
  56. EXPECT_FALSE(zone.hasCallbacks());
  57. for (size_t i(0); i < count; ++ i) {
  58. zone.addCallback(callbacks[i] = shared_ptr<AddressRequestCallback>(
  59. new Callback));
  60. }
  61. for (size_t i(0); i < count; ++ i) {
  62. ASSERT_TRUE(zone.hasCallbacks());
  63. EXPECT_EQ(callbacks[i], zone.popCallback());
  64. }
  65. EXPECT_FALSE(zone.hasCallbacks());
  66. }
  67. TEST_F(ZoneEntryTest, NameserverIterators) {
  68. ZoneEntry zone(EXAMPLE_CO_UK, RRClass::IN().getCode());
  69. shared_ptr<NameserverEntry> nse(new NameserverEntry(EXAMPLE_CO_UK,
  70. RRClass::IN().getCode()));
  71. // The iterator can't be printed, so we can't use EQ
  72. const ZoneEntry& zone_const(zone);
  73. EXPECT_TRUE(zone.begin() == zone.end());
  74. EXPECT_TRUE(zone_const.begin() == zone_const.end());
  75. zone.nameserverAdd(nse);
  76. EXPECT_FALSE(zone.begin() == zone.end());
  77. EXPECT_FALSE(zone_const.begin() == zone_const.end());
  78. EXPECT_TRUE(*zone.begin() == nse);
  79. EXPECT_TRUE(*zone_const.begin() == nse);
  80. EXPECT_TRUE(zone.begin() + 1 == zone.end());
  81. EXPECT_TRUE(zone_const.begin() + 1 == zone_const.end());
  82. }
  83. void lockAndWait(ZoneEntry* zone, barrier* when) {
  84. ZoneEntry::Lock lock(zone->getLock());
  85. when->wait();
  86. }
  87. void lockAndKeep(ZoneEntry* zone, bool* locked_self, bool* locked_other,
  88. barrier* when)
  89. {
  90. // Wait for go signal
  91. when->wait();
  92. // Lock
  93. ZoneEntry::Lock lock(zone->getLock());
  94. *locked_self = true;
  95. // Wait for the start of the other thread
  96. when->wait();
  97. // Make sure the other thread gets a chance to run
  98. for (int i(0); i < 100; ++ i) {
  99. this_thread::yield();
  100. EXPECT_FALSE(*locked_other);
  101. }
  102. *locked_self = false;
  103. }
  104. TEST_F(ZoneEntryTest, Lock) {
  105. // Create some testing data
  106. ZoneEntry z1(EXAMPLE_CO_UK, RRClass::IN().getCode());
  107. ZoneEntry z2(EXAMPLE_NET, RRClass::IN().getCode());
  108. shared_ptr<NameserverEntry> ns1(new NameserverEntry(EXAMPLE_CO_UK,
  109. RRClass::IN().getCode()));
  110. shared_ptr<NameserverEntry> ns2(new NameserverEntry(EXAMPLE_NET,
  111. RRClass::IN().getCode()));
  112. z1.nameserverAdd(ns1);
  113. z2.nameserverAdd(ns2);
  114. barrier both(2);
  115. // This tries that both can lock right now.
  116. // FIXME If they can't it will deadlock. Any idea how to do it better?
  117. thread t1(lockAndWait, &z1, &both);
  118. thread t2(lockAndWait, &z2, &both);
  119. t1.join();
  120. t2.join();
  121. z1.nameserverAdd(ns2);
  122. z2.nameserverAdd(ns1);
  123. // Now check that they can't both lock at the same time.
  124. barrier both_second(2);
  125. bool l1(false), l2(false);
  126. thread t3(lockAndKeep, &z1, &l1, &l2, &both);
  127. // Let this one run into the lock, not the other
  128. both.wait();
  129. thread t4(lockAndKeep, &z2, &l2, &l1, &both_second);
  130. // Let it start the loop
  131. both.wait();
  132. // Let this one run to the lock and wait on it
  133. both_second.wait();
  134. // Make sure the threads has time now
  135. for (int i(0); i < 100; ++ i) {
  136. this_thread::yield();
  137. }
  138. both_second.wait();
  139. t3.join();
  140. t4.join();
  141. // Try it the other way around (so it does not depend on the order of nameservers
  142. thread t6(lockAndKeep, &z2, &l2, &l1, &both);
  143. both.wait();
  144. thread t5(lockAndKeep, &z1, &l1, &l2, &both_second);
  145. both.wait();
  146. both_second.wait();
  147. for (int i(0); i < 100; ++ i) {
  148. this_thread::yield();
  149. }
  150. both_second.wait();
  151. t5.join();
  152. t6.join();
  153. }
  154. } // namespace nsas
  155. } // namespace isc