]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/test-distributor_hh.cc
Merge pull request #14032 from rgacogne/ddist-192-changelog-secpoll
[thirdparty/pdns.git] / pdns / test-distributor_hh.cc
1 #ifndef BOOST_TEST_DYN_LINK
2 #define BOOST_TEST_DYN_LINK
3 #endif
4
5 #define BOOST_TEST_NO_MAIN
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <boost/test/unit_test.hpp>
12 #include "distributor.hh"
13 #include "dnspacket.hh"
14 #include "namespaces.hh"
15
16 bool g_doGssTSIG = false;
17
18 BOOST_AUTO_TEST_SUITE(test_distributor_hh)
19
20 struct Question
21 {
22 int q;
23 DTime d_dt;
24 DNSName qdomain;
25 QType qtype;
26 std::unique_ptr<DNSPacket> replyPacket()
27 {
28 return make_unique<DNSPacket>(false);
29 }
30 void cleanupGSS(int){}
31 };
32
33 struct Backend
34 {
35 std::unique_ptr<DNSPacket> question(Question&)
36 {
37 return make_unique<DNSPacket>(true);
38 }
39 };
40
41 static std::atomic<int> g_receivedAnswers;
42 static void report(std::unique_ptr<DNSPacket>& /* A */, int /* B */)
43 {
44 g_receivedAnswers++;
45 }
46
47 BOOST_AUTO_TEST_CASE(test_distributor_basic) {
48 ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0";
49 ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000";
50 ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
51 S.declare("servfail-packets","Number of times a server-failed packet was sent out");
52 S.declare("timedout-packets", "timedout-packets");
53
54 auto d=Distributor<DNSPacket, Question, Backend>::Create(2);
55
56 int n;
57 for(n=0; n < 100; ++n) {
58 Question q;
59 q.d_dt.set();
60 d->question(q, report);
61 }
62 sleep(1);
63 BOOST_CHECK_EQUAL(n, g_receivedAnswers);
64 };
65
66 struct BackendSlow
67 {
68 std::unique_ptr<DNSPacket> question(Question&)
69 {
70 sleep(1);
71 return make_unique<DNSPacket>(true);
72 }
73 };
74
75 static std::atomic<int> g_receivedAnswers1;
76 static void report1(std::unique_ptr<DNSPacket>& /* A */, int /* B */)
77 {
78 g_receivedAnswers1++;
79 }
80
81 BOOST_AUTO_TEST_CASE(test_distributor_queue) {
82 ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0";
83 ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="1000";
84 ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
85 S.declare("servfail-packets","Number of times a server-failed packet was sent out");
86 S.declare("timedout-packets", "timedout-packets");
87
88 auto d=Distributor<DNSPacket, Question, BackendSlow>::Create(2);
89
90 BOOST_CHECK_EXCEPTION( {
91 int n;
92 // bound should be higher than max-queue-length
93 for(n=0; n < 2000; ++n) {
94 Question q;
95 q.d_dt.set();
96 d->question(q, report1);
97 }
98 }, DistributorFatal, [](DistributorFatal) { return true; });
99 };
100
101 struct BackendDies
102 {
103 BackendDies()
104 {
105 d_ourcount=s_count++;
106 }
107 ~BackendDies()
108 {
109 }
110 std::unique_ptr<DNSPacket> question(Question& /* q */)
111 {
112 // cout<<"Q: "<<q->qdomain<<endl;
113 if(!d_ourcount && ++d_count == 10) {
114 // cerr<<"Going.. down!"<<endl;
115 throw runtime_error("kill");
116 }
117 return make_unique<DNSPacket>(true);
118 }
119 static std::atomic<int> s_count;
120 int d_count{0};
121 int d_ourcount;
122 };
123
124 std::atomic<int> BackendDies::s_count;
125
126 std::atomic<int> g_receivedAnswers2;
127
128 static void report2(std::unique_ptr<DNSPacket>& /* A */, int /* B */)
129 {
130 g_receivedAnswers2++;
131 }
132
133
134 BOOST_AUTO_TEST_CASE(test_distributor_dies) {
135 ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0";
136 ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000";
137 ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
138 S.declare("servfail-packets","Number of times a server-failed packet was sent out");
139 S.declare("timedout-packets", "timedout-packets");
140
141 auto d=Distributor<DNSPacket, Question, BackendDies>::Create(10);
142
143 try {
144 for(int n=0; n < 100; ++n) {
145 Question q;
146 q.d_dt.set();
147 q.qdomain=DNSName(std::to_string(n));
148 q.qtype = QType(QType::A);
149 d->question(q, report2);
150 }
151
152 sleep(1);
153 cerr<<"Queued: "<<d->getQueueSize()<<endl;
154 cerr<<"Received: "<<g_receivedAnswers2<<endl;
155 }
156 catch(std::exception& e) {
157 cerr<<e.what()<<endl;
158 }
159 catch(PDNSException &pe) {
160 cerr<<pe.reason<<endl;
161 }
162 };
163
164
165
166 BOOST_AUTO_TEST_SUITE_END();