Commit 175c195a authored by Dominik Charousset's avatar Dominik Charousset

Remove CAF_EXP_THROW and simplify CAF_RAISE_ERROR

The old `CAF_EXP_THROW` is obsolete because of the much nicer `unbox`
utility in the testing DSL. Further, `CAF_RAISE_ERROR` can simply take a
C string instead of doing the `std::string` conversions.
parent a2873264
......@@ -262,7 +262,7 @@
#ifdef CAF_NO_EXCEPTIONS
# define CAF_RAISE_ERROR(msg) \
do { std::string str = msg; CAF_CRITICAL(str.c_str()); } while (true)
CAF_CRITICAL(msg)
#else // CAF_NO_EXCEPTIONS
# define CAF_RAISE_ERROR(msg) \
throw std::runtime_error(msg)
......
......@@ -54,8 +54,6 @@ public:
explicit deserializer(execution_unit* x = nullptr);
};
#ifndef CAF_NO_EXCEPTIONS
template <class T>
typename std::enable_if<
std::is_same<
......@@ -66,7 +64,7 @@ typename std::enable_if<
operator&(deserializer& source, T& x) {
auto e = source.apply(x);
if (e)
CAF_RAISE_ERROR(to_string(e));
CAF_RAISE_ERROR("error during serialization (using operator&)");
}
template <class T>
......@@ -82,7 +80,5 @@ operator>>(deserializer& source, T& x) {
return source;
}
#endif // CAF_NO_EXCEPTIONS
} // namespace caf
......@@ -231,11 +231,7 @@ public:
mapped_type& at(const K& key) {
auto i = find(key);
if (i == end())
#ifdef CAF_NO_EXCEPTIONS
CAF_CRITICAL("caf::detail::unordered_flat_map::at out of range");
#else
throw std::out_of_range{"caf::detail::unordered_flat_map::at"};
#endif
CAF_RAISE_ERROR("caf::detail::unordered_flat_map::at out of range");
return i->second;
}
......
......@@ -443,18 +443,6 @@ inline std::string to_string(const expected<void>& x) {
return "!" + to_string(x.error());
}
/// @cond PRIVATE
/// Assigns the value of `expr` (which must return an `expected`)
/// to a new variable named `var` or throws a `std::runtime_error` on error.
/// @relates expected
/// @experimental
#define CAF_EXP_THROW(var, expr) \
auto CAF_UNIFYN(tmp_var_) = expr; \
if (!CAF_UNIFYN(tmp_var_)) \
CAF_RAISE_ERROR(to_string(CAF_UNIFYN(tmp_var_).error())); \
auto& var = *CAF_UNIFYN(tmp_var_)
/// @endcond
} // namespace caf
namespace std {
......
......@@ -53,8 +53,6 @@ public:
~serializer() override;
};
#ifndef CAF_NO_EXCEPTIONS
template <class T>
typename std::enable_if<
std::is_same<
......@@ -66,7 +64,7 @@ operator&(serializer& sink, const T& x) {
// implementations are required to never modify `x` while saving
auto e = sink.apply(const_cast<T&>(x));
if (e)
CAF_RAISE_ERROR(to_string(e));
CAF_RAISE_ERROR("error during serialization (using operator&)");
}
template <class T>
......@@ -82,7 +80,5 @@ operator<<(serializer& sink, const T& x) {
return sink;
}
#endif // CAF_NO_EXCEPTIONS
} // namespace caf
......@@ -19,7 +19,7 @@
#include "caf/config.hpp"
#define CAF_SUITE io_dynamic_broker
#include "caf/test/unit_test.hpp"
#include "caf/test/dsl.hpp"
#include <memory>
#include <iostream>
......@@ -151,8 +151,8 @@ void run_client(int argc, char** argv, uint16_t port) {
actor_system system{cfg.load<io::middleman>().parse(argc, argv)};
auto p = system.spawn(ping, size_t{10});
CAF_MESSAGE("spawn_client...");
CAF_EXP_THROW(cl, system.middleman().spawn_client(peer_fun, "127.0.0.1",
port, p));
auto cl = unbox(system.middleman().spawn_client(peer_fun,
"127.0.0.1", port, p));
CAF_MESSAGE("spawn_client finished");
anon_send(p, kickoff_atom::value, cl);
CAF_MESSAGE("`kickoff_atom` has been send");
......
......@@ -19,7 +19,7 @@
#include "caf/config.hpp"
#define CAF_SUITE io_dynamic_remote_actor_tcp
#include "caf/test/unit_test.hpp"
#include "caf/test/dsl.hpp"
#include <vector>
#include <sstream>
......@@ -145,14 +145,14 @@ CAF_TEST_FIXTURE_SCOPE(dynamic_remote_actor_tests, fixture)
CAF_TEST(identity_semantics_tcp) {
// server side
auto server = server_side.spawn(make_pong_behavior);
CAF_EXP_THROW(port1, server_side_mm.publish(server, 0, local_host));
CAF_EXP_THROW(port2, server_side_mm.publish(server, 0, local_host));
auto port1 = unbox(server_side_mm.publish(server, 0, local_host));
auto port2 = unbox(server_side_mm.publish(server, 0, local_host));
CAF_REQUIRE_NOT_EQUAL(port1, port2);
CAF_EXP_THROW(same_server, server_side_mm.remote_actor(local_host, port2));
auto same_server = unbox(server_side_mm.remote_actor(local_host, port2));
CAF_REQUIRE_EQUAL(same_server, server);
CAF_CHECK_EQUAL(same_server->node(), server_side.node());
CAF_EXP_THROW(server1, client_side_mm.remote_actor(local_host, port1));
CAF_EXP_THROW(server2, client_side_mm.remote_actor(local_host, port2));
auto server1 = unbox(client_side_mm.remote_actor(local_host, port1));
auto server2 = unbox(client_side_mm.remote_actor(local_host, port2));
CAF_CHECK_EQUAL(server1, client_side_mm.remote_actor(local_host, port1));
CAF_CHECK_EQUAL(server2, client_side_mm.remote_actor(local_host, port2));
anon_send_exit(server, exit_reason::user_shutdown);
......@@ -160,29 +160,28 @@ CAF_TEST(identity_semantics_tcp) {
CAF_TEST(ping_pong_tcp) {
// server side
CAF_EXP_THROW(port,
server_side_mm.publish(server_side.spawn(make_pong_behavior),
0, local_host));
auto port = unbox(server_side_mm.publish(
server_side.spawn(make_pong_behavior), 0, local_host));
// client side
CAF_EXP_THROW(pong, client_side_mm.remote_actor(local_host, port));
auto pong = unbox(client_side_mm.remote_actor(local_host, port));
client_side.spawn(make_ping_behavior, pong);
}
CAF_TEST(custom_message_type_tcp) {
// server side
CAF_EXP_THROW(port, server_side_mm.publish(server_side.spawn(make_sort_behavior),
0, local_host));
auto port = unbox(server_side_mm.publish(
server_side.spawn(make_sort_behavior), 0, local_host));
// client side
CAF_EXP_THROW(sorter, client_side_mm.remote_actor(local_host, port));
auto sorter = unbox(client_side_mm.remote_actor(local_host, port));
client_side.spawn(make_sort_requester_behavior, sorter);
}
CAF_TEST(remote_link_tcp) {
// server side
CAF_EXP_THROW(port, server_side_mm.publish(server_side.spawn(fragile_mirror),
0, local_host));
auto port = unbox(
server_side_mm.publish(server_side.spawn(fragile_mirror), 0, local_host));
// client side
CAF_EXP_THROW(mirror, client_side_mm.remote_actor(local_host, port));
auto mirror = unbox(client_side_mm.remote_actor(local_host, port));
auto linker = client_side.spawn(linking_actor, mirror);
scoped_actor self{client_side};
self->wait_for(linker);
......
......@@ -19,7 +19,7 @@
#include "caf/config.hpp"
#define CAF_SUITE io_dynamic_remote_actor_udp
#include "caf/test/unit_test.hpp"
#include "caf/test/dsl.hpp"
#include <vector>
#include <sstream>
......@@ -146,14 +146,14 @@ CAF_TEST_FIXTURE_SCOPE(dynamic_remote_actor_tests_udp, fixture)
CAF_TEST(identity_semantics_udp) {
// server side
auto server = server_side.spawn(make_pong_behavior);
CAF_EXP_THROW(port1, server_side_mm.publish_udp(server, 0, local_host));
CAF_EXP_THROW(port2, server_side_mm.publish_udp(server, 0, local_host));
auto port1 = unbox(server_side_mm.publish_udp(server, 0, local_host));
auto port2 = unbox(server_side_mm.publish_udp(server, 0, local_host));
CAF_REQUIRE_NOT_EQUAL(port1, port2);
CAF_EXP_THROW(same_server, server_side_mm.remote_actor_udp(local_host, port2));
auto same_server = unbox(server_side_mm.remote_actor_udp(local_host, port2));
CAF_REQUIRE_EQUAL(same_server, server);
CAF_CHECK_EQUAL(same_server->node(), server_side.node());
CAF_EXP_THROW(server1, client_side_mm.remote_actor_udp(local_host, port1));
CAF_EXP_THROW(server2, client_side_mm.remote_actor_udp(local_host, port2));
auto server1 = unbox(client_side_mm.remote_actor_udp(local_host, port1));
auto server2 = unbox(client_side_mm.remote_actor_udp(local_host, port2));
CAF_CHECK_EQUAL(server1, client_side_mm.remote_actor_udp(local_host, port1));
CAF_CHECK_EQUAL(server2, client_side_mm.remote_actor_udp(local_host, port2));
anon_send_exit(server, exit_reason::user_shutdown);
......@@ -161,31 +161,28 @@ CAF_TEST(identity_semantics_udp) {
CAF_TEST(ping_pong_udp) {
// server side
CAF_EXP_THROW(port,
server_side_mm.publish_udp(server_side.spawn(make_pong_behavior),
0, local_host));
auto port = unbox(server_side_mm.publish_udp(
server_side.spawn(make_pong_behavior), 0, local_host));
// client side
CAF_EXP_THROW(pong, client_side_mm.remote_actor_udp(local_host, port));
auto pong = unbox(client_side_mm.remote_actor_udp(local_host, port));
client_side.spawn(make_ping_behavior, pong);
}
CAF_TEST(custom_message_type_udp) {
// server side
CAF_EXP_THROW(port,
server_side_mm.publish_udp(server_side.spawn(make_sort_behavior),
0, local_host));
auto port = unbox(server_side_mm.publish_udp(
server_side.spawn(make_sort_behavior), 0, local_host));
// client side
CAF_EXP_THROW(sorter, client_side_mm.remote_actor_udp(local_host, port));
auto sorter = unbox(client_side_mm.remote_actor_udp(local_host, port));
client_side.spawn(make_sort_requester_behavior, sorter);
}
CAF_TEST(remote_link_udp) {
// server side
CAF_EXP_THROW(port,
server_side_mm.publish_udp(server_side.spawn(fragile_mirror),
0, local_host));
auto port = unbox(server_side_mm.publish_udp(
server_side.spawn(fragile_mirror), 0, local_host));
// client side
CAF_EXP_THROW(mirror, client_side_mm.remote_actor_udp(local_host, port));
auto mirror = unbox(client_side_mm.remote_actor_udp(local_host, port));
auto linker = client_side.spawn(linking_actor, mirror);
scoped_actor self{client_side};
self->wait_for(linker);
......
......@@ -19,7 +19,7 @@
#include "caf/config.hpp"
#define CAF_SUITE io_remote_spawn
#include "caf/test/unit_test.hpp"
#include "caf/test/dsl.hpp"
#include <thread>
#include <string>
......@@ -96,7 +96,7 @@ void run_client(int argc, char** argv, uint16_t port) {
void run_server(int argc, char** argv) {
config cfg{argc, argv};
actor_system system{cfg};
CAF_EXP_THROW(port, system.middleman().open(0));
auto port = unbox(system.middleman().open(0));
std::thread child{[=] { run_client(argc, argv, port); }};
child.join();
}
......
......@@ -19,7 +19,7 @@
#include "caf/config.hpp"
#define CAF_SUITE io_typed_broker
#include "caf/test/unit_test.hpp"
#include "caf/test/dsl.hpp"
#include <memory>
#include <iostream>
......@@ -167,8 +167,8 @@ void run_client(int argc, char** argv, uint16_t port) {
actor_system system{cfg.load<io::middleman>().parse(argc, argv)};
auto p = system.spawn(ping, size_t{10});
CAF_MESSAGE("spawn_client_typed...");
CAF_EXP_THROW(cl, system.middleman().spawn_client(peer_fun, "localhost",
port, p));
auto cl = unbox(system.middleman().spawn_client(peer_fun,
"localhost", port, p));
CAF_MESSAGE("spawn_client_typed finished");
anon_send(p, kickoff_atom::value, cl);
CAF_MESSAGE("`kickoff_atom` has been send");
......
......@@ -79,17 +79,16 @@ void run_client(int argc, char** argv, uint16_t port) {
.add_message_type<ping>("ping")
.add_message_type<pong>("pong")
.parse(argc, argv);
actor_system system{cfg};
actor_system sys{cfg};
// check whether invalid_argument is thrown
// when trying to connect to get an untyped
// handle to the server
auto res = system.middleman().remote_actor("127.0.0.1", port);
auto res = sys.middleman().remote_actor("127.0.0.1", port);
CAF_REQUIRE(!res);
CAF_MESSAGE(system.render(res.error()));
CAF_MESSAGE(sys.render(res.error()));
CAF_MESSAGE("connect to typed_remote_actor");
CAF_EXP_THROW(serv,
system.middleman().remote_actor<server_type>("127.0.0.1",
port));
auto serv = unbox(sys.middleman().remote_actor<server_type>("127.0.0.1",
port));
auto f = make_function_view(serv);
CAF_CHECK_EQUAL(f(ping{42}), pong{42});
anon_send_exit(serv, exit_reason::user_shutdown);
......@@ -101,9 +100,8 @@ void run_server(int argc, char** argv) {
.add_message_type<ping>("ping")
.add_message_type<pong>("pong")
.parse(argc, argv);
actor_system system{cfg};
CAF_EXP_THROW(port, system.middleman().publish(system.spawn(server),
0, "127.0.0.1"));
actor_system sys{cfg};
auto port = unbox(sys.middleman().publish(sys.spawn(server), 0, "127.0.0.1"));
CAF_REQUIRE(port != 0);
CAF_MESSAGE("running on port " << port << ", start client");
std::thread child{[=] { run_client(argc, argv, port); }};
......
......@@ -19,7 +19,7 @@
#include "caf/config.hpp"
#define CAF_SUITE io_unpublish
#include "caf/test/unit_test.hpp"
#include "caf/test/dsl.hpp"
#include <new>
#include <thread>
......@@ -100,7 +100,7 @@ struct fixture {
CAF_TEST_FIXTURE_SCOPE(unpublish_tests, fixture)
CAF_TEST(unpublishing) {
CAF_EXP_THROW(port, system.middleman().publish(testee, 0));
auto port = unbox(system.middleman().publish(testee, 0));
CAF_REQUIRE(port != 0);
CAF_MESSAGE("published actor on port " << port);
CAF_MESSAGE("test invalid unpublish");
......
......@@ -19,7 +19,7 @@
#include "caf/config.hpp"
#define CAF_SUITE openssl_authentication
#include "caf/test/unit_test.hpp"
#include "caf/test/dsl.hpp"
#ifndef CAF_WINDOWS
# include <unistd.h>
......@@ -206,12 +206,12 @@ CAF_TEST(authentication_success) {
exec_loop();
CAF_MESSAGE("publish pong");
loop_after_next_enqueue(server_side);
CAF_EXP_THROW(port, publish(spong, 0, local_host));
auto port = unbox(publish(spong, 0, local_host));
exec_loop();
// client side
CAF_MESSAGE("connect to pong via port " << port);
loop_after_next_enqueue(client_side);
CAF_EXP_THROW(pong, remote_actor(client_side, local_host, port));
auto pong = unbox(remote_actor(client_side, local_host, port));
CAF_MESSAGE("spawn ping and exchange messages");
auto sping = client_side.spawn(make_ping_behavior, pong);
while (!terminated(sping))
......@@ -230,7 +230,7 @@ CAF_TEST(authentication_failure) {
exec_loop();
loop_after_next_enqueue(server_side);
CAF_MESSAGE("publish pong");
CAF_EXP_THROW(port, publish(spong, 0, local_host));
auto port = unbox(publish(spong, 0, local_host));
exec_loop();
// client side
CAF_MESSAGE("connect to pong via port " << port);
......
......@@ -21,7 +21,7 @@
#include <signal.h>
#define CAF_SUITE openssl_dynamic_remote_actor
#include "caf/test/unit_test.hpp"
#include "caf/test/dsl.hpp"
#include <vector>
#include <sstream>
......@@ -151,14 +151,14 @@ using openssl::publish;
CAF_TEST(identity_semantics) {
// server side
auto server = server_side.spawn(make_pong_behavior);
CAF_EXP_THROW(port1, publish(server, 0, local_host));
CAF_EXP_THROW(port2, publish(server, 0, local_host));
auto port1 = unbox(publish(server, 0, local_host));
auto port2 = unbox(publish(server, 0, local_host));
CAF_REQUIRE_NOT_EQUAL(port1, port2);
CAF_EXP_THROW(same_server, remote_actor(server_side, local_host, port2));
auto same_server = unbox(remote_actor(server_side, local_host, port2));
CAF_REQUIRE_EQUAL(same_server, server);
CAF_CHECK_EQUAL(same_server->node(), server_side.node());
CAF_EXP_THROW(server1, remote_actor(client_side, local_host, port1));
CAF_EXP_THROW(server2, remote_actor(client_side, local_host, port2));
auto server1 = unbox(remote_actor(client_side, local_host, port1));
auto server2 = unbox(remote_actor(client_side, local_host, port2));
CAF_CHECK_EQUAL(server1, remote_actor(client_side, local_host, port1));
CAF_CHECK_EQUAL(server2, remote_actor(client_side, local_host, port2));
anon_send_exit(server, exit_reason::user_shutdown);
......@@ -166,28 +166,27 @@ CAF_TEST(identity_semantics) {
CAF_TEST(ping_pong) {
// server side
CAF_EXP_THROW(port,
publish(server_side.spawn(make_pong_behavior), 0, local_host));
auto port = unbox(publish(server_side.spawn(make_pong_behavior),
0, local_host));
// client side
CAF_EXP_THROW(pong, remote_actor(client_side, local_host, port));
auto pong = unbox(remote_actor(client_side, local_host, port));
client_side.spawn(make_ping_behavior, pong);
}
CAF_TEST(custom_message_type) {
// server side
CAF_EXP_THROW(port,
publish(server_side.spawn(make_sort_behavior), 0, local_host));
auto port = unbox(publish(server_side.spawn(make_sort_behavior),
0, local_host));
// client side
CAF_EXP_THROW(sorter, remote_actor(client_side, local_host, port));
auto sorter = unbox(remote_actor(client_side, local_host, port));
client_side.spawn(make_sort_requester_behavior, sorter);
}
CAF_TEST(remote_link) {
// server side
CAF_EXP_THROW(port,
publish(server_side.spawn(fragile_mirror), 0, local_host));
auto port = unbox(publish(server_side.spawn(fragile_mirror), 0, local_host));
// client side
CAF_EXP_THROW(mirror, remote_actor(client_side, local_host, port));
auto mirror = unbox(remote_actor(client_side, local_host, port));
auto linker = client_side.spawn(linking_actor, mirror);
scoped_actor self{client_side};
self->wait_for(linker);
......
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