From 43bce230e993bc0d3a2c09471c84b980bb97049f Mon Sep 17 00:00:00 2001 From: bert hubert Date: Fri, 7 Apr 2017 15:09:01 +0200 Subject: [PATCH] add tests for lock.hh --- pdns/Makefile.am | 1 + pdns/test-lock_hh.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 pdns/test-lock_hh.cc diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 50ebea912..48acdbe8f 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -1156,6 +1156,7 @@ testrunner_SOURCES = \ test-dnsparser_hh.cc \ test-dnsrecords_cc.cc \ test-iputils_hh.cc \ + test-lock_hh.cc \ test-md5_hh.cc \ test-misc_hh.cc \ test-nameserver_cc.cc \ diff --git a/pdns/test-lock_hh.cc b/pdns/test-lock_hh.cc new file mode 100644 index 000000000..707dcc183 --- /dev/null +++ b/pdns/test-lock_hh.cc @@ -0,0 +1,54 @@ +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_NO_MAIN +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include +#include "lock.hh" +#include + +using namespace boost; + +BOOST_AUTO_TEST_SUITE(test_lock_hh) + +static std::vector > g_locks; + +static void lthread() +{ + std::vector rlocks; + for(auto& pp : g_locks) + rlocks.emplace_back(&*pp); + +} + +BOOST_AUTO_TEST_CASE(test_pdns_lock) +{ + for(unsigned int n=0; n < 1000; ++n) { + auto p = new pthread_rwlock_t; + pthread_rwlock_init(p, 0); + g_locks.emplace_back(p); + } + + std::vector rlocks; + for(auto& pp : g_locks) + rlocks.emplace_back(&*pp); + + std::thread thr(lthread); + thr.join(); + rlocks.clear(); + + std::vector wlocks; + for(auto& pp : g_locks) + wlocks.emplace_back(&*pp); + + TryReadLock trl(&*g_locks[0]); + BOOST_CHECK(!trl.gotIt()); + + wlocks.clear(); + TryReadLock trl2(&*g_locks[0]); + BOOST_CHECK(trl2.gotIt()); + + +} + +BOOST_AUTO_TEST_SUITE_END() -- 2.40.0