]>
Commit | Line | Data |
---|---|---|
1c2d079d | 1 | #ifndef BOOST_TEST_DYN_LINK |
43bce230 | 2 | #define BOOST_TEST_DYN_LINK |
1c2d079d FM |
3 | #endif |
4 | ||
43bce230 | 5 | #define BOOST_TEST_NO_MAIN |
6 | #ifdef HAVE_CONFIG_H | |
7 | #include "config.h" | |
8 | #endif | |
9 | #include <boost/test/unit_test.hpp> | |
43bce230 | 10 | #include <thread> |
11 | ||
0898164e RG |
12 | #include "lock.hh" |
13 | #include "pdnsexception.hh" | |
14 | ||
43bce230 | 15 | using namespace boost; |
16 | ||
17 | BOOST_AUTO_TEST_SUITE(test_lock_hh) | |
18 | ||
9092419e | 19 | static std::vector<ReadWriteLock> g_locks(1000); |
43bce230 | 20 | |
21 | static void lthread() | |
22 | { | |
23 | std::vector<ReadLock> rlocks; | |
0898164e | 24 | for(auto& pp : g_locks) { |
9092419e | 25 | rlocks.emplace_back(pp); |
0898164e | 26 | } |
43bce230 | 27 | } |
28 | ||
29 | BOOST_AUTO_TEST_CASE(test_pdns_lock) | |
30 | { | |
43bce230 | 31 | std::vector<ReadLock> rlocks; |
32 | for(auto& pp : g_locks) | |
9092419e | 33 | rlocks.emplace_back(pp); |
43bce230 | 34 | |
35 | std::thread thr(lthread); | |
36 | thr.join(); | |
37 | rlocks.clear(); | |
38 | ||
39 | std::vector<WriteLock> wlocks; | |
40 | for(auto& pp : g_locks) | |
9092419e | 41 | wlocks.emplace_back(pp); |
43bce230 | 42 | |
30f29724 PD |
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 { | |
9092419e | 47 | TryReadLock trl(g_locks.at(0)); |
30f29724 PD |
48 | gotit = trl.gotIt(); |
49 | } | |
50 | catch(const PDNSException &e) { | |
51 | gotit = false; | |
52 | } | |
53 | BOOST_CHECK(!gotit); | |
43bce230 | 54 | |
55 | wlocks.clear(); | |
fe37ad98 OM |
56 | |
57 | { | |
9092419e | 58 | TryReadLock trl2(g_locks.at(0)); |
fe37ad98 OM |
59 | BOOST_CHECK(trl2.gotIt()); |
60 | } | |
61 | ||
9092419e | 62 | g_locks.clear(); |
43bce230 | 63 | } |
64 | ||
65 | BOOST_AUTO_TEST_SUITE_END() |