]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/test-lock_hh.cc
Merge pull request #8345 from omoerbeek/format-code-script
[thirdparty/pdns.git] / pdns / test-lock_hh.cc
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
10 using namespace boost;
11
12 BOOST_AUTO_TEST_SUITE(test_lock_hh)
13
14 static std::vector<std::unique_ptr<pthread_rwlock_t> > g_locks;
15
16 static void lthread()
17 {
18 std::vector<ReadLock> rlocks;
19 for(auto& pp : g_locks)
20 rlocks.emplace_back(&*pp);
21
22 }
23
24 BOOST_AUTO_TEST_CASE(test_pdns_lock)
25 {
26 for(unsigned int n=0; n < 1000; ++n) {
27 auto p = make_unique<pthread_rwlock_t>();
28 pthread_rwlock_init(p.get(), 0);
29 g_locks.emplace_back(std::move(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
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);
55
56 wlocks.clear();
57 TryReadLock trl2(&*g_locks[0]);
58 BOOST_CHECK(trl2.gotIt());
59
60
61 }
62
63 BOOST_AUTO_TEST_SUITE_END()