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