]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/test-distributor_hh.cc
Merge branch 'master' of github.com:PowerDNS/pdns
[thirdparty/pdns.git] / pdns / test-distributor_hh.cc
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
13 BOOST_AUTO_TEST_SUITE(test_distributor_hh)
14
15 struct Question
16 {
17 int q;
18 DTime d_dt;
19 string qdomain;
20 DNSPacket* replyPacket()
21 {
22 return new DNSPacket();
23 }
24 };
25
26 struct Backend
27 {
28 DNSPacket* question(Question*)
29 {
30 return new DNSPacket();
31 }
32 };
33
34 static std::atomic<int> g_receivedAnswers;
35 static void report(DNSPacket* A)
36 {
37 delete A;
38 g_receivedAnswers++;
39 }
40
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");
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
60 struct BackendSlow
61 {
62 DNSPacket* question(Question*)
63 {
64 sleep(1);
65 return new DNSPacket();
66 }
67 };
68
69
70 BOOST_AUTO_TEST_CASE(test_distributor_queue) {
71 auto d=Distributor<DNSPacket, Question, BackendSlow>::Create(2);
72
73 BOOST_CHECK_EXCEPTION( {
74 int n;
75 for(n=0; n < 6000; ++n) {
76 auto q = new Question();
77 q->d_dt.set();
78 d->question(q, report);
79 }
80 }, DistributorFatal, [](DistributorFatal) { return true; });
81 };
82
83 struct BackendDies
84 {
85 BackendDies()
86 {
87 d_ourcount=s_count++;
88 }
89 ~BackendDies()
90 {
91 }
92 DNSPacket* question(Question* q)
93 {
94 // cout<<"Q: "<<q->qdomain<<endl;
95 if(!d_ourcount && ++d_count == 10) {
96 // cerr<<"Going.. down!"<<endl;
97 throw runtime_error("kill");
98 }
99 return new DNSPacket();
100 }
101 static std::atomic<int> s_count;
102 int d_count{0};
103 int d_ourcount;
104 };
105
106 std::atomic<int> BackendDies::s_count;
107
108 std::atomic<int> g_receivedAnswers2;
109
110 static void report2(DNSPacket* A)
111 {
112 delete A;
113 g_receivedAnswers2++;
114 }
115
116
117 BOOST_AUTO_TEST_CASE(test_distributor_dies) {
118 auto d=Distributor<DNSPacket, Question, BackendDies>::Create(10);
119 sleep(1);
120 g_receivedAnswers=0;
121
122 try {
123 for(int n=0; n < 100; ++n) {
124 auto q = new Question();
125 q->d_dt.set();
126 q->qdomain=std::to_string(n);
127 d->question(q, report2);
128 }
129
130 sleep(1);
131 cerr<<"Queued: "<<d->getQueueSize()<<endl;
132 cerr<<"Received: "<<g_receivedAnswers2<<endl;
133 }
134 catch(std::exception& e) {
135 cerr<<e.what()<<endl;
136 }
137 catch(PDNSException &pe) {
138 cerr<<pe.reason<<endl;
139 }
140 };
141
142
143
144 BOOST_AUTO_TEST_SUITE_END();