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