]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/test-lock_hh.cc
Merge pull request #8223 from PowerDNS/omoerbeek-patch-1
[thirdparty/pdns.git] / pdns / test-lock_hh.cc
CommitLineData
43bce230 1#define BOOST_TEST_DYN_LINK
2#define BOOST_TEST_NO_MAIN
3#ifdef HAVE_CONFIG_H
4#include "config.h"
5#endif
6#include <boost/test/unit_test.hpp>
7#include "lock.hh"
8#include <thread>
9
10using namespace boost;
11
12BOOST_AUTO_TEST_SUITE(test_lock_hh)
13
14static std::vector<std::unique_ptr<pthread_rwlock_t> > g_locks;
15
16static void lthread()
17{
18 std::vector<ReadLock> rlocks;
19 for(auto& pp : g_locks)
20 rlocks.emplace_back(&*pp);
21
22}
23
24BOOST_AUTO_TEST_CASE(test_pdns_lock)
25{
26 for(unsigned int n=0; n < 1000; ++n) {
27 auto p = new pthread_rwlock_t;
28 pthread_rwlock_init(p, 0);
29 g_locks.emplace_back(p);
30 }
31
32 std::vector<ReadLock> rlocks;
33 for(auto& pp : g_locks)
34 rlocks.emplace_back(&*pp);
35
36 std::thread thr(lthread);
37 thr.join();
38 rlocks.clear();
39
40 std::vector<WriteLock> wlocks;
41 for(auto& pp : g_locks)
42 wlocks.emplace_back(&*pp);
43
30f29724
PD
44 // on macOS, this TryReadLock throws (EDEADLK) instead of simply failing
45 // so we catch the exception and consider that success for this test
46 bool gotit = false;
47 try {
48 TryReadLock trl(&*g_locks[0]);
49 gotit = trl.gotIt();
50 }
51 catch(const PDNSException &e) {
52 gotit = false;
53 }
54 BOOST_CHECK(!gotit);
43bce230 55
56 wlocks.clear();
57 TryReadLock trl2(&*g_locks[0]);
58 BOOST_CHECK(trl2.gotIt());
59
60
61}
62
63BOOST_AUTO_TEST_SUITE_END()