Debian Codegraph @

cpp/src/container_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
21 #include "test_bits.hpp"
22
23 #include "proton/connection.hpp"
24 #include "proton/connection_options.hpp"
25 #include "proton/container.hpp"
26 #include "proton/listener.hpp"
27 #include "proton/listen_handler.hpp"
28 #include "proton/messaging_handler.hpp"
29 #include "proton/receiver.hpp"
30 #include "proton/receiver_options.hpp"
31 #include "proton/sender.hpp"
32 #include "proton/sender_options.hpp"
33 #include "proton/work_queue.hpp"
34
35 #include <cstdlib>
36 #include <ctime>
37 #include <string>
38 #include <cstdio>
39 #include <sstream>
40 #include <thread>
41 #include <mutex>
42 #include <condition_variable>
43
44
45 namespace {
46
47 std::string make_url(std::string host, int port) {
48 std::ostringstream url;
49 url << "//" << host << ":" << port;
50 return url.str();
51 }
52
53 struct test_listen_handler : public proton::listen_handler {
54 bool on_open_, on_accept_, on_close_;
55 std::string on_error_;
56 std::string host_;
57 proton::connection_options opts_;
58
59 test_listen_handler(const std::string& host=std::string(),
60 const proton::connection_options& opts=proton::connection_options()
61 ) : on_open_(false), on_accept_(false), on_close_(false), host_(host), opts_(opts) {}
62
63 proton::connection_options on_accept(proton::listener&) override {
64 on_accept_ = true;
65 return proton::connection_options();
66 }
67 void on_open(proton::listener& l) override {
68 on_open_ = true;
69 ASSERT(!on_accept_);
70 ASSERT(on_error_.empty());
71 ASSERT(!on_close_);
72 l.container().connect(make_url(host_, l.port()), opts_);
73 }
74
75 void on_close(proton::listener&) override {
76 on_close_ = true;
77 ASSERT(on_open_ || on_error_.size());
78 }
79
80 void on_error(proton::listener&, const std::string& e) override {
81 on_error_ = e;
82 ASSERT(!on_close_);
83 }
84 };
85
86 class test_handler : public proton::messaging_handler {
87 public:
88 bool closing;
89 bool done;
90
91 std::string peer_vhost;
92 std::string peer_container_id;
93 std::vector<proton::symbol> peer_offered_capabilities;
94 std::vector<proton::symbol> peer_desired_capabilities;
95 std::map<proton::symbol, proton::value> peer_properties;
96 proton::listener listener;
97 test_listen_handler listen_handler;
98
99 test_handler(const std::string h, const proton::connection_options& c_opts)
100 : closing(false), done(false), listen_handler(h, c_opts)
101 {}
102
103 void on_container_start(proton::container &c) override {
104 listener = c.listen("//:0", listen_handler);
105 }
106
107 void on_connection_open(proton::connection &c) override {
108 ASSERT(listen_handler.on_open_);
109 ASSERT(!listen_handler.on_close_);
110 ASSERT(listen_handler.on_error_.empty());
111 // First call is the incoming server-side of the connection, that we are interested in.
112 // Second call is for the response to the client, ignore that.
113 if (!closing) {
114 peer_vhost = c.virtual_host();
115 peer_container_id = c.container_id();
116 peer_offered_capabilities = c.offered_capabilities();
117 peer_desired_capabilities = c.desired_capabilities();
118 peer_properties = c.properties();
119 c.close();
120 }
121 closing = true;
122 }
123
124 void on_connection_close(proton::connection &) override {
125 if (!done) listener.stop();
126 done = true;
127 }
128 };
129
130 int test_container_default_container_id() {
131 proton::connection_options opts;
132 test_handler th("", opts);
133 proton::container(th).run();
134 ASSERT(!th.peer_container_id.empty());
135 ASSERT(th.listen_handler.on_error_.empty());
136 ASSERT(th.listen_handler.on_close_);
137 return 0;
138 }
139
140 int test_container_vhost() {
141 proton::connection_options opts;
142 opts.virtual_host("a.b.c");
143 test_handler th("", opts);
144 proton::container(th).run();
145 ASSERT_EQUAL(th.peer_vhost, "a.b.c");
146 return 0;
147 }
148
149 int test_container_default_vhost() {
150 proton::connection_options opts;
151 test_handler th("127.0.0.1", opts);
152 proton::container(th).run();
153 ASSERT_EQUAL(th.peer_vhost, "127.0.0.1");
154 return 0;
155 }
156
157 int test_container_no_vhost() {
158 // explicitly setting an empty virtual-host will cause the Open
159 // performative to be sent without a hostname field present.
160 // Sadly whether or not a 'hostname' field was received cannot be
161 // determined from here, so just exercise the code
162 proton::connection_options opts;
163 opts.virtual_host("");
164 test_handler th("127.0.0.1", opts);
165 proton::container(th).run();
166 ASSERT_EQUAL(th.peer_vhost, "");
167 return 0;
168 }
169
170 std::vector<proton::symbol> make_caps(const std::string& s) {
171 std::vector<proton::symbol> caps;
172 caps.push_back(s);
173 return caps;
174 }
175
176 int test_container_capabilities() {
177 proton::connection_options opts;
178 opts.offered_capabilities(make_caps("offered"));
179 opts.desired_capabilities(make_caps("desired"));
180 test_handler th("", opts);
181 proton::container(th).run();
182 ASSERT_EQUAL(th.peer_offered_capabilities.size(), 1u);
183 ASSERT_EQUAL(th.peer_offered_capabilities[0], proton::symbol("offered"));
184 ASSERT_EQUAL(th.peer_desired_capabilities.size(), 1u);
185 ASSERT_EQUAL(th.peer_desired_capabilities[0], proton::symbol("desired"));
186 ASSERT_EQUAL(th.peer_properties.size(), 0u);
187 return 0;
188 }
189
190 int test_container_properties() {
191 proton::connection_options opts;
192 std::map<proton::symbol, proton::value> props;
193 props["qpid.client_process"] = "test_process";
194 props["qpid.client_pid"] = 123;
195 opts.properties(props);
196 test_handler th("", opts);
197 proton::container(th).run();
198 ASSERT_EQUAL(th.peer_properties.size(), 2u);
199 ASSERT_EQUAL(th.peer_properties["qpid.client_process"], "test_process");
200 ASSERT_EQUAL(th.peer_properties["qpid.client_pid"], 123);
201 return 0;
202 }
203
204 int test_container_bad_address() {
205 // Listen on a bad address, check for leaks
206 // Regression test for https://issues.apache.org/jira/browse/PROTON-1217
207
208 proton::container c;
209 // Default fixed-option listener. Valgrind for leaks.
210 c.listen("999.666.999.666:0");
211 c.run();
212 // Dummy listener.
213 test_listen_handler l;
214 test_handler h2("999.999.999.666", proton::connection_options());
215 c.listen("999.666.999.666:0", l);
216 c.run();
217 ASSERT(!l.on_open_);
218 ASSERT(!l.on_accept_);
219 ASSERT(l.on_close_);
220 ASSERT(!l.on_error_.empty()); // proton:io: Name or service not known
221 return 0;
222 }
223
224 class stop_tester : public proton::messaging_handler {
225 proton::listener listener;
226 test_listen_handler listen_handler;
227
228 // Set up a listener which would block forever
229 void on_container_start(proton::container& c) override {
230 ASSERT(state==0);
231 listener = c.listen("//:0", listen_handler);
232 c.auto_stop(false);
233 state = 1;
234 }
235
236 // Get here twice - once for listener, once for connector
237 void on_connection_open(proton::connection &c) override {
238 c.close();
239 state++;
240 }
241
242 void on_connection_close(proton::connection &c) override {
243 ASSERT(state==3);
244 c.container().stop();
245 state = 4;
246 }
247 void on_container_stop(proton::container & ) override {
248 ASSERT(state==4);
249 state = 5;
250 }
251
252 void on_transport_error(proton::transport & t) override {
253 // Do nothing - ignore transport errors - we're going to get one when
254 // the container stops.
255 }
256
257 public:
258 stop_tester(): state(0) {}
259
260 int state;
261 };
262
263 int test_container_stop() {
264 stop_tester t;
265 proton::container(t).run();
266 ASSERT(t.state==5);
267 return 0;
268 }
269
270
271 struct hang_tester : public proton::messaging_handler {
272 proton::listener listener;
273 bool done;
274
275 hang_tester() : done(false) {}
276
277 void connect(proton::container* c) {
278 c->connect(make_url("", listener.port()));
279 }
280
281 void on_container_start(proton::container& c) override {
282 listener = c.listen("//:0");
283 c.schedule(proton::duration(250), proton::make_work(&hang_tester::connect, this, &c));
284 }
285
286 void on_connection_open(proton::connection& c) override {
287 c.close();
288 }
289
290 void on_connection_close(proton::connection& c) override {
291 if (!done) {
292 done = true;
293 listener.stop();
294 }
295 }
296 };
297
298 int test_container_schedule_nohang() {
299 hang_tester t;
300 proton::container(t).run();
301 return 0;
302 }
303
304 class immediate_stop_tester : public proton::messaging_handler {
305 public:
306 void on_container_start(proton::container &c) override {
307 c.stop();
308 }
309 };
310
311 int test_container_immediate_stop() {
312 immediate_stop_tester t;
313 proton::container(t).run(); // Should return after on_container_start
314 return 0;
315 }
316
317 int test_container_pre_stop() {
318 proton::container c;
319 c.stop();
320 c.run(); // Should return immediately
321 return 0;
322 }
323
324
325 struct schedule_tester : public proton::messaging_handler {
326 void stop(proton::container* c) { c->stop(); }
327
328 void on_container_start(proton::container& c) override {
329 c.schedule(proton::duration(250), proton::make_work(&schedule_tester::stop, this, &c));
330 }
331 };
332
333 int test_container_schedule_stop() {
334 schedule_tester tester;
335 proton::container c(tester);
336 c.auto_stop(false);
337 c.run();
338 return 0;
339 }
340
341 class link_test_handler : public proton::messaging_handler {//, public proton::listen_handler {
342 public:
343 bool had_receiver;
344 bool had_sender;
345
346 test_listen_handler listen_handler;
347 proton::listener listener;
348
349 proton::receiver_options receiver_options;
350 proton::sender_options sender_options;
351
352 std::map<proton::symbol, proton::value> peer_receiver_properties;
353 std::map<proton::symbol, proton::value> peer_sender_properties;
354
355 link_test_handler(const proton::receiver_options &r_opts=proton::receiver_options(),
356 const proton::sender_options &s_opts=proton::sender_options())
357 : had_receiver(false),
358 had_sender(false),
359 receiver_options(r_opts),
360 sender_options(s_opts)
361 {}
362
363 void on_container_start(proton::container &c) override {
364 listener = c.listen("//:0", listen_handler);
365 }
366
367 void on_connection_open(proton::connection &c) override {
368 if (c.uninitialized()) {
369 proton::messaging_handler::on_connection_open(c);
370 c.open_receiver("", receiver_options);
371 c.open_sender("", sender_options);
372 }
373 }
374
375
376 void check_close(proton::link &l) {
377 if (had_receiver && had_sender) {
378 l.connection().close();
379 listener.stop();
380 }
381 }
382
383 void on_receiver_open(proton::receiver &l) override {
384 had_receiver = true;
385 // When a client creates a sender then the server is notified about it as a receiver
386 peer_sender_properties = l.properties();
387 check_close(l);
388 }
389
390 void on_sender_open(proton::sender &l) override {
391 had_sender = true;
392 // When a client creates a receiver then the server is notified about it as a sender
393 peer_receiver_properties = l.properties();
394 check_close(l);
395 }
396 };
397
398 int test_container_links_no_properties() {
399 link_test_handler th;
400 proton::container(th).run();
401 ASSERT(th.had_receiver);
402 ASSERT(th.had_sender);
403 ASSERT_EQUAL(th.peer_receiver_properties.size(), 0u);
404 ASSERT_EQUAL(th.peer_sender_properties.size(), 0u);
405 return 0;
406 }
407
408 int test_container_links_properties() {
409 proton::receiver_options r_opts;
410 std::map<proton::symbol, proton::value> r_props;
411 r_props["recv.prop_str"] = "receiver_string";
412 r_opts.properties(r_props);
413
414 proton::sender_options s_opts;
415 std::map<proton::symbol, proton::value> s_props;
416 s_props["send.prop_str"] = "sender_string";
417 s_props["send.prop_int"] = 123456789;
418 s_opts.properties(s_props);
419
420 link_test_handler th(r_opts, s_opts);
421 proton::container(th).run();
422
423 ASSERT(th.had_receiver);
424 ASSERT(th.had_sender);
425 ASSERT_EQUAL(th.peer_receiver_properties.size(), 1u);
426 ASSERT_EQUAL(th.peer_receiver_properties["recv.prop_str"], "receiver_string");
427 ASSERT_EQUAL(th.peer_sender_properties.size(), 2u);
428 ASSERT_EQUAL(th.peer_sender_properties["send.prop_str"], "sender_string");
429 ASSERT_EQUAL(th.peer_sender_properties["send.prop_int"], 123456789);
430 return 0;
431 }
432
433 class test_mt_handler : public proton::messaging_handler {
434 public:
435 std::mutex lock_;
436 std::condition_variable cond_;
437 std::string str_;
438 proton::error_condition err_;
439
440 void set(const std::string& s) {
441 std::lock_guard<std::mutex> l(lock_);
442 str_ = s;
443 cond_.notify_one();
444 }
445
446 std::string wait() {
447 std::unique_lock<std::mutex> l(lock_);
448 while (str_.empty()) cond_.wait(l);
449 std::string s = str_;
450 str_.clear();
451 return s;
452 }
453
454 proton::error_condition error() const { return err_; }
455 void on_container_start(proton::container &) override { set("start"); }
456 void on_connection_open(proton::connection &) override { set("open"); }
457
458 // Catch errors and save.
459 void on_error(const proton::error_condition& e) override { err_ = e; }
460 };
461
462 class container_runner {
463 proton::container& c_;
464
465 public:
466 container_runner(proton::container& c) : c_(c) {}
467
468 void operator()() {c_.run();}
469 };
470
471 void test_container_mt_stop_empty() {
472 test_mt_handler th;
473 proton::container c(th);
474 c.auto_stop( false );
475 container_runner runner(c);
476 auto t = std::thread(runner);
477 // Must ensure that thread is joined
478 try {
479 ASSERT_EQUAL("start", th.wait());
480 c.stop();
481 t.join();
482 ASSERT_EQUAL("", th.error().name());
483 } catch (const std::exception &e) {
484 std::cerr << FAIL_MSG(e.what()) << std::endl;
485 // If join hangs, let the test die by timeout. We cannot
486 // detach and continue: deleting the container while it is
487 // still alive will put the process in an undefined state.
488 t.join();
489 throw;
490 }
491 }
492
493 void test_container_mt_stop() {
494 test_mt_handler th;
495 proton::container c(th);
496 c.auto_stop(false);
497 container_runner runner(c);
498 auto t = std::thread(runner);
499 // Must ensure that thread is joined
500 try {
501 test_listen_handler lh;
502 ASSERT_EQUAL("start", th.wait());
503 c.listen("//:0", lh); // Also opens a connection
504 ASSERT_EQUAL("open", th.wait());
505 c.stop();
506 t.join();
507 } catch (const std::exception& e) {
508 std::cerr << FAIL_MSG(e.what()) << std::endl;
509 // If join hangs, let the test die by timeout. We cannot
510 // detach and continue: deleting the container while t is
511 // still alive will put the process in an undefined state.
512 t.join();
513 throw;
514 }
515 }
516
517 class test_mt_handler_wq : public test_mt_handler {
518 public:
519 proton::work_queue *wq_;
520 proton::work call_do_close_;
521 proton::connection connection_;
522 std::mutex wqlock_;
523
524 test_mt_handler_wq() : wq_(0) {}
525
526 void on_connection_open(proton::connection &c) override {
527 {
528 std::unique_lock<std::mutex> l(wqlock_);
529 // Just record first connection side, inbound or outbound
530 if (!connection_) {
531 connection_ = c;
532 wq_ = &c.work_queue();
533 call_do_close_ = make_work(&test_mt_handler_wq::do_close, this);
534 }
535 else
536 return;
537 }
538 test_mt_handler::on_connection_open(c);
539 }
540 void initiate_close() {
541 std::unique_lock<std::mutex> l(wqlock_);
542 wq_->add(call_do_close_);
543 }
544 void do_close() { connection_.close(); }
545 void on_connection_close(proton::connection &) override { set("closed"); }
546 };
547
548 void test_container_mt_close_race() {
549 test_mt_handler_wq th;
550 proton::container c(th);
551 c.auto_stop(false);
552 container_runner runner(c);
553 auto t = std::thread(runner);
554 // Must ensure that thread is joined
555 try {
556 test_listen_handler lh;
557 ASSERT_EQUAL("start", th.wait());
558 c.listen("//:0", lh); // Also opens a connection
559 ASSERT_EQUAL("open", th.wait());
560 th.initiate_close();
561 ASSERT_EQUAL("closed", th.wait());
562 // The two sides of the connection are closing, each with its
563 // own connection context. Start a proactor disconnect to run
564 // competing close cleanup in a third context. PROTON-2027.
565 c.stop();
566 t.join();
567 } catch (const std::exception& e) {
568 std::cerr << FAIL_MSG(e.what()) << std::endl;
569 // If join hangs, let the test die by timeout. We cannot
570 // detach and continue: deleting the container while t is
571 // still alive will put the process in an undefined state.
572 t.join();
573 throw;
574 }
575 }
576
577 } // namespace
578
579 int main(int argc, char** argv) {
580 int failed = 0;
581 RUN_ARGV_TEST(failed, test_container_default_container_id());
582 RUN_ARGV_TEST(failed, test_container_vhost());
583 RUN_ARGV_TEST(failed, test_container_capabilities());
584 RUN_ARGV_TEST(failed, test_container_properties());
585 RUN_ARGV_TEST(failed, test_container_default_vhost());
586 RUN_ARGV_TEST(failed, test_container_no_vhost());
587 RUN_ARGV_TEST(failed, test_container_bad_address());
588 RUN_ARGV_TEST(failed, test_container_stop());
589 RUN_ARGV_TEST(failed, test_container_schedule_nohang());
590 RUN_ARGV_TEST(failed, test_container_immediate_stop());
591 RUN_ARGV_TEST(failed, test_container_pre_stop());
592 RUN_ARGV_TEST(failed, test_container_schedule_stop());
593 RUN_ARGV_TEST(failed, test_container_links_no_properties());
594 RUN_ARGV_TEST(failed, test_container_links_properties());
595 RUN_ARGV_TEST(failed, test_container_mt_stop_empty());
596 RUN_ARGV_TEST(failed, test_container_mt_stop());
597 RUN_ARGV_TEST(failed, test_container_mt_close_race());
598 return failed;
599 }