Debian Codegraph @

cpp/src/delivery_test.cpp

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include <proton/connection.hpp>
23 #include <proton/connection_options.hpp>
24 #include <proton/container.hpp>
25 #include <proton/delivery.h>
26 #include <proton/delivery.hpp>
27 #include <proton/link.hpp>
28 #include <proton/listen_handler.hpp>
29 #include <proton/listener.hpp>
30 #include <proton/message.hpp>
31 #include <proton/message_id.hpp>
32 #include <proton/messaging_handler.hpp>
33 #include <proton/tracker.hpp>
34 #include <proton/types.h>
35 #include <proton/types.hpp>
36 #include <proton/value.hpp>
37
38 #include "proton/error_condition.hpp"
39 #include "proton/receiver_options.hpp"
40 #include "proton/transport.hpp"
41 #include "proton/work_queue.hpp"
42 #include "test_bits.hpp"
43
44 #include <condition_variable>
45 #include <mutex>
46 #include <thread>
47
48 namespace {
49 std::mutex m;
50 std::condition_variable cv;
51 bool listener_ready = false;
52 int listener_port;
53 int tracker_accept_counter;
54 int tracker_settle_counter;
55 proton::binary test_tag("TESTTAG");
56 } // namespace
57
58 class test_server : public proton::messaging_handler {
59 private:
60 class listener_ready_handler : public proton::listen_handler {
61 void on_open(proton::listener &l) override {
62 {
63 std::lock_guard<std::mutex> lk(m);
64 listener_port = l.port();
65 listener_ready = true;
66 }
67 cv.notify_one();
68 }
69 };
70
71 std::string url;
72 proton::listener listener;
73 listener_ready_handler listen_handler;
74
75 public:
76 test_server (const std::string &s) : url(s) {}
77
78 void on_container_start(proton::container &c) override {
79 listener = c.listen(url, listen_handler);
80 }
81
82 void on_message(proton::delivery &d, proton::message &msg) override {
83 ASSERT_EQUAL(test_tag, d.tag());
84 d.receiver().close();
85 d.connection().close();
86 listener.stop();
87 }
88 };
89
90 class test_client : public proton::messaging_handler {
91 private:
92 std::string url;
93 proton::sender sender;
94
95 public:
96 test_client (const std::string &s) : url(s) {}
97
98 void on_container_start(proton::container &c) override {
99 proton::connection_options co;
100 sender = c.open_sender(url, co);
101 }
102
103 void on_sendable(proton::sender &s) override {
104 proton::message msg;
105 msg.body("message");
106 proton::tracker t = s.send(msg, test_tag);
107 ASSERT_EQUAL(test_tag, t.tag());
108 s.connection().close();
109 }
110
111 void on_tracker_accept(proton::tracker &t) override {
112 ASSERT_EQUAL(test_tag, t.tag());
113 tracker_accept_counter++;
114 }
115
116 void on_tracker_settle(proton::tracker &t) override {
117 ASSERT_EQUAL(test_tag, t.tag());
118 tracker_settle_counter++;
119 }
120 };
121
122 int test_delivery_tag() {
123 tracker_accept_counter = 0;
124 tracker_settle_counter = 0;
125
126 std::string recv_address("127.0.0.1:0/test");
127 test_server recv(recv_address);
128 proton::container c(recv);
129 std::thread thread_recv([&c]() -> void { c.run(); });
130
131 // wait until listener is ready
132 std::unique_lock<std::mutex> lk(m);
133 cv.wait(lk, [] { return listener_ready; });
134
135 std::string send_address =
136 "127.0.0.1:" + std::to_string(listener_port) + "/test";
137 test_client send(send_address);
138 proton::container(send).run();
139 thread_recv.join();
140
141 ASSERT_EQUAL(1 ,tracker_accept_counter);
142 ASSERT_EQUAL(1 ,tracker_settle_counter);
143
144 return 0;
145 }
146
147 int main(int argc, char **argv) {
148 int failed = 0;
149 RUN_ARGV_TEST(failed, test_delivery_tag());
150 return failed;
151 }