| 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/delivery.hpp> |
| 27 |
#include <proton/listen_handler.hpp> |
| 28 |
#include <proton/listener.hpp> |
| 29 |
#include <proton/message.hpp> |
| 30 |
#include <proton/messaging_handler.hpp> |
| 31 |
#include <proton/receiver_options.hpp> |
| 32 |
#include <proton/sender_options.hpp> |
| 33 |
#include <proton/source_options.hpp> |
| 34 |
#include <proton/target_options.hpp> |
| 35 |
#include <proton/types.hpp> |
| 36 |
|
| 37 |
#include <iostream> |
| 38 |
|
| 39 |
#include <condition_variable> |
| 40 |
#include <map> |
| 41 |
#include <mutex> |
| 42 |
#include <thread> |
| 43 |
|
| 44 |
using property_map=proton::terminus::dynamic_property_map; |
| 45 |
|
| 46 |
namespace { |
| 47 |
std::mutex m; |
| 48 |
std::condition_variable cv; |
| 49 |
bool listener_ready = false; |
| 50 |
int listener_port; |
| 51 |
const std::string DYNAMIC_ADDRESS = "test_dynamic_address"; |
| 52 |
} // namespace |
| 53 |
|
| 54 |
class test_server : public proton::messaging_handler { |
| 55 |
private: |
| 56 |
class listener_ready_handler : public proton::listen_handler { |
| 57 |
void on_open(proton::listener& l) override { |
| 58 |
{ |
| 59 |
std::lock_guard<std::mutex> lk(m); |
| 60 |
listener_port = l.port(); |
| 61 |
listener_ready = true; |
| 62 |
} |
| 63 |
cv.notify_one(); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
std::string url; |
| 68 |
proton::listener listener; |
| 69 |
listener_ready_handler listen_handler; |
| 70 |
|
| 71 |
public: |
| 72 |
test_server (const std::string& s) : url(s) {} |
| 73 |
|
| 74 |
void on_container_start(proton::container& c) override { |
| 75 |
listener = c.listen(url, listen_handler); |
| 76 |
} |
| 77 |
|
| 78 |
void on_sender_open(proton::sender& s) override { |
| 79 |
ASSERT(s.source().dynamic()); |
| 80 |
ASSERT(s.source().address().empty()); |
| 81 |
property_map p = { |
| 82 |
{proton::symbol("supported-dist-modes"), proton::symbol("copy")} |
| 83 |
}; |
| 84 |
ASSERT_EQUAL(s.source().dynamic_properties(), p); |
| 85 |
|
| 86 |
proton::source_options opts; |
| 87 |
opts.address(DYNAMIC_ADDRESS) |
| 88 |
// This fails due to a bug in the C++ bindings - PROTON-2480 |
| 89 |
// .dynamic(true) |
| 90 |
.dynamic_properties({{proton::symbol("supported-dist-modes"), proton::symbol("move")}}); |
| 91 |
s.open(proton::sender_options().source(opts)); |
| 92 |
listener.stop(); |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
class test_client : public proton::messaging_handler { |
| 97 |
private: |
| 98 |
std::string url; |
| 99 |
|
| 100 |
public: |
| 101 |
test_client (const std::string& s) : url(s) {} |
| 102 |
|
| 103 |
void on_container_start(proton::container& c) override { |
| 104 |
property_map sp = std::map<proton::symbol, proton::value>{ |
| 105 |
{proton::symbol("supported-dist-modes"), proton::symbol("copy")} |
| 106 |
}; |
| 107 |
proton::source_options opts; |
| 108 |
opts.dynamic(true) |
| 109 |
.dynamic_properties(sp); |
| 110 |
c.open_receiver(url, proton::receiver_options().source(opts)); |
| 111 |
} |
| 112 |
|
| 113 |
void on_receiver_open(proton::receiver& r) override { |
| 114 |
// This fails due to a bug in the c++ bindings - PROTON-2480 |
| 115 |
// ASSERT(r.source().dynamic()); |
| 116 |
ASSERT_EQUAL(DYNAMIC_ADDRESS, r.source().address()); |
| 117 |
property_map m = std::map<proton::symbol, proton::value>{ |
| 118 |
{proton::symbol("supported-dist-modes"), proton::symbol("move")} |
| 119 |
}; |
| 120 |
ASSERT_EQUAL(m, r.source().dynamic_properties()); |
| 121 |
|
| 122 |
r.connection().close(); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
int test_dynamic_node_properties() { |
| 127 |
|
| 128 |
std::string recv_address("127.0.0.1:0"); |
| 129 |
test_server server(recv_address); |
| 130 |
proton::container c(server); |
| 131 |
std::thread thread_recv([&c]() -> void { c.run(); }); |
| 132 |
|
| 133 |
// wait until listener is ready |
| 134 |
std::unique_lock<std::mutex> lk(m); |
| 135 |
cv.wait(lk, [] { return listener_ready; }); |
| 136 |
|
| 137 |
std::string send_address = "127.0.0.1:" + std::to_string(listener_port); |
| 138 |
test_client client(send_address); |
| 139 |
proton::container(client).run(); |
| 140 |
thread_recv.join(); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
int test_link_name() |
| 146 |
{ |
| 147 |
proton::container c; |
| 148 |
|
| 149 |
proton::sender_options so; |
| 150 |
proton::receiver_options ro; |
| 151 |
|
| 152 |
so.name("qpid-s"); |
| 153 |
ro.name("qpid-r"); |
| 154 |
|
| 155 |
proton::sender sender = c.open_sender("", so); |
| 156 |
proton::receiver receiver = c.open_receiver("", ro); |
| 157 |
|
| 158 |
ASSERT_EQUAL("qpid-s", sender.name()); |
| 159 |
ASSERT_EQUAL("qpid-r", receiver.name()); |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
int main(int argc, char** argv) { |
| 165 |
int failed = 0; |
| 166 |
RUN_ARGV_TEST(failed, test_link_name()); |
| 167 |
RUN_ARGV_TEST(failed, test_dynamic_node_properties()); |
| 168 |
return failed; |
| 169 |
} |