Debian Codegraph @

cpp/src/credit_test.cpp

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "test_bits.hpp"
21 #include "proton/connection.hpp"
22 #include "proton/connection_options.hpp"
23 #include "proton/container.hpp"
24 #include "proton/delivery.hpp"
25 #include "proton/error_condition.hpp"
26 #include "proton/listen_handler.hpp"
27 #include "proton/listener.hpp"
28 #include "proton/message.hpp"
29 #include "proton/messaging_handler.hpp"
30 #include "proton/receiver_options.hpp"
31 #include "proton/transport.hpp"
32 #include "proton/work_queue.hpp"
33
34 #include <cstdlib>
35 #include <ctime>
36 #include <string>
37 #include <cstdio>
38 #include <memory>
39 #include <sstream>
40
41 namespace {
42
43 // Wait for N things to be done.
44 class waiter {
45 size_t count;
46 public:
47 waiter(size_t n) : count(n) {}
48 void done() { if (--count == 0) ready(); }
49 virtual void ready() = 0;
50 };
51
52 class server_connection_handler : public proton::messaging_handler {
53
54 struct listen_handler : public proton::listen_handler {
55 proton::connection_options opts;
56 std::string url;
57 waiter& listen_waiter;
58
59 listen_handler(proton::messaging_handler& h, waiter& w) : listen_waiter(w) {
60 opts.handler(h);
61 }
62
63 void on_open(proton::listener& l) override {
64 std::ostringstream o;
65 o << "//:" << l.port(); // Connect to the actual listening port
66 url = o.str();
67 // Schedule rather than call done() direct to ensure serialization
68 l.container().schedule(proton::duration::IMMEDIATE,
69 proton::make_work(&waiter::done, &listen_waiter));
70 }
71
72 proton::connection_options on_accept(proton::listener&) override { return opts; }
73 };
74
75 proton::listener listener_;
76 proton::sender sender_;
77 int expect_;
78 bool closing_;
79 int available_;
80 int acked_;
81 listen_handler listen_handler_;
82 proton::work_queue *notify_wq_;
83 proton::work notify_work_;
84
85 void close (proton::connection &c) {
86 if (closing_) return;
87
88 c.close(proton::error_condition("amqp:connection:forced", "Failover testing"));
89 closing_ = true;
90 }
91
92 public:
93 server_connection_handler(proton::container& c, int a, waiter& w)
94 : expect_(0), closing_(false), available_(a), acked_(0), listen_handler_(*this, w), notify_wq_(0)
95 {
96 listener_ = c.listen("//:0", listen_handler_);
97 }
98
99 std::string url() const {
100 if (listen_handler_.url.empty()) throw std::runtime_error("no url");
101 return listen_handler_.url;
102 }
103
104 void on_connection_open(proton::connection &c) override {
105 // Only listen for a single connection
106 listener_.stop();
107 c.open();
108 }
109
110 void on_sender_open(proton::sender &s) override {
111 s.open();
112 sender_ = s;
113 }
114
115 void on_sendable(proton::sender &s) override {
116 send_available_messages(s);
117 }
118
119 void send_available_messages(proton::sender &s) {
120 bool draining = s.draining();
121 while (available_ && s.credit() > 0) {
122 s.send(proton::message("hello"));
123 available_--;
124 expect_++;
125 }
126 if (draining && !available_ && s.credit()) {
127 s.return_credit(); // return the rest
128 }
129 }
130
131 void notify_idle() {
132 notify_wq_->add(notify_work_);
133 }
134
135 void on_tracker_accept(proton::tracker& d) override {
136 acked_++;
137 if (acked_ == expect_ && (available_ == 0 || d.sender().credit() == 0))
138 notify_idle();
139 }
140
141 void on_transport_error(proton::transport & ) override {
142 // If we get an error then (try to) stop the listener
143 // - this will stop the listener if we didn't already accept a connection
144 listener_.stop();
145 }
146
147 void notify_on_idle(proton::work_queue &wq, proton::work &w) { notify_wq_ = &wq; notify_work_ = w;}
148
149 void available(int i) {
150 available_ = i;
151 }
152 };
153
154 class tester : public proton::messaging_handler, public waiter {
155 public:
156 tester() : waiter(1), container_(*this, "credit_tester"),
157 received_(0), initial_credit_(0) {}
158
159 void on_container_start(proton::container &c) override {
160 srv_.reset(new server_connection_handler(c, 100000, *this));
161 }
162
163 // waiter::ready is called when listener can accept connections.
164 void ready() override {
165 container_.connect(srv_->url());
166 }
167
168 void on_connection_open(proton::connection& c) override {
169 c.open_receiver("messages", proton::receiver_options().credit_window(0));
170 }
171
172 void on_receiver_open(proton::receiver &r) override {
173 receiver_ = r;
174 next_idle_ = proton::make_work(&tester::first_idle, this);
175 proton::work call_on_server_idle(make_work(&tester::on_server_idle, this));
176 srv_->notify_on_idle(r.work_queue(), call_on_server_idle);
177 r.add_credit(initial_credit_);
178 }
179
180 void on_message(proton::delivery &d, proton::message &m) override {
181 received_++;
182 d.accept();
183 }
184
185 void run() {
186 container_.run(); // Single threaded to avoid locks and barriers.
187 }
188
189 void server_available(int available) {
190 // If multithreaded, locking would be required.
191 srv_->available(available);
192 }
193
194 void on_server_idle() {
195 next_idle_();
196 }
197
198 void fail(const std::string &msg, int rcv) {
199 // Call from work_queue. Remember the exception later.
200 std::ostringstream os;
201 os << msg << rcv;
202 fail_msg_ = os.str();
203 receiver_.connection().close();
204 }
205
206 void on_connection_close(proton::connection& c) override {
207 if (!fail_msg_.empty())
208 FAIL(fail_msg_);
209 }
210
211 virtual void first_idle() = 0;
212
213 protected:
214 std::unique_ptr<server_connection_handler> srv_;
215 proton::container container_;
216 proton::receiver receiver_;
217 proton::work next_idle_;
218 std::string fail_msg_;
219 int received_;
220 int initial_credit_;
221 };
222
223
224 class basic_credit_tester : public tester {
225 public:
226 basic_credit_tester() { initial_credit_ = 3; }
227
228 void first_idle() override {
229 if (received_ != 3) {
230 fail(FAIL_MSG("messages received should be 3 not "), received_);
231 return;
232 }
233 next_idle_ = proton::make_work(&basic_credit_tester::second_idle, this);
234 server_available(2);
235 receiver_.add_credit(3);
236 }
237
238 void second_idle() {
239 if (received_ != 5) {
240 fail(FAIL_MSG("messages received should be 5 not "), received_);
241 return;
242 }
243 next_idle_ = proton::make_work(&basic_credit_tester::third_idle, this);
244 server_available(10);
245 receiver_.add_credit(1);
246 }
247
248 void third_idle() {
249 if (received_ != 7) {
250 fail(FAIL_MSG("messages received should be 7 not "), received_);
251 return;
252 }
253 // passed
254 receiver_.connection().close();
255 }
256 };
257
258
259 int test_basic_credit() {
260 basic_credit_tester().run();
261 return 0;
262 }
263
264
265 class drain_credit_tester : public tester {
266 int drain_finishes_;
267
268 public:
269 drain_credit_tester() : drain_finishes_(0) { initial_credit_ = 10; }
270
271 void on_receiver_drain_finish(proton::receiver &r) override {
272 drain_finishes_++;
273 }
274
275 void first_idle() override {
276 if (received_ != 10) {
277 fail(FAIL_MSG("messages received should be 10 not "), received_);
278 return;
279 }
280 next_idle_ = proton::make_work(&drain_credit_tester::second_idle, this);
281 server_available(10);
282 receiver_.add_credit(15);
283 receiver_.drain();
284 }
285
286 void second_idle() {
287 if (received_ != 20) {
288 fail(FAIL_MSG("messages received should be 20 not "), received_);
289 return;
290 }
291 if (drain_finishes_ != 1) {
292 fail(FAIL_MSG("drain finish callbacks should be 1, not: "), drain_finishes_);
293 return;
294 }
295 if (receiver_.credit() != 0) {
296 fail(FAIL_MSG("credit not returned on drain, remaining: "), receiver_.credit());
297 return;
298 }
299 next_idle_ = proton::make_work(&drain_credit_tester::third_idle, this);
300 server_available(5);
301 receiver_.add_credit(10);
302 }
303
304 void third_idle() {
305 if (received_ != 25) {
306 fail(FAIL_MSG("messages received should be 20 not "), received_);
307 return;
308 }
309 if (receiver_.credit() != 5) {
310 fail(FAIL_MSG("incorrect credit after drain, should be 5, not "), receiver_.credit());
311 return;
312 }
313 next_idle_ = proton::make_work(&drain_credit_tester::fourth_idle, this);
314 server_available(3);
315 receiver_.add_credit(1);
316 }
317
318 void fourth_idle() {
319 if (received_ != 28) {
320 fail(FAIL_MSG("messages received should be 28 not "), received_);
321 return;
322 }
323 if (receiver_.credit() != 3) {
324 fail(FAIL_MSG("incorrect credit, should be 3, not "), receiver_.credit());
325 return;
326 }
327 next_idle_ = proton::make_work(&drain_credit_tester::fifth_idle, this);
328 server_available(1);
329 receiver_.drain();
330 }
331
332 void fifth_idle() {
333 if (received_ != 29) {
334 fail(FAIL_MSG("messages received should be 29 not "), received_);
335 return;
336 }
337 if (drain_finishes_ != 2) {
338 fail(FAIL_MSG("drain finish callbacks should be 2, not: "), drain_finishes_);
339 return;
340 }
341 if (receiver_.credit() != 0) {
342 fail(FAIL_MSG("second drain credit failed, should be 0, not "), receiver_.credit());
343 return;
344 }
345 // passed
346 receiver_.connection().close();
347 }
348 };
349
350 int test_drain_credit() {
351 drain_credit_tester().run();
352 return 0;
353 }
354
355
356 } // namespace
357
358
359 int main(int argc, char** argv) {
360 int failed = 0;
361 RUN_ARGV_TEST(failed, test_basic_credit());
362 RUN_ARGV_TEST(failed, test_drain_credit());
363 return failed;
364 }