]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Some systems have a low-resolution nanosleep(2), calling it will
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 19 Dec 2022 08:08:14 +0000 (09:08 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 19 Dec 2022 08:13:25 +0000 (09:13 +0100)
sleep for at least a few ms. Compensate for that by running fewer
loops with longer sleeps.

Also use dns_random and make sure it is initlized properly for all tests.

pdns/recursordist/test-rec-tcounters_cc.cc
pdns/recursordist/test-recpacketcache_cc.cc
pdns/recursordist/testrunner.cc

index a86cab0d343145aaa2151e6153b96d651faed14c..bdeede0699b1c8d773fe28fba126aee141327895 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <unistd.h>
 #include <thread>
+#include "dns_random.hh"
 #include "rec-tcounters.hh"
 
 static rec::GlobalCounters global;
@@ -49,7 +50,7 @@ BOOST_AUTO_TEST_CASE(update_fast)
       ++tlocal.at(rec::Counter::servFails);
       ++tlocal.at(rec::Counter::nxDomains);
       tlocal.at(rec::DoubleWAvgCounter::avgLatencyUsec).add(1.1);
-      if (random() % 10000 == 0) {
+      if (dns_random(10000) == 0) {
         tlocal.updateSnap();
       }
     }
@@ -60,7 +61,7 @@ BOOST_AUTO_TEST_CASE(update_fast)
       ++tlocal.at(rec::Counter::servFails);
       ++tlocal.at(rec::Counter::nxDomains);
       tlocal.at(rec::DoubleWAvgCounter::avgLatencyUsec).add(2.2);
-      if (random() % 10000 == 0) {
+      if (dns_random(10000) == 0) {
         tlocal.updateSnap();
       }
     }
@@ -90,18 +91,18 @@ BOOST_AUTO_TEST_CASE(update_with_sleep)
 
   std::atomic<int> done{};
 
-  const size_t count = 1000000;
+  const size_t count = 100;
   std::thread thread1([&done] {
     for (size_t i = 0; i < count; i++) {
       ++tlocal.at(rec::Counter::servFails);
       ++tlocal.at(rec::Counter::nxDomains);
       tlocal.at(rec::DoubleWAvgCounter::avgLatencyUsec).add(1.1);
-      if (random() % 10000 == 0) {
+      if (dns_random(10000) == 0) {
         tlocal.updateSnap();
       }
       struct timespec interval
       {
-        0, random() % 5000
+        0, dns_random(20 * 1000 * 1000)
       };
       nanosleep(&interval, nullptr);
     }
@@ -112,12 +113,12 @@ BOOST_AUTO_TEST_CASE(update_with_sleep)
       ++tlocal.at(rec::Counter::servFails);
       ++tlocal.at(rec::Counter::nxDomains);
       tlocal.at(rec::DoubleWAvgCounter::avgLatencyUsec).add(2.2);
-      if (random() % 10000 == 0) {
+      if (dns_random(10000) == 0) {
         tlocal.updateSnap();
       }
       struct timespec interval
       {
-        0, random() % 999
+        0, dns_random(40 * 1000 * 1000)
       };
       nanosleep(&interval, nullptr);
     }
@@ -132,7 +133,7 @@ BOOST_AUTO_TEST_CASE(update_with_sleep)
       BOOST_CHECK(avg == 0.0 || (avg >= 1.1 && avg <= 2.2));
       struct timespec interval
       {
-        0, random() % 10000
+        0, dns_random(80 * 1000 * 1000)
       };
       nanosleep(&interval, nullptr);
     }
index 996113c5c84fe72fb9953f1c4cbd65401f2f39d5..e0494fe18ac994f28b07fee883fd941c7b0525fb 100644 (file)
@@ -5,7 +5,6 @@
 #include "config.h"
 #endif
 #include <boost/test/unit_test.hpp>
-#include "arguments.hh"
 #include "dnswriter.hh"
 #include "dnsrecords.hh"
 #include "dns_random.hh"
@@ -25,9 +24,6 @@ BOOST_AUTO_TEST_CASE(test_recPacketCacheSimple)
   uint32_t ttd = 3600;
   BOOST_CHECK_EQUAL(rpc.size(), 0U);
 
-  ::arg().set("rng") = "auto";
-  ::arg().set("entropy-source") = "/dev/urandom";
-
   DNSName qname("www.powerdns.com");
   vector<uint8_t> packet;
   DNSPacketWriter pw(packet, qname, QType::A);
@@ -94,9 +90,6 @@ BOOST_AUTO_TEST_CASE(test_recPacketCacheSimplePost2038)
   uint32_t ttd = 3600;
   BOOST_CHECK_EQUAL(rpc.size(), 0U);
 
-  ::arg().set("rng") = "auto";
-  ::arg().set("entropy-source") = "/dev/urandom";
-
   DNSName qname("www.powerdns.com");
   vector<uint8_t> packet;
   DNSPacketWriter pw(packet, qname, QType::A);
@@ -178,9 +171,6 @@ BOOST_AUTO_TEST_CASE(test_recPacketCache_Tags)
   uint32_t ttd = 3600;
   BOOST_CHECK_EQUAL(rpc.size(), 0U);
 
-  ::arg().set("rng") = "auto";
-  ::arg().set("entropy-source") = "/dev/urandom";
-
   DNSName qname("www.powerdns.com");
   vector<uint8_t> packet;
   DNSPacketWriter pw(packet, qname, QType::A);
@@ -295,9 +285,6 @@ BOOST_AUTO_TEST_CASE(test_recPacketCache_TCP)
   uint32_t ttd = 3600;
   BOOST_CHECK_EQUAL(rpc.size(), 0U);
 
-  ::arg().set("rng") = "auto";
-  ::arg().set("entropy-source") = "/dev/urandom";
-
   DNSName qname("www.powerdns.com");
   vector<uint8_t> packet;
   DNSPacketWriter pw(packet, qname, QType::A);
index 6d9b09374625defd1a635c6986e694bc3255c02e..b0c3e2613d8bf5d4054f5846099c8de64d558cc6 100644 (file)
@@ -31,6 +31,8 @@
 #include <iomanip>
 #include "logger.hh"
 #include "logging.hh"
+#include "arguments.hh"
+#include "dns_random.hh"
 
 static std::string s_timestampFormat = "%s";
 
@@ -76,6 +78,10 @@ static void loggerBackend(const Logging::Entry& entry)
 
 static bool init_unit_test()
 {
+  ::arg().set("rng") = "auto";
+  ::arg().set("entropy-source") = "/dev/urandom";
+  // Force init while w're still unthreaded
+  dns_random_uint16();
   g_slog = Logging::Logger::create(loggerBackend);
   reportAllTypes();
   return true;