Commit 20a3bb15 authored by Dominik Charousset's avatar Dominik Charousset

Extend remote unit test for multiple connections

parent 0a321cf9
...@@ -314,30 +314,46 @@ class server : public event_based_actor { ...@@ -314,30 +314,46 @@ class server : public event_based_actor {
}; };
void test_remote_actor(std::string app_path, bool run_remote_actor) { template <class F>
scoped_actor self; uint16_t at_some_port(uint16_t first_port, F fun) {
auto serv = self->spawn<server, monitored>(); auto port = first_port;
uint16_t port = 4242; for (;;) {
bool success = false;
do {
try { try {
io::publish(serv, port, "127.0.0.1"); fun(port);
auto serv2 = io::remote_actor("127.0.0.1", port); return port;
CAF_CHECK(serv == serv2);
success = true;
CAF_PRINT("running on port " << port);
CAF_LOGF_INFO("running on port " << port);
} }
catch (bind_failure&) { catch (bind_failure&) {
// try next port // try next port
++port; ++port;
} }
} while (!success); }
}
void test_remote_actor(std::string app_path, bool run_remote_actor) {
scoped_actor self;
auto serv = self->spawn<server, monitored>();
auto publish_serv = [=](uint16_t p) {
io::publish(serv, p, "127.0.0.1");
};
auto publish_groups = [](uint16_t p) {
io::publish_local_groups(p);
};
// publish on two distinct ports and use the latter one afterwards
auto port0 = at_some_port(4242, publish_serv);
CAF_LOGF_INFO("first publish succeeded on port " << port0);
auto port = at_some_port(port0 + 1, publish_serv);
CAF_PRINT("running on port " << port);
CAF_LOGF_INFO("running on port " << port);
// publish local groups as well
auto gport = at_some_port(port + 1, publish_groups);
auto serv2 = io::remote_actor("127.0.0.1", port);
CAF_CHECK(serv == serv2);
CAF_TEST(test_remote_actor); CAF_TEST(test_remote_actor);
thread child; thread child;
ostringstream oss; ostringstream oss;
if (run_remote_actor) { if (run_remote_actor) {
oss << app_path << " -c " << port << to_dev_null; oss << app_path << " -c " << port << " " << port0
<< " " << gport << to_dev_null;
// execute client_part() in a separate process, // execute client_part() in a separate process,
// connected via localhost socket // connected via localhost socket
child = thread([&oss]() { child = thread([&oss]() {
...@@ -372,18 +388,20 @@ int main(int argc, char** argv) { ...@@ -372,18 +388,20 @@ int main(int argc, char** argv) {
cout << "this node is: " << to_string(caf::detail::singletons::get_node_id()) cout << "this node is: " << to_string(caf::detail::singletons::get_node_id())
<< endl; << endl;
message_builder{argv + 1, argv + argc}.apply({ message_builder{argv + 1, argv + argc}.apply({
on("-c", spro<uint16_t>)>> [](uint16_t port) { on("-c", spro<uint16_t>, spro<uint16_t>, spro<uint16_t>)
>> [](uint16_t p1, uint16_t p2, uint16_t gport) {
CAF_LOGF_INFO("run in client mode"); CAF_LOGF_INFO("run in client mode");
scoped_actor self; scoped_actor self;
auto serv = io::remote_actor("localhost", port); auto serv = io::remote_actor("localhost", p1);
auto serv2 = io::remote_actor("localhost", p2);
// remote_actor is supposed to return the same server // remote_actor is supposed to return the same server
// when connecting to the same host again // when connecting to the same host again
{ {
auto server2 = io::remote_actor("localhost", port); CAF_CHECK(serv == io::remote_actor("localhost", p1));
CAF_CHECK(serv == server2); CAF_CHECK(serv2 == io::remote_actor("127.0.0.1", p2));
auto server3 = io::remote_actor("127.0.0.1", port);
CAF_CHECK(serv == server3);
} }
// connect to published groups
io::remote_group("whatever", "127.0.0.1", gport);
auto c = self->spawn<client, monitored>(serv); auto c = self->spawn<client, monitored>(serv);
self->receive( self->receive(
[&](const down_msg& dm) { [&](const down_msg& dm) {
...@@ -391,13 +409,13 @@ int main(int argc, char** argv) { ...@@ -391,13 +409,13 @@ int main(int argc, char** argv) {
CAF_CHECK_EQUAL(dm.reason, exit_reason::normal); CAF_CHECK_EQUAL(dm.reason, exit_reason::normal);
} }
); );
}, },
on("-s") >> [&] { on("-s") >> [&] {
CAF_PRINT("don't run remote actor (server mode)"); CAF_PRINT("don't run remote actor (server mode)");
test_remote_actor(argv[0], false); test_remote_actor(argv[0], false);
}, },
on() >> [&] { test_remote_actor(argv[0], true); }, others() >> [&] { on() >> [&] { test_remote_actor(argv[0], true); }, others() >> [&] {
CAF_PRINTERR("usage: " << argv[0] << " [-s|-c PORT]"); CAF_PRINTERR("usage: " << argv[0] << " [-s PORT|-c PORT1 PORT2 GROUP_PORT]");
} }
}); });
await_all_actors_done(); await_all_actors_done();
......
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