udpipmap.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "jsocket.hpp"
  15. #include <thread>
  16. #include "udpipmap.hpp"
  17. // Look up an IP to get a sender
  18. #ifdef _USE_CPPUNIT
  19. #include "unittests.hpp"
  20. class IpMapTest : public CppUnit::TestFixture
  21. {
  22. CPPUNIT_TEST_SUITE(IpMapTest);
  23. CPPUNIT_TEST(testIpMap);
  24. // CPPUNIT_TEST(testIpV6);
  25. CPPUNIT_TEST(testThread);
  26. CPPUNIT_TEST(testMulti);
  27. CPPUNIT_TEST_SUITE_END();
  28. static unsigned *createMapEntry(const ServerIdentifier &)
  29. {
  30. return new unsigned(3);
  31. }
  32. void testIpMap()
  33. {
  34. unsigned five = 5;
  35. auto createMapEntry = [five](const ServerIdentifier ip)
  36. {
  37. StringBuffer s;
  38. printf("adding ip %s\n", ip.getTraceText(s).str());
  39. return new unsigned(five);
  40. };
  41. IpMapOf<unsigned> map(createMapEntry);
  42. IpAddress notLocal("123.4.5.1");
  43. IpAddress notLocal2("123.4.6.1");
  44. IpAddress notLocal3("123.4.5.1");
  45. ASSERT(map.lookup(queryLocalIP())==five);
  46. ASSERT(&map.lookup(queryLocalIP())==&map.lookup(queryLocalIP()));
  47. ASSERT(&map.lookup(notLocal)!=&map.lookup(notLocal2));
  48. ASSERT(&map.lookup(notLocal)==&map.lookup(notLocal3));
  49. unsigned entries = 0;
  50. for (auto v:map)
  51. {
  52. ASSERT(v == 5);
  53. entries++;
  54. }
  55. printf("entries = %d\n", entries);
  56. ASSERT(entries == 3);
  57. }
  58. #if 0
  59. // Current implementation of IpMapOf assumes ipv4
  60. void testIpV6()
  61. {
  62. IpAddress ip1("fe80::1c7e:ebe8:4ee8:6154");
  63. IpAddress ip2("fe80::1c37:fb7f:f657:d57a");
  64. ASSERT(ip1.fasthash() != ip2.fasthash());
  65. IpMapOf<unsigned> map(createMapEntry);
  66. ASSERT(&map.lookup(ip1)!=&map.lookup(ip2));
  67. ASSERT(&map.lookup(ip1)==&map.lookup(ip1));
  68. }
  69. #endif
  70. class IpEntry
  71. {
  72. public:
  73. IpEntry(const ServerIdentifier &_s) : s(_s)
  74. {
  75. numCreated++;
  76. }
  77. ServerIdentifier s;
  78. static RelaxedAtomic<unsigned> numCreated;
  79. };
  80. void testThread()
  81. {
  82. IpEntry::numCreated = 0;
  83. IpMapOf<IpEntry> map([](const ServerIdentifier s){return new IpEntry(s); });
  84. std::thread threads[100];
  85. Semaphore ready;
  86. for (int i = 0; i < 100; i++)
  87. {
  88. threads[i] = std::thread([&]()
  89. {
  90. ready.wait();
  91. IpAddress startIP("10.0.0.1");
  92. for (int i = 0; i < 1000; i++)
  93. {
  94. map.lookup(startIP);
  95. startIP.ipincrement(1);
  96. }
  97. });
  98. }
  99. ready.signal(100);
  100. for (int i = 0; i < 100; i++)
  101. {
  102. threads[i].join();
  103. }
  104. ASSERT(IpEntry::numCreated == 1000)
  105. unsigned numInTable = 0;
  106. for (auto &&i: map)
  107. {
  108. auto &entry = map[i.s];
  109. ASSERT(&entry==&i);
  110. numInTable++;
  111. }
  112. ASSERT(numInTable == 1000);
  113. }
  114. void testMulti()
  115. {
  116. for (unsigned i = 0; i < 1000; i++)
  117. testThread();
  118. }
  119. };
  120. RelaxedAtomic<unsigned> IpMapTest::IpEntry::numCreated {0};
  121. CPPUNIT_TEST_SUITE_REGISTRATION( IpMapTest );
  122. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IpMapTest, "IpMapTest" );
  123. #endif