]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/test-distributor_hh.cc
Add comment on upper bound
[thirdparty/pdns.git] / pdns / test-distributor_hh.cc
CommitLineData
491d03d7 1#define BOOST_TEST_DYN_LINK
2#define BOOST_TEST_NO_MAIN
3#ifdef HAVE_CONFIG_H
4#include "config.h"
5#endif
6#include <stdlib.h>
7#include <unistd.h>
8#include <boost/test/unit_test.hpp>
9#include "distributor.hh"
10#include "dnspacket.hh"
11#include "namespaces.hh"
12
13BOOST_AUTO_TEST_SUITE(test_distributor_hh)
14
15struct Question
16{
17 int q;
18 DTime d_dt;
cb47aa7d 19 DNSName qdomain;
491d03d7 20 DNSPacket* replyPacket()
21 {
27c0050c 22 return new DNSPacket(false);
491d03d7 23 }
24};
25
26struct Backend
27{
28 DNSPacket* question(Question*)
29 {
27c0050c 30 return new DNSPacket(true);
491d03d7 31 }
32};
33
34static std::atomic<int> g_receivedAnswers;
35static void report(DNSPacket* A)
36{
37 delete A;
38 g_receivedAnswers++;
39}
40
41BOOST_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");
47
48 auto d=Distributor<DNSPacket, Question, Backend>::Create(2);
49
50 int n;
51 for(n=0; n < 100; ++n) {
52 auto q = new Question();
53 q->d_dt.set();
54 d->question(q, report);
55 }
56 sleep(1);
57 BOOST_CHECK_EQUAL(n, g_receivedAnswers);
58};
59
60struct BackendSlow
61{
62 DNSPacket* question(Question*)
63 {
64 sleep(1);
27c0050c 65 return new DNSPacket(true);
491d03d7 66 }
67};
68
69
70BOOST_AUTO_TEST_CASE(test_distributor_queue) {
f556fbeb
OM
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");
76
491d03d7 77 auto d=Distributor<DNSPacket, Question, BackendSlow>::Create(2);
78
79 BOOST_CHECK_EXCEPTION( {
80 int n;
618d20cd 81 // bound should be higher than max-queue-length
f556fbeb 82 for(n=0; n < 2000; ++n) {
491d03d7 83 auto q = new Question();
84 q->d_dt.set();
85 d->question(q, report);
86 }
87 }, DistributorFatal, [](DistributorFatal) { return true; });
88};
89
90struct BackendDies
91{
92 BackendDies()
93 {
94 d_ourcount=s_count++;
95 }
96 ~BackendDies()
97 {
98 }
99 DNSPacket* question(Question* q)
100 {
101 // cout<<"Q: "<<q->qdomain<<endl;
102 if(!d_ourcount && ++d_count == 10) {
103 // cerr<<"Going.. down!"<<endl;
104 throw runtime_error("kill");
105 }
27c0050c 106 return new DNSPacket(true);
491d03d7 107 }
108 static std::atomic<int> s_count;
109 int d_count{0};
110 int d_ourcount;
111};
112
113std::atomic<int> BackendDies::s_count;
114
115std::atomic<int> g_receivedAnswers2;
116
117static void report2(DNSPacket* A)
118{
119 delete A;
120 g_receivedAnswers2++;
121}
122
123
124BOOST_AUTO_TEST_CASE(test_distributor_dies) {
f556fbeb
OM
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");
130
491d03d7 131 auto d=Distributor<DNSPacket, Question, BackendDies>::Create(10);
132 sleep(1);
133 g_receivedAnswers=0;
134
135 try {
136 for(int n=0; n < 100; ++n) {
137 auto q = new Question();
138 q->d_dt.set();
eaedd091 139 q->qdomain=DNSName(std::to_string(n));
491d03d7 140 d->question(q, report2);
141 }
142
143 sleep(1);
144 cerr<<"Queued: "<<d->getQueueSize()<<endl;
145 cerr<<"Received: "<<g_receivedAnswers2<<endl;
146 }
147 catch(std::exception& e) {
148 cerr<<e.what()<<endl;
149 }
150 catch(PDNSException &pe) {
151 cerr<<pe.reason<<endl;
152 }
153};
154
155
156
157BOOST_AUTO_TEST_SUITE_END();