Debian Codegraph @

cpp/src/connection_driver_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 #include "proton_bits.hpp"
23 #include "link_namer.hpp"
24
25 #include "proton/connection.hpp"
26 #include "proton/container.hpp"
27 #include "proton/io/connection_driver.hpp"
28 #include "proton/link.hpp"
29 #include "proton/message.hpp"
30 #include "proton/messaging_handler.hpp"
31 #include "proton/receiver_options.hpp"
32 #include "proton/sender.hpp"
33 #include "proton/sender_options.hpp"
34 #include "proton/source.hpp"
35 #include "proton/source_options.hpp"
36 #include "proton/target.hpp"
37 #include "proton/target_options.hpp"
38 #include "proton/transport.hpp"
39 #include "proton/types_fwd.hpp"
40 #include "proton/uuid.hpp"
41
42 #include <deque>
43 #include <algorithm>
44
45 namespace {
46
47 using namespace std;
48 using namespace proton;
49 using namespace test;
50
51 using proton::io::connection_driver;
52 using proton::io::const_buffer;
53 using proton::io::mutable_buffer;
54
55 typedef std::deque<char> byte_stream;
56
57 static const int MAX_SPIN = 1000; // Give up after 1000 event-less dispatches
58
59 /// In memory connection_driver that reads and writes from byte_streams
60 struct in_memory_driver : public connection_driver {
61
62 byte_stream& reads;
63 byte_stream& writes;
64 int spinning;
65
66 in_memory_driver(byte_stream& rd, byte_stream& wr, const std::string& name) :
67 connection_driver(name), reads(rd), writes(wr), spinning(0) {}
68
69 void do_read() {
70 mutable_buffer rbuf = read_buffer();
71 size_t size = std::min(reads.size(), rbuf.size);
72 if (size) {
73 copy(reads.begin(), reads.begin()+size, static_cast<char*>(rbuf.data));
74 read_done(size);
75 reads.erase(reads.begin(), reads.begin()+size);
76 }
77 }
78
79 void do_write() {
80 const_buffer wbuf = write_buffer();
81 if (wbuf.size) {
82 writes.insert(writes.begin(),
83 static_cast<const char*>(wbuf.data),
84 static_cast<const char*>(wbuf.data) + wbuf.size);
85 write_done(wbuf.size);
86 }
87 }
88
89 void check_idle() {
90 spinning = has_events() ? 0 : spinning+1;
91 if (spinning > MAX_SPIN)
92 throw test::error("no activity, interrupting test");
93 }
94
95 timestamp process(timestamp t = timestamp()) {
96 check_idle();
97 if (!dispatch())
98 throw test::error("unexpected close: "+connection().error().what());
99 timestamp next_tick;
100 if (t!=timestamp()) next_tick = tick(t);
101 do_read();
102 do_write();
103 check_idle();
104 dispatch();
105 return next_tick;
106 }
107 };
108
109 /// A pair of drivers that talk to each other in-memory, simulating a connection.
110 struct driver_pair {
111 byte_stream ab, ba;
112 in_memory_driver a, b;
113
114 driver_pair(const connection_options& oa, const connection_options& ob,
115 const std::string& name=""
116 ) :
117 a(ba, ab, name+"a"), b(ab, ba, name+"b")
118 {
119 a.connect(oa);
120 b.accept(ob);
121 }
122
123 void process() { a.process(); b.process(); }
124 };
125
126 /// A pair of drivers that talk to each other in-memory, simulating a connection.
127 /// This version also simulates the passage of time
128 struct timed_driver_pair {
129 duration timeout;
130 byte_stream ab, ba;
131 in_memory_driver a, b;
132 timestamp now;
133
134 timed_driver_pair(duration t, const connection_options& oa0, const connection_options& ob0,
135 const std::string& name=""
136 ) :
137 timeout(t),
138 a(ba, ab, name+"a"), b(ab, ba, name+"b"),
139 now(100100100)
140 {
141 connection_options oa(oa0);
142 connection_options ob(ob0);
143 a.connect(oa.idle_timeout(t));
144 b.accept(ob.idle_timeout(t));
145 }
146
147 void process_untimed() { a.process(); b.process(); }
148 void process_timed_succeed() {
149 timestamp anow = now + timeout - duration(100);
150 timestamp bnow = now + timeout - duration(100);
151 a.process(anow);
152 b.process(bnow);
153 now = std::max(anow, bnow);
154 }
155 void process_timed_fail() {
156 timestamp anow = now + timeout + timeout + duration(100);
157 timestamp bnow = now + timeout + timeout + duration(100);
158 a.process(anow);
159 b.process(bnow);
160 now = std::max(anow, bnow);
161 }
162 };
163
164 /// A handler that records incoming endpoints, errors etc.
165 struct record_handler : public messaging_handler {
166 std::deque<proton::receiver> receivers;
167 std::deque<proton::sender> senders;
168 std::deque<proton::session> sessions;
169 std::deque<std::string> unhandled_errors, transport_errors, connection_errors;
170 std::deque<proton::message> messages;
171
172 size_t link_count() const { return senders.size() + receivers.size(); }
173
174 void on_receiver_open(receiver &l) override {
175 messaging_handler::on_receiver_open(l);
176 receivers.push_back(l);
177 }
178
179 void on_sender_open(sender &l) override {
180 messaging_handler::on_sender_open(l);
181 senders.push_back(l);
182 }
183
184 void on_session_open(session &s) override {
185 messaging_handler::on_session_open(s);
186 sessions.push_back(s);
187 }
188
189 void on_transport_error(transport& t) override {
190 transport_errors.push_back(t.error().what());
191 }
192
193 void on_connection_error(connection& c) override {
194 connection_errors.push_back(c.error().what());
195 }
196
197 void on_error(const proton::error_condition& c) override {
198 unhandled_errors.push_back(c.what());
199 }
200
201 void on_message(proton::delivery&, proton::message& m) override {
202 messages.push_back(m);
203 }
204 };
205
206 template <class S> typename S::value_type quick_pop(S& s) {
207 ASSERT(!s.empty());
208 typename S::value_type x = s.front();
209 s.pop_front();
210 return x;
211 }
212
213 struct namer : public io::link_namer {
214 char name;
215 namer(char c) : name(c) {}
216 std::string link_name() { return std::string(1, name++); }
217 };
218
219 void test_driver_link_id() {
220 record_handler ha, hb;
221 driver_pair d(ha, hb);
222 d.a.connect(ha);
223 d.b.accept(hb);
224
225 namer na('x');
226 namer nb('b');
227 connection ca = d.a.connection();
228 connection cb = d.b.connection();
229 set_link_namer(ca, na);
230 set_link_namer(cb, nb);
231
232 d.b.connection().open();
233
234 d.a.connection().open_sender("foo");
235 while (ha.senders.empty() || hb.receivers.empty()) d.process();
236 sender s = quick_pop(ha.senders);
237 ASSERT_EQUAL("x", s.name());
238
239 ASSERT_EQUAL("x", quick_pop(hb.receivers).name());
240
241 d.a.connection().open_receiver("bar");
242 while (ha.receivers.empty() || hb.senders.empty()) d.process();
243 ASSERT_EQUAL("y", quick_pop(ha.receivers).name());
244 ASSERT_EQUAL("y", quick_pop(hb.senders).name());
245
246 d.b.connection().open_receiver("");
247 while (ha.senders.empty() || hb.receivers.empty()) d.process();
248 ASSERT_EQUAL("b", quick_pop(ha.senders).name());
249 ASSERT_EQUAL("b", quick_pop(hb.receivers).name());
250 }
251
252 void test_endpoint_close() {
253 record_handler ha, hb;
254 driver_pair d(ha, hb);
255 d.a.connection().open_sender("x");
256 d.a.connection().open_receiver("y");
257 while (ha.senders.size()+ha.receivers.size() < 2 ||
258 hb.senders.size()+hb.receivers.size() < 2) d.process();
259 proton::link ax = quick_pop(ha.senders), ay = quick_pop(ha.receivers);
260 proton::link bx = quick_pop(hb.receivers), by = quick_pop(hb.senders);
261
262 // Close a link
263 ax.close(proton::error_condition("err", "foo bar"));
264 while (!bx.closed()) d.process();
265 proton::error_condition c = bx.error();
266 ASSERT_EQUAL("err", c.name());
267 ASSERT_EQUAL("foo bar", c.description());
268 ASSERT_EQUAL("err: foo bar", c.what());
269
270 // Close a link with an empty condition
271 ay.close(proton::error_condition());
272 while (!by.closed()) d.process();
273 ASSERT(by.error().empty());
274
275 // Close a connection
276 connection ca = d.a.connection(), cb = d.b.connection();
277 ca.close(proton::error_condition("conn", "bad connection"));
278 while (!cb.closed()) d.process();
279 ASSERT_EQUAL("conn: bad connection", cb.error().what());
280 ASSERT_EQUAL(1u, hb.connection_errors.size());
281 ASSERT_EQUAL("conn: bad connection", hb.connection_errors.front());
282 }
283
284 void test_driver_disconnected() {
285 // driver.disconnected() aborts the connection and calls the local on_transport_error()
286 record_handler ha, hb;
287 driver_pair d(ha, hb);
288 d.a.connect(ha);
289 d.b.accept(hb);
290 while (!d.a.connection().active() || !d.b.connection().active())
291 d.process();
292
293 // Close a with an error condition. The AMQP connection is still open.
294 d.a.disconnected(proton::error_condition("oops", "driver failure"));
295 ASSERT(!d.a.dispatch());
296 ASSERT(!d.a.connection().closed());
297 ASSERT(d.a.connection().error().empty());
298 ASSERT_EQUAL(0u, ha.connection_errors.size());
299 ASSERT_EQUAL("oops: driver failure", d.a.transport().error().what());
300 ASSERT_EQUAL(1u, ha.transport_errors.size());
301 ASSERT_EQUAL("oops: driver failure", ha.transport_errors.front());
302
303 // In a real app the IO code would detect the abort and do this:
304 d.b.disconnected(proton::error_condition("broken", "it broke"));
305 ASSERT(!d.b.dispatch());
306 ASSERT(!d.b.connection().closed());
307 ASSERT(d.b.connection().error().empty());
308 ASSERT_EQUAL(0u, hb.connection_errors.size());
309 // Proton-C adds (connection aborted) if transport closes too early,
310 // and provides a default message if there is no user message.
311 ASSERT_EQUAL("broken: it broke (connection aborted)", d.b.transport().error().what());
312 ASSERT_EQUAL(1u, hb.transport_errors.size());
313 ASSERT_EQUAL("broken: it broke (connection aborted)", hb.transport_errors.front());
314 }
315
316 void test_no_container() {
317 // An driver with no container should throw, not crash.
318 connection_driver d;
319 try {
320 d.connection().container();
321 FAIL("expected error");
322 } catch (const proton::error&) {}
323 }
324
325 void test_spin_interrupt() {
326 // Check the test framework interrupts a spinning driver pair with nothing to do.
327 record_handler ha, hb;
328 driver_pair d(ha, hb);
329 try {
330 while (true)
331 d.process();
332 FAIL("expected exception");
333 } catch (const test::error&) {}
334 }
335
336 #define ASSERT_ADDR(ADDR, TERMINUS) do { \
337 ASSERT_EQUAL((ADDR), (TERMINUS).address()); \
338 if ((ADDR) == std::string()) ASSERT((TERMINUS).anonymous()); \
339 else ASSERT(!(TERMINUS).anonymous()); \
340 } while(0);
341
342 #define ASSERT_LINK(SRC, TGT, LINK) do { \
343 ASSERT_ADDR((SRC), (LINK).source()); \
344 ASSERT_ADDR((TGT), (LINK).target()); \
345 } while(0);
346
347 void test_link_address() {
348 record_handler ha, hb;
349 driver_pair d(ha, hb);
350
351 // Using open(address, opts)
352 d.a.connection().open_sender("tx", sender_options().name("_x").source(source_options().address("sx")));
353 d.a.connection().open_receiver("sy", receiver_options().name("_y").target(target_options().address("ty")));
354 while (ha.link_count()+hb.link_count() < 4) d.process();
355
356 proton::sender ax = quick_pop(ha.senders);
357 ASSERT_EQUAL("_x", ax.name());
358 ASSERT_LINK("sx", "tx", ax);
359 proton::receiver bx = quick_pop(hb.receivers);
360 ASSERT_EQUAL("_x", bx.name());
361 ASSERT_LINK("sx", "tx", bx);
362
363 proton::receiver ay = quick_pop(ha.receivers);
364 ASSERT_EQUAL("_y", ay.name());
365 ASSERT_LINK("sy", "ty", ay);
366 proton::sender by = quick_pop(hb.senders);
367 ASSERT_EQUAL("_y", by.name());
368 ASSERT_LINK("sy", "ty", by);
369
370 // Override address parameter in opts
371 d.a.connection().open_sender("x", sender_options().target(target_options().address("X")));
372 d.a.connection().open_receiver("y", receiver_options().source(source_options().address("Y")));
373 while (ha.link_count()+hb.link_count() < 4) d.process();
374
375 ax = quick_pop(ha.senders);
376 ASSERT_LINK("", "X", ax);
377 bx = quick_pop(hb.receivers);
378 ASSERT_LINK("", "X", bx);
379
380 ay = quick_pop(ha.receivers);
381 ASSERT_LINK("Y", "", ay);
382 by = quick_pop(hb.senders);
383 ASSERT_LINK("Y", "", by);
384 }
385
386 void test_link_anonymous_dynamic() {
387 record_handler ha, hb;
388 driver_pair d(ha, hb);
389
390 // Anonymous link should have NULL address
391 d.a.connection().open_sender("x", sender_options().target(target_options().anonymous(true)));
392 d.a.connection().open_receiver("y", receiver_options().source(source_options().anonymous(true)));
393 while (ha.link_count()+hb.link_count() < 4) d.process();
394
395 proton::sender ax = quick_pop(ha.senders);
396 ASSERT_LINK("", "", ax);
397 proton::receiver bx = quick_pop(hb.receivers);
398 ASSERT_LINK("", "", bx);
399
400 proton::receiver ay = quick_pop(ha.receivers);
401 ASSERT_LINK("", "", ay);
402 proton::sender by = quick_pop(hb.senders);
403 ASSERT_LINK("", "", by);
404
405 // Dynamic link should have NULL address and dynamic flag
406 d.a.connection().open_sender("x", sender_options().target(target_options().dynamic(true)));
407 d.a.connection().open_receiver("y", receiver_options().source(source_options().dynamic(true)));
408 while (ha.link_count()+hb.link_count() < 4) d.process();
409
410 ax = quick_pop(ha.senders);
411 ASSERT(ax.target().dynamic());
412 ASSERT_LINK("", "", ax);
413 bx = quick_pop(hb.receivers);
414 ASSERT(bx.target().dynamic());
415 ASSERT_LINK("", "", bx);
416
417 ay = quick_pop(ha.receivers);
418 ASSERT(ay.source().dynamic());
419 ASSERT_LINK("", "", ay);
420 by = quick_pop(hb.senders);
421 ASSERT(by.source().dynamic());
422 ASSERT_LINK("", "", by);
423
424 // Empty string as a link address is allowed and not considered anonymous.
425 d.a.connection().open_sender("", sender_options());
426 d.a.connection().open_receiver("", receiver_options());
427 while (ha.link_count()+hb.link_count() < 4) d.process();
428
429 ax = quick_pop(ha.senders);
430 ASSERT(ax.target().address().empty());
431 ASSERT(!ax.target().anonymous());
432
433 ay = quick_pop(ha.receivers);
434 ASSERT(ay.source().address().empty());
435 ASSERT(!ay.source().anonymous());
436 }
437
438 void test_link_capability_filter() {
439 record_handler ha, hb;
440 driver_pair d(ha, hb);
441
442 // Capabilities and filters
443 std::vector<proton::symbol> caps;
444 caps.push_back("foo");
445 caps.push_back("bar");
446
447 d.a.connection().open_sender("x", sender_options().target(target_options().capabilities(caps)));
448
449 source::filter_map f;
450 f.put("1", "11");
451 f.put("2", "22");
452 d.a.connection().open_receiver("y", receiver_options().source(source_options().filters(f).capabilities(caps)));
453 while (ha.link_count()+hb.link_count() < 4) d.process();
454
455 proton::sender ax = quick_pop(ha.senders);
456 ASSERT_EQUAL(many<proton::symbol>() + "foo" + "bar", ax.target().capabilities());
457
458 proton::receiver ay = quick_pop(ha.receivers);
459 ASSERT_EQUAL(many<proton::symbol>() + "foo" + "bar", ay.source().capabilities());
460
461 proton::receiver bx = quick_pop(hb.receivers);
462 ASSERT_EQUAL(many<proton::symbol>() + "foo" + "bar", bx.target().capabilities());
463 ASSERT_EQUAL(many<proton::symbol>(), bx.source().capabilities());
464
465 proton::sender by = quick_pop(hb.senders);
466 ASSERT_EQUAL(many<proton::symbol>() + "foo" + "bar", by.source().capabilities());
467 f = by.source().filters();
468 ASSERT_EQUAL(2U, f.size());
469 ASSERT_EQUAL(value("11"), f.get("1"));
470 ASSERT_EQUAL(value("22"), f.get("2"));
471 }
472
473 void test_message() {
474 // Verify a message arrives intact
475 record_handler ha, hb;
476 driver_pair d(ha, hb);
477
478 proton::sender s = d.a.connection().open_sender("x");
479 proton::message m("barefoot");
480 m.properties().put("x", "y");
481 m.message_annotations().put("a", "b");
482 s.send(m);
483
484 while (hb.messages.size() == 0)
485 d.process();
486
487 proton::message m2 = quick_pop(hb.messages);
488 ASSERT_EQUAL(value("barefoot"), m2.body());
489 ASSERT_EQUAL(value("y"), m2.properties().get("x"));
490 ASSERT_EQUAL(value("b"), m2.message_annotations().get("a"));
491 }
492
493 void test_message_timeout_succeed() {
494 // Verify a message arrives intact
495 record_handler ha, hb;
496 timed_driver_pair d(duration(2000), ha, hb);
497
498 proton::sender s = d.a.connection().open_sender("x");
499 d.process_timed_succeed();
500 proton::message m("barefoot_timed_succeed");
501 m.properties().put("x", "y");
502 m.message_annotations().put("a", "b");
503 s.send(m);
504
505 while (hb.messages.size() == 0)
506 d.process_timed_succeed();
507
508 proton::message m2 = quick_pop(hb.messages);
509 ASSERT_EQUAL(value("barefoot_timed_succeed"), m2.body());
510 ASSERT_EQUAL(value("y"), m2.properties().get("x"));
511 ASSERT_EQUAL(value("b"), m2.message_annotations().get("a"));
512 }
513
514 void test_message_timeout_fail() {
515 // Verify a message arrives intact
516 record_handler ha, hb;
517 timed_driver_pair d(duration(2000), ha, hb);
518
519 proton::sender s = d.a.connection().open_sender("x");
520 d.process_timed_fail();
521 proton::message m("barefoot_timed_fail");
522 m.properties().put("x", "y");
523 m.message_annotations().put("a", "b");
524 s.send(m);
525
526 d.process_timed_fail();
527
528 ASSERT_THROWS(test::error,
529 while (hb.messages.size() == 0) {
530 d.process_timed_fail();
531 }
532 );
533
534 ASSERT_EQUAL(1u, hb.transport_errors.size());
535 ASSERT_EQUAL("amqp:resource-limit-exceeded: local-idle-timeout expired", d.b.transport().error().what());
536 ASSERT_EQUAL(1u, ha.connection_errors.size());
537 ASSERT_EQUAL("amqp:resource-limit-exceeded: local-idle-timeout expired", d.a.connection().error().what());
538 }
539 }
540
541 int main(int argc, char** argv) {
542 int failed = 0;
543 RUN_ARGV_TEST(failed, test_driver_link_id());
544 RUN_ARGV_TEST(failed, test_endpoint_close());
545 RUN_ARGV_TEST(failed, test_driver_disconnected());
546 RUN_ARGV_TEST(failed, test_no_container());
547 RUN_ARGV_TEST(failed, test_spin_interrupt());
548 RUN_ARGV_TEST(failed, test_link_address());
549 RUN_ARGV_TEST(failed, test_link_anonymous_dynamic());
550 RUN_ARGV_TEST(failed, test_link_capability_filter());
551 RUN_ARGV_TEST(failed, test_message());
552 RUN_ARGV_TEST(failed, test_message_timeout_succeed());
553 RUN_ARGV_TEST(failed, test_message_timeout_fail());
554 return failed;
555 }