]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | BOOST_AUTO_TEST_SUITE(test_distributor_hh) | |
14 | ||
15 | struct 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 | ||
26 | struct Backend | |
27 | { | |
28 | DNSPacket* question(Question*) | |
29 | { | |
27c0050c | 30 | return new DNSPacket(true); |
491d03d7 | 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); | |
27c0050c | 65 | return new DNSPacket(true); |
491d03d7 | 66 | } |
67 | }; | |
68 | ||
48215c19 OM |
69 | static std::atomic<int> g_receivedAnswers1; |
70 | static void report1(DNSPacket* A) | |
71 | { | |
72 | delete A; | |
73 | g_receivedAnswers1++; | |
74 | } | |
491d03d7 | 75 | |
76 | BOOST_AUTO_TEST_CASE(test_distributor_queue) { | |
f556fbeb OM |
77 | ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0"; |
78 | ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="1000"; | |
79 | ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500"; | |
80 | S.declare("servfail-packets","Number of times a server-failed packet was sent out"); | |
81 | S.declare("timedout-packets", "timedout-packets"); | |
82 | ||
491d03d7 | 83 | auto d=Distributor<DNSPacket, Question, BackendSlow>::Create(2); |
84 | ||
85 | BOOST_CHECK_EXCEPTION( { | |
86 | int n; | |
618d20cd | 87 | // bound should be higher than max-queue-length |
f556fbeb | 88 | for(n=0; n < 2000; ++n) { |
491d03d7 | 89 | auto q = new Question(); |
90 | q->d_dt.set(); | |
48215c19 | 91 | d->question(q, report1); |
491d03d7 | 92 | } |
93 | }, DistributorFatal, [](DistributorFatal) { return true; }); | |
94 | }; | |
95 | ||
96 | struct BackendDies | |
97 | { | |
98 | BackendDies() | |
99 | { | |
100 | d_ourcount=s_count++; | |
101 | } | |
102 | ~BackendDies() | |
103 | { | |
104 | } | |
105 | DNSPacket* question(Question* q) | |
106 | { | |
107 | // cout<<"Q: "<<q->qdomain<<endl; | |
108 | if(!d_ourcount && ++d_count == 10) { | |
109 | // cerr<<"Going.. down!"<<endl; | |
110 | throw runtime_error("kill"); | |
111 | } | |
27c0050c | 112 | return new DNSPacket(true); |
491d03d7 | 113 | } |
114 | static std::atomic<int> s_count; | |
115 | int d_count{0}; | |
116 | int d_ourcount; | |
117 | }; | |
118 | ||
119 | std::atomic<int> BackendDies::s_count; | |
120 | ||
121 | std::atomic<int> g_receivedAnswers2; | |
122 | ||
123 | static void report2(DNSPacket* A) | |
124 | { | |
125 | delete A; | |
126 | g_receivedAnswers2++; | |
127 | } | |
128 | ||
129 | ||
130 | BOOST_AUTO_TEST_CASE(test_distributor_dies) { | |
f556fbeb OM |
131 | ::arg().set("overload-queue-length","Maximum queuelength moving to packetcache only")="0"; |
132 | ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000"; | |
133 | ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500"; | |
134 | S.declare("servfail-packets","Number of times a server-failed packet was sent out"); | |
135 | S.declare("timedout-packets", "timedout-packets"); | |
136 | ||
491d03d7 | 137 | auto d=Distributor<DNSPacket, Question, BackendDies>::Create(10); |
491d03d7 | 138 | |
139 | try { | |
140 | for(int n=0; n < 100; ++n) { | |
141 | auto q = new Question(); | |
142 | q->d_dt.set(); | |
eaedd091 | 143 | q->qdomain=DNSName(std::to_string(n)); |
491d03d7 | 144 | d->question(q, report2); |
145 | } | |
146 | ||
147 | sleep(1); | |
148 | cerr<<"Queued: "<<d->getQueueSize()<<endl; | |
149 | cerr<<"Received: "<<g_receivedAnswers2<<endl; | |
150 | } | |
151 | catch(std::exception& e) { | |
152 | cerr<<e.what()<<endl; | |
153 | } | |
154 | catch(PDNSException &pe) { | |
155 | cerr<<pe.reason<<endl; | |
156 | } | |
157 | }; | |
158 | ||
159 | ||
160 | ||
161 | BOOST_AUTO_TEST_SUITE_END(); |