From: Otto Moerbeek Date: Mon, 19 Dec 2022 08:08:14 +0000 (+0100) Subject: Some systems have a low-resolution nanosleep(2), calling it will X-Git-Tag: dnsdist-1.8.0-rc1~154^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=28bc59591441c18e463ffde67aa4e0bddc28132d;p=thirdparty%2Fpdns.git Some systems have a low-resolution nanosleep(2), calling it will 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. --- diff --git a/pdns/recursordist/test-rec-tcounters_cc.cc b/pdns/recursordist/test-rec-tcounters_cc.cc index a86cab0d34..bdeede0699 100644 --- a/pdns/recursordist/test-rec-tcounters_cc.cc +++ b/pdns/recursordist/test-rec-tcounters_cc.cc @@ -9,6 +9,7 @@ #include #include +#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 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); } diff --git a/pdns/recursordist/test-recpacketcache_cc.cc b/pdns/recursordist/test-recpacketcache_cc.cc index 996113c5c8..e0494fe18a 100644 --- a/pdns/recursordist/test-recpacketcache_cc.cc +++ b/pdns/recursordist/test-recpacketcache_cc.cc @@ -5,7 +5,6 @@ #include "config.h" #endif #include -#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 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 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 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 packet; DNSPacketWriter pw(packet, qname, QType::A); diff --git a/pdns/recursordist/testrunner.cc b/pdns/recursordist/testrunner.cc index 6d9b093746..b0c3e2613d 100644 --- a/pdns/recursordist/testrunner.cc +++ b/pdns/recursordist/testrunner.cc @@ -31,6 +31,8 @@ #include #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;