]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/test-statbag_cc.cc
Meson: Add systemd feature support for service files
[thirdparty/pdns.git] / pdns / test-statbag_cc.cc
1 #ifndef BOOST_TEST_DYN_LINK
2 #define BOOST_TEST_DYN_LINK
3 #endif
4
5 #define BOOST_TEST_NO_MAIN
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 #include <boost/test/unit_test.hpp>
11 #include <boost/assign/list_of.hpp>
12
13 #include <stdint.h>
14 #include <thread>
15 #include "misc.hh"
16 #include "dns.hh"
17 #include "statbag.hh"
18
19 using std::string;
20
21 static void threadMangler(AtomicCounter* ac)
22 {
23 for(unsigned int n=0; n < 1000000; ++n)
24 (*ac)++;
25 }
26
27 static void threadMangler2(StatBag* S)
28 {
29 for(unsigned int n=0; n < 1000000; ++n)
30 S->inc("c");
31 }
32
33
34
35 BOOST_AUTO_TEST_SUITE(test_misc_hh)
36
37 BOOST_AUTO_TEST_CASE(test_StatBagBasic) {
38 StatBag s;
39 s.declare("a", "description");
40 s.declare("b", "description");
41 s.declare("c", "description");
42 s.inc("a");
43 BOOST_CHECK_EQUAL(s.read("a"), 1UL);
44
45 unsigned long n;
46 for(n=0; n < 1000000; ++n)
47 s.inc("b");
48
49 BOOST_CHECK_EQUAL(s.read("b"), n);
50
51 AtomicCounter* ac = s.getPointer("a");
52 for(n=0; n < 1000000; ++n)
53 (*ac)++;
54
55 BOOST_CHECK_EQUAL(s.read("a"), n+1);
56
57 AtomicCounter* acc = s.getPointer("c");
58 std::vector<std::thread> manglers;
59 for (int i=0; i < 4; ++i) {
60 manglers.push_back(std::thread(threadMangler, acc));
61 }
62
63 for (auto& t : manglers) {
64 t.join();
65 }
66 manglers.clear();
67
68 BOOST_CHECK_EQUAL(s.read("c"), 4000000U);
69
70 s.set("c", 0);
71
72 for (int i=0; i < 4; ++i) {
73 manglers.push_back(std::thread(threadMangler2, &s));
74 }
75
76 for (auto& t : manglers) {
77 t.join();
78 }
79 manglers.clear();
80
81 BOOST_CHECK_EQUAL(s.read("c"), 4000000U);
82
83
84 s.set("c", 1ULL<<31);
85 BOOST_CHECK_EQUAL(s.read("c"), (1ULL<<31) );
86 s.inc("c");
87 BOOST_CHECK_EQUAL(s.read("c"), (1ULL<<31) +1 );
88
89 #ifdef UINTPTR_MAX
90 #if UINTPTR_MAX > 0xffffffffULL
91 BOOST_CHECK_EQUAL(sizeof(AtomicCounterInner), 8U);
92 s.set("c", 1ULL<<33);
93 BOOST_CHECK_EQUAL(s.read("c"), (1ULL<<33) );
94 s.inc("c");
95 BOOST_CHECK_EQUAL(s.read("c"), (1ULL<<33) +1 );
96
97 s.set("c", ~0ULL);
98 BOOST_CHECK_EQUAL(s.read("c"), 0xffffffffffffffffULL );
99 s.inc("c");
100 BOOST_CHECK_EQUAL(s.read("c"), 0UL );
101 #else
102 BOOST_CHECK_EQUAL(sizeof(AtomicCounterInner), 4U);
103 BOOST_CHECK_EQUAL(~0UL, 0xffffffffUL);
104 s.set("c", ~0UL);
105 BOOST_CHECK_EQUAL(s.read("c"), 0xffffffffUL );
106 s.inc("c");
107 BOOST_CHECK_EQUAL(s.read("c"), 0UL );
108 #endif
109 #endif
110 }
111
112
113 BOOST_AUTO_TEST_SUITE_END()