]>
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);
70 BOOST_AUTO_TEST_CASE(test_distributor_queue
) {
71 ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0";
72 ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="1000";
73 ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
74 S
.declare("servfail-packets","Number of times a server-failed packet was sent out");
75 S
.declare("timedout-packets", "timedout-packets");
77 auto d
=Distributor
<DNSPacket
, Question
, BackendSlow
>::Create(2);
79 BOOST_CHECK_EXCEPTION( {
81 // bound should be higher than max-queue-length
82 for(n
=0; n
< 2000; ++n
) {
83 auto q
= new Question();
85 d
->question(q
, report
);
87 }, DistributorFatal
, [](DistributorFatal
) { return true; });
99 DNSPacket
* question(Question
* q
)
101 // cout<<"Q: "<<q->qdomain<<endl;
102 if(!d_ourcount
&& ++d_count
== 10) {
103 // cerr<<"Going.. down!"<<endl;
104 throw runtime_error("kill");
106 return new DNSPacket(true);
108 static std::atomic
<int> s_count
;
113 std::atomic
<int> BackendDies::s_count
;
115 std::atomic
<int> g_receivedAnswers2
;
117 static void report2(DNSPacket
* A
)
120 g_receivedAnswers2
++;
124 BOOST_AUTO_TEST_CASE(test_distributor_dies
) {
125 ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0";
126 ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000";
127 ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
128 S
.declare("servfail-packets","Number of times a server-failed packet was sent out");
129 S
.declare("timedout-packets", "timedout-packets");
131 auto d
=Distributor
<DNSPacket
, Question
, BackendDies
>::Create(10);
136 for(int n
=0; n
< 100; ++n
) {
137 auto q
= new Question();
139 q
->qdomain
=DNSName(std::to_string(n
));
140 d
->question(q
, report2
);
144 cerr
<<"Queued: "<<d
->getQueueSize()<<endl
;
145 cerr
<<"Received: "<<g_receivedAnswers2
<<endl
;
147 catch(std::exception
& e
) {
148 cerr
<<e
.what()<<endl
;
150 catch(PDNSException
&pe
) {
151 cerr
<<pe
.reason
<<endl
;
157 BOOST_AUTO_TEST_SUITE_END();