Commit 89940b10 authored by Joseph Noir's avatar Joseph Noir

Update test to use URIs instead of host and port

parent 3bca295b
...@@ -31,16 +31,17 @@ ...@@ -31,16 +31,17 @@
#include "caf/io/all.hpp" #include "caf/io/all.hpp"
using namespace caf; using namespace caf;
using namespace std;
namespace { namespace {
constexpr char local_host[] = "127.0.0.1"; constexpr char host_uri[] = "tcp://127.0.0.1";
class config : public actor_system_config { class config : public actor_system_config {
public: public:
config() { config() {
load<io::middleman>(); load<io::middleman>();
add_message_type<std::vector<int>>("std::vector<int>"); add_message_type<vector<int>>("vector<int>");
actor_system_config::parse(test::engine::argc(), actor_system_config::parse(test::engine::argc(),
test::engine::argv()); test::engine::argv());
} }
...@@ -82,8 +83,8 @@ behavior make_ping_behavior(event_based_actor* self, actor pong) { ...@@ -82,8 +83,8 @@ behavior make_ping_behavior(event_based_actor* self, actor pong) {
}; };
} }
std::string to_string(const std::vector<int>& vec) { string to_string(const vector<int>& vec) {
std::ostringstream os; ostringstream os;
for (size_t i = 0; i + 1 < vec.size(); ++i) for (size_t i = 0; i + 1 < vec.size(); ++i)
os << vec[i] << ", "; os << vec[i] << ", ";
os << vec.back(); os << vec.back();
...@@ -92,19 +93,19 @@ std::string to_string(const std::vector<int>& vec) { ...@@ -92,19 +93,19 @@ std::string to_string(const std::vector<int>& vec) {
behavior make_sort_behavior() { behavior make_sort_behavior() {
return { return {
[](std::vector<int>& vec) -> std::vector<int> { [](vector<int>& vec) -> vector<int> {
CAF_MESSAGE("sorter received: " << to_string(vec)); CAF_MESSAGE("sorter received: " << to_string(vec));
std::sort(vec.begin(), vec.end()); sort(vec.begin(), vec.end());
CAF_MESSAGE("sorter sent: " << to_string(vec)); CAF_MESSAGE("sorter sent: " << to_string(vec));
return std::move(vec); return move(vec);
} }
}; };
} }
behavior make_sort_requester_behavior(event_based_actor* self, actor sorter) { behavior make_sort_requester_behavior(event_based_actor* self, actor sorter) {
self->send(sorter, std::vector<int>{5, 4, 3, 2, 1}); self->send(sorter, vector<int>{5, 4, 3, 2, 1});
return { return {
[=](const std::vector<int>& vec) { [=](const vector<int>& vec) {
CAF_MESSAGE("sort requester received: " << to_string(vec)); CAF_MESSAGE("sort requester received: " << to_string(vec));
for (size_t i = 1; i < vec.size(); ++i) for (size_t i = 1; i < vec.size(); ++i)
CAF_CHECK_EQUAL(static_cast<int>(i), vec[i - 1]); CAF_CHECK_EQUAL(static_cast<int>(i), vec[i - 1]);
...@@ -141,44 +142,62 @@ CAF_TEST_FIXTURE_SCOPE(dynamic_remote_actor_tests, fixture) ...@@ -141,44 +142,62 @@ CAF_TEST_FIXTURE_SCOPE(dynamic_remote_actor_tests, fixture)
CAF_TEST(identity_semantics) { CAF_TEST(identity_semantics) {
// server side // server side
auto server = server_side.spawn(make_pong_behavior); auto server = server_side.spawn(make_pong_behavior);
CAF_EXP_THROW(port1, server_side_mm.publish(server, 0, local_host)); auto uri_no_port = io::uri::make(host_uri);
CAF_EXP_THROW(port2, server_side_mm.publish(server, 0, local_host)); CAF_REQUIRE(uri_no_port);
CAF_EXP_THROW(port1, server_side_mm.publish(server, *uri_no_port));
CAF_EXP_THROW(port2, server_side_mm.publish(server, *uri_no_port));
CAF_REQUIRE_NOT_EQUAL(port1, port2); CAF_REQUIRE_NOT_EQUAL(port1, port2);
CAF_EXP_THROW(same_server, server_side_mm.remote_actor(local_host, port2)); auto uri_port1 = io::uri::make(string(host_uri) + ":" + to_string(port1));
CAF_REQUIRE(uri_port1);
auto uri_port2 = io::uri::make(string(host_uri) + ":" + to_string(port2));
CAF_REQUIRE(uri_port2);
CAF_EXP_THROW(same_server, server_side_mm.remote_actor(*uri_port2));
CAF_REQUIRE_EQUAL(same_server, server); CAF_REQUIRE_EQUAL(same_server, server);
CAF_CHECK_EQUAL(same_server->node(), server_side.node()); CAF_CHECK_EQUAL(same_server->node(), server_side.node());
CAF_EXP_THROW(server1, client_side_mm.remote_actor(local_host, port1)); CAF_EXP_THROW(server1, client_side_mm.remote_actor(*uri_port1));
CAF_EXP_THROW(server2, client_side_mm.remote_actor(local_host, port2)); CAF_EXP_THROW(server2, client_side_mm.remote_actor(*uri_port2));
CAF_CHECK_EQUAL(server1, client_side_mm.remote_actor(local_host, port1)); CAF_CHECK_EQUAL(server1, client_side_mm.remote_actor(*uri_port1));
CAF_CHECK_EQUAL(server2, client_side_mm.remote_actor(local_host, port2)); CAF_CHECK_EQUAL(server2, client_side_mm.remote_actor(*uri_port2));
anon_send_exit(server, exit_reason::user_shutdown); anon_send_exit(server, exit_reason::user_shutdown);
} }
CAF_TEST(ping_pong) { CAF_TEST(ping_pong) {
// server side // server side
auto server_uri = io::uri::make(host_uri);
CAF_REQUIRE(server_uri);
CAF_EXP_THROW(port, CAF_EXP_THROW(port,
server_side_mm.publish(server_side.spawn(make_pong_behavior), server_side_mm.publish(server_side.spawn(make_pong_behavior),
0, local_host)); *server_uri));
// client side // client side
CAF_EXP_THROW(pong, client_side_mm.remote_actor(local_host, port)); auto uri_with_port = io::uri::make(string(host_uri) + ":" + to_string(port));
CAF_REQUIRE(uri_with_port);
CAF_EXP_THROW(pong, client_side_mm.remote_actor(*uri_with_port));
client_side.spawn(make_ping_behavior, pong); client_side.spawn(make_ping_behavior, pong);
} }
CAF_TEST(custom_message_type) { CAF_TEST(custom_message_type) {
// server side // server side
auto server_uri = io::uri::make(host_uri);
CAF_REQUIRE(server_uri);
CAF_EXP_THROW(port, server_side_mm.publish(server_side.spawn(make_sort_behavior), CAF_EXP_THROW(port, server_side_mm.publish(server_side.spawn(make_sort_behavior),
0, local_host)); *server_uri));
// client side // client side
CAF_EXP_THROW(sorter, client_side_mm.remote_actor(local_host, port)); auto uri_with_port = io::uri::make(string(host_uri) + ":" + to_string(port));
CAF_REQUIRE(uri_with_port);
CAF_EXP_THROW(sorter, client_side_mm.remote_actor(*uri_with_port));
client_side.spawn(make_sort_requester_behavior, sorter); client_side.spawn(make_sort_requester_behavior, sorter);
} }
CAF_TEST(remote_link) { CAF_TEST(remote_link) {
// server side // server side
auto server_uri = io::uri::make(host_uri);
CAF_REQUIRE(server_uri);
CAF_EXP_THROW(port, server_side_mm.publish(server_side.spawn(fragile_mirror), CAF_EXP_THROW(port, server_side_mm.publish(server_side.spawn(fragile_mirror),
0, local_host)); *server_uri));
// client side // client side
CAF_EXP_THROW(mirror, client_side_mm.remote_actor(local_host, port)); auto uri_with_port = io::uri::make(string(host_uri) + ":" + to_string(port));
CAF_REQUIRE(uri_with_port);
CAF_EXP_THROW(mirror, client_side_mm.remote_actor(*uri_with_port));
auto linker = client_side.spawn(linking_actor, mirror); auto linker = client_side.spawn(linking_actor, mirror);
scoped_actor self{client_side}; scoped_actor self{client_side};
self->wait_for(linker); self->wait_for(linker);
......
...@@ -84,13 +84,14 @@ void run_client(int argc, char** argv, uint16_t port) { ...@@ -84,13 +84,14 @@ void run_client(int argc, char** argv, uint16_t port) {
// check whether invalid_argument is thrown // check whether invalid_argument is thrown
// when trying to connect to get an untyped // when trying to connect to get an untyped
// handle to the server // handle to the server
auto res = system.middleman().remote_actor("127.0.0.1", port); auto local_uri = io::uri::make("tcp://127.0.0.1:" + std::to_string(port));
CAF_REQUIRE(local_uri);
auto res = system.middleman().remote_actor(*local_uri);
CAF_REQUIRE(!res); CAF_REQUIRE(!res);
CAF_MESSAGE(system.render(res.error())); CAF_MESSAGE(system.render(res.error()));
CAF_MESSAGE("connect to typed_remote_actor"); CAF_MESSAGE("connect to typed_remote_actor");
CAF_EXP_THROW(serv, CAF_EXP_THROW(serv,
system.middleman().remote_actor<server_type>("127.0.0.1", system.middleman().remote_actor<server_type>(*local_uri));
port));
auto f = make_function_view(serv); auto f = make_function_view(serv);
CAF_CHECK_EQUAL(f(ping{42}), pong{42}); CAF_CHECK_EQUAL(f(ping{42}), pong{42});
anon_send_exit(serv, exit_reason::user_shutdown); anon_send_exit(serv, exit_reason::user_shutdown);
...@@ -103,8 +104,10 @@ void run_server(int argc, char** argv) { ...@@ -103,8 +104,10 @@ void run_server(int argc, char** argv) {
.add_message_type<pong>("pong") .add_message_type<pong>("pong")
.parse(argc, argv); .parse(argc, argv);
actor_system system{cfg}; actor_system system{cfg};
auto server_uri = io::uri::make("tcp://127.0.0.1");
CAF_REQUIRE(server_uri);
CAF_EXP_THROW(port, system.middleman().publish(system.spawn(server), CAF_EXP_THROW(port, system.middleman().publish(system.spawn(server),
0, "127.0.0.1")); *server_uri));
CAF_REQUIRE(port != 0); CAF_REQUIRE(port != 0);
CAF_MESSAGE("running on port " << port << ", start client"); CAF_MESSAGE("running on port " << port << ", start client");
std::thread child{[=] { run_client(argc, argv, port); }}; std::thread child{[=] { run_client(argc, argv, port); }};
......
...@@ -69,12 +69,13 @@ struct fixture { ...@@ -69,12 +69,13 @@ struct fixture {
CAF_CHECK_EQUAL(s_dtor_called.load(), 2); CAF_CHECK_EQUAL(s_dtor_called.load(), 2);
} }
actor remote_actor(const char* hostname, uint16_t port, actor remote_actor(io::uri u, bool expect_fail = false) {
bool expect_fail = false) {
actor result; actor result;
scoped_actor self{system, true}; scoped_actor self{system, true};
auto host = std::string(u.host().first, u.host().second);
auto port = u.port_as_int();
self->request(system.middleman().actor_handle(), infinite, self->request(system.middleman().actor_handle(), infinite,
connect_atom::value, hostname, port).receive( connect_atom::value, host, port).receive(
[&](node_id&, strong_actor_ptr& res, std::set<std::string>& xs) { [&](node_id&, strong_actor_ptr& res, std::set<std::string>& xs) {
CAF_REQUIRE(xs.empty()); CAF_REQUIRE(xs.empty());
if (res) if (res)
...@@ -101,26 +102,31 @@ struct fixture { ...@@ -101,26 +102,31 @@ struct fixture {
CAF_TEST_FIXTURE_SCOPE(unpublish_tests, fixture) CAF_TEST_FIXTURE_SCOPE(unpublish_tests, fixture)
CAF_TEST(unpublishing) { CAF_TEST(unpublishing) {
CAF_EXP_THROW(port, system.middleman().publish(testee, 0)); auto testee_uri = io::uri::make("tcp://0.0.0.0");
CAF_REQUIRE(testee_uri);
CAF_EXP_THROW(port, system.middleman().publish(testee, *testee_uri));
CAF_REQUIRE(port != 0); CAF_REQUIRE(port != 0);
CAF_MESSAGE("published actor on port " << port); CAF_MESSAGE("published actor on port " << port);
CAF_MESSAGE("test invalid unpublish"); CAF_MESSAGE("test invalid unpublish");
auto testee2 = system.spawn<dummy>(); auto testee2 = system.spawn<dummy>();
system.middleman().unpublish(testee2, port); auto testee_uri_with_port = io::uri::make(testee_uri->str() + ":" +
auto x0 = remote_actor("127.0.0.1", port); std::to_string(port));
CAF_REQUIRE(testee_uri_with_port);
system.middleman().unpublish(testee2, *testee_uri_with_port);
auto x0 = remote_actor(*testee_uri_with_port);
CAF_CHECK_NOT_EQUAL(x0, testee2); CAF_CHECK_NOT_EQUAL(x0, testee2);
CAF_CHECK_EQUAL(x0, testee); CAF_CHECK_EQUAL(x0, testee);
anon_send_exit(testee2, exit_reason::kill); anon_send_exit(testee2, exit_reason::kill);
CAF_MESSAGE("unpublish testee"); CAF_MESSAGE("unpublish testee");
system.middleman().unpublish(testee, port); system.middleman().unpublish(testee, port);
CAF_MESSAGE("check whether testee is still available via cache"); CAF_MESSAGE("check whether testee is still available via cache");
auto x1 = remote_actor("127.0.0.1", port); auto x1 = remote_actor(*testee_uri_with_port);
CAF_CHECK_EQUAL(x1, testee); CAF_CHECK_EQUAL(x1, testee);
CAF_MESSAGE("fake dead of testee and check if testee becomes unavailable"); CAF_MESSAGE("fake dead of testee and check if testee becomes unavailable");
anon_send(actor_cast<actor>(system.middleman().actor_handle()), anon_send(actor_cast<actor>(system.middleman().actor_handle()),
down_msg{testee.address(), exit_reason::normal}); down_msg{testee.address(), exit_reason::normal});
// must fail now // must fail now
auto x2 = remote_actor("127.0.0.1", port, true); auto x2 = remote_actor(*testee_uri_with_port, true);
CAF_CHECK(!x2); CAF_CHECK(!x2);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment