]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/test-distributor_hh.cc
rec: mention rust compiler in compiling docs
[thirdparty/pdns.git] / pdns / test-distributor_hh.cc
CommitLineData
1c2d079d 1#ifndef BOOST_TEST_DYN_LINK
491d03d7 2#define BOOST_TEST_DYN_LINK
1c2d079d
FM
3#endif
4
491d03d7 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"
c113acc3
OM
14#include "namespaces.hh"
15
16bool g_doGssTSIG = false;
491d03d7 17
18BOOST_AUTO_TEST_SUITE(test_distributor_hh)
19
20struct Question
21{
22 int q;
23 DTime d_dt;
cb47aa7d 24 DNSName qdomain;
5c293d11 25 QType qtype;
c2826d2e 26 std::unique_ptr<DNSPacket> replyPacket()
491d03d7 27 {
c2826d2e 28 return make_unique<DNSPacket>(false);
491d03d7 29 }
dd0ef5ed 30 void cleanupGSS(int){}
491d03d7 31};
32
33struct Backend
34{
c2826d2e 35 std::unique_ptr<DNSPacket> question(Question&)
491d03d7 36 {
c2826d2e 37 return make_unique<DNSPacket>(true);
491d03d7 38 }
39};
40
41static std::atomic<int> g_receivedAnswers;
d73de874 42static void report(std::unique_ptr<DNSPacket>& /* A */, int /* B */)
491d03d7 43{
491d03d7 44 g_receivedAnswers++;
45}
46
47BOOST_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) {
c2826d2e 58 Question q;
bb85386d 59 q.d_dt.set();
491d03d7 60 d->question(q, report);
61 }
62 sleep(1);
63 BOOST_CHECK_EQUAL(n, g_receivedAnswers);
64};
65
66struct BackendSlow
67{
c2826d2e 68 std::unique_ptr<DNSPacket> question(Question&)
491d03d7 69 {
70 sleep(1);
c2826d2e 71 return make_unique<DNSPacket>(true);
491d03d7 72 }
73};
74
48215c19 75static std::atomic<int> g_receivedAnswers1;
d73de874 76static void report1(std::unique_ptr<DNSPacket>& /* A */, int /* B */)
48215c19 77{
48215c19
OM
78 g_receivedAnswers1++;
79}
491d03d7 80
81BOOST_AUTO_TEST_CASE(test_distributor_queue) {
f556fbeb
OM
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
491d03d7 88 auto d=Distributor<DNSPacket, Question, BackendSlow>::Create(2);
89
90 BOOST_CHECK_EXCEPTION( {
91 int n;
618d20cd 92 // bound should be higher than max-queue-length
f556fbeb 93 for(n=0; n < 2000; ++n) {
c2826d2e 94 Question q;
bb85386d 95 q.d_dt.set();
48215c19 96 d->question(q, report1);
491d03d7 97 }
98 }, DistributorFatal, [](DistributorFatal) { return true; });
99};
100
101struct BackendDies
102{
103 BackendDies()
104 {
105 d_ourcount=s_count++;
106 }
107 ~BackendDies()
108 {
109 }
d73de874 110 std::unique_ptr<DNSPacket> question(Question& /* q */)
491d03d7 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 }
c2826d2e 117 return make_unique<DNSPacket>(true);
491d03d7 118 }
119 static std::atomic<int> s_count;
120 int d_count{0};
121 int d_ourcount;
122};
123
124std::atomic<int> BackendDies::s_count;
125
126std::atomic<int> g_receivedAnswers2;
127
d73de874 128static void report2(std::unique_ptr<DNSPacket>& /* A */, int /* B */)
491d03d7 129{
491d03d7 130 g_receivedAnswers2++;
131}
132
133
134BOOST_AUTO_TEST_CASE(test_distributor_dies) {
f556fbeb
OM
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
491d03d7 141 auto d=Distributor<DNSPacket, Question, BackendDies>::Create(10);
491d03d7 142
143 try {
144 for(int n=0; n < 100; ++n) {
c2826d2e 145 Question q;
bb85386d 146 q.d_dt.set();
c2826d2e
RG
147 q.qdomain=DNSName(std::to_string(n));
148 q.qtype = QType(QType::A);
491d03d7 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
166BOOST_AUTO_TEST_SUITE_END();