]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/test-distributor_hh.cc
1 #define BOOST_TEST_DYN_LINK
2 #define BOOST_TEST_NO_MAIN
8 #include <boost/test/unit_test.hpp>
9 #include "distributor.hh"
10 #include "dnspacket.hh"
11 #include "namespaces.hh"
13 BOOST_AUTO_TEST_SUITE(test_distributor_hh
)
20 DNSPacket
* replyPacket()
22 return new DNSPacket(false);
28 DNSPacket
* question(Question
*)
30 return new DNSPacket(true);
34 static std::atomic
<int> g_receivedAnswers
;
35 static void report(DNSPacket
* A
)
41 BOOST_AUTO_TEST_CASE(test_distributor_basic
) {
42 ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0";
43 ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000";
44 ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
45 S
.declare("servfail-packets","Number of times a server-failed packet was sent out");
46 S
.declare("timedout-packets", "timedout-packets");
48 auto d
=Distributor
<DNSPacket
, Question
, Backend
>::Create(2);
51 for(n
=0; n
< 100; ++n
) {
52 auto q
= new Question();
54 d
->question(q
, report
);
57 BOOST_CHECK_EQUAL(n
, g_receivedAnswers
);
62 DNSPacket
* question(Question
*)
65 return new DNSPacket(true);
69 static std::atomic
<int> g_receivedAnswers1
;
70 static void report1(DNSPacket
* A
)
76 BOOST_AUTO_TEST_CASE(test_distributor_queue
) {
77 ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0";
78 ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="1000";
79 ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
80 S
.declare("servfail-packets","Number of times a server-failed packet was sent out");
81 S
.declare("timedout-packets", "timedout-packets");
83 auto d
=Distributor
<DNSPacket
, Question
, BackendSlow
>::Create(2);
85 BOOST_CHECK_EXCEPTION( {
87 // bound should be higher than max-queue-length
88 for(n
=0; n
< 2000; ++n
) {
89 auto q
= new Question();
91 d
->question(q
, report1
);
93 }, DistributorFatal
, [](DistributorFatal
) { return true; });
100 d_ourcount
=s_count
++;
105 DNSPacket
* question(Question
* q
)
107 // cout<<"Q: "<<q->qdomain<<endl;
108 if(!d_ourcount
&& ++d_count
== 10) {
109 // cerr<<"Going.. down!"<<endl;
110 throw runtime_error("kill");
112 return new DNSPacket(true);
114 static std::atomic
<int> s_count
;
119 std::atomic
<int> BackendDies::s_count
;
121 std::atomic
<int> g_receivedAnswers2
;
123 static void report2(DNSPacket
* A
)
126 g_receivedAnswers2
++;
130 BOOST_AUTO_TEST_CASE(test_distributor_dies
) {
131 ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0";
132 ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000";
133 ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
134 S
.declare("servfail-packets","Number of times a server-failed packet was sent out");
135 S
.declare("timedout-packets", "timedout-packets");
137 auto d
=Distributor
<DNSPacket
, Question
, BackendDies
>::Create(10);
140 for(int n
=0; n
< 100; ++n
) {
141 auto q
= new Question();
143 q
->qdomain
=DNSName(std::to_string(n
));
144 d
->question(q
, report2
);
148 cerr
<<"Queued: "<<d
->getQueueSize()<<endl
;
149 cerr
<<"Received: "<<g_receivedAnswers2
<<endl
;
151 catch(std::exception
& e
) {
152 cerr
<<e
.what()<<endl
;
154 catch(PDNSException
&pe
) {
155 cerr
<<pe
.reason
<<endl
;
161 BOOST_AUTO_TEST_SUITE_END();