Commit ec1763af authored by Dominik Charousset's avatar Dominik Charousset

Use new atom constant syntax in (most) unit tests

parent a6cb56a0
...@@ -15,38 +15,40 @@ namespace { ...@@ -15,38 +15,40 @@ namespace {
size_t s_pongs = 0; size_t s_pongs = 0;
behavior ping_behavior(local_actor* self, size_t num_pings) { behavior ping_behavior(local_actor* self, size_t num_pings) {
return (on(atom("pong"), arg_match) >> [=](int value)->message { return {
if (!self->last_sender()) { [=](pong_atom, int value) -> message {
CAF_PRINT("last_sender() invalid!"); if (!self->last_sender()) {
} CAF_PRINT("last_sender() invalid!");
CAF_PRINT("received {'pong', " << value << "}"); }
// cout << to_string(self->last_dequeued()) << endl; CAF_PRINT("received {'pong', " << value << "}");
if (++s_pongs >= num_pings) { // cout << to_string(self->last_dequeued()) << endl;
CAF_PRINT("reached maximum, send {'EXIT', user_defined} " if (++s_pongs >= num_pings) {
<< "to last sender and quit with normal reason"); CAF_PRINT("reached maximum, send {'EXIT', user_defined} "
self->send_exit(self->last_sender(), << "to last sender and quit with normal reason");
exit_reason::user_shutdown); self->send_exit(self->last_sender(),
self->quit(); exit_reason::user_shutdown);
} self->quit();
return make_message(atom("ping"), value); }
}, return make_message(ping_atom::value, value);
others() >> [=] { },
CAF_LOGF_ERROR("unexpected message; " others() >> [=] {
<< to_string(self->last_dequeued())); CAF_LOGF_ERROR("unexpected; " << to_string(self->last_dequeued()));
self->quit(exit_reason::user_shutdown); self->quit(exit_reason::user_shutdown);
}); }
};
} }
behavior pong_behavior(local_actor* self) { behavior pong_behavior(local_actor* self) {
return (on(atom("ping"), arg_match) >> [](int value)->message { return {
CAF_PRINT("received {'ping', " << value << "}"); [](ping_atom, int value) -> message {
return make_message(atom("pong"), value + 1); CAF_PRINT("received {'ping', " << value << "}");
}, return make_message(pong_atom::value, value + 1);
others() >> [=] { },
CAF_LOGF_ERROR("unexpected message; " others() >> [=] {
<< to_string(self->last_dequeued())); CAF_LOGF_ERROR("unexpected; " << to_string(self->last_dequeued()));
self->quit(exit_reason::user_shutdown); self->quit(exit_reason::user_shutdown);
}); }
};
} }
} // namespace <anonymous> } // namespace <anonymous>
...@@ -67,13 +69,13 @@ void event_based_ping(event_based_actor* self, size_t num_pings) { ...@@ -67,13 +69,13 @@ void event_based_ping(event_based_actor* self, size_t num_pings) {
void pong(blocking_actor* self, actor ping_actor) { void pong(blocking_actor* self, actor ping_actor) {
CAF_LOGF_TRACE("ping_actor = " << to_string(ping_actor)); CAF_LOGF_TRACE("ping_actor = " << to_string(ping_actor));
self->send(ping_actor, atom("pong"), 0); // kickoff self->send(ping_actor, pong_atom::value, 0); // kickoff
self->receive_loop(pong_behavior(self)); self->receive_loop(pong_behavior(self));
} }
void event_based_pong(event_based_actor* self, actor ping_actor) { void event_based_pong(event_based_actor* self, actor ping_actor) {
CAF_LOGF_TRACE("ping_actor = " << to_string(ping_actor)); CAF_LOGF_TRACE("ping_actor = " << to_string(ping_actor));
CAF_REQUIRE(ping_actor != invalid_actor); CAF_REQUIRE(ping_actor != invalid_actor);
self->send(ping_actor, atom("pong"), 0); // kickoff self->send(ping_actor, pong_atom::value, 0); // kickoff
self->become(pong_behavior(self)); self->become(pong_behavior(self));
} }
...@@ -226,4 +226,10 @@ std::thread run_program(caf::actor listener, const char* path, Ts&&... args) { ...@@ -226,4 +226,10 @@ std::thread run_program(caf::actor listener, const char* path, Ts&&... args) {
return run_program_impl(listener, path, std::move(vec)); return run_program_impl(listener, path, std::move(vec));
} }
using ping_atom = caf::atom_constant<caf::atom("ping")>;
using pong_atom = caf::atom_constant<caf::atom("pong")>;
using check_atom = caf::atom_constant<caf::atom("check")>;
using passed_atom = caf::atom_constant<caf::atom("passed")>;
using kickoff_atom = caf::atom_constant<caf::atom("kickoff")>;
#endif // TEST_HPP #endif // TEST_HPP
...@@ -55,9 +55,10 @@ behavior tester(event_based_actor* self, const actor& aut) { ...@@ -55,9 +55,10 @@ behavior tester(event_based_actor* self, const actor& aut) {
// another worker thread; by waiting some milliseconds, we make sure // another worker thread; by waiting some milliseconds, we make sure
// testee had enough time to return control to the scheduler // testee had enough time to return control to the scheduler
// which in turn destroys it by dropping the last remaining reference // which in turn destroys it by dropping the last remaining reference
self->delayed_send(self, std::chrono::milliseconds(30), atom("check")); self->delayed_send(self, std::chrono::milliseconds(30),
check_atom::value);
}, },
on(atom("check")) >> [self] { [self](check_atom) {
// make sure dude's dtor has been called // make sure dude's dtor has been called
CAF_CHECK_EQUAL(s_testees.load(), 0); CAF_CHECK_EQUAL(s_testees.load(), 0);
self->quit(); self->quit();
......
...@@ -36,6 +36,7 @@ testee::behavior_type testee_impl(testee::pointer self) { ...@@ -36,6 +36,7 @@ testee::behavior_type testee_impl(testee::pointer self) {
} }
void test_typed_atom_interface() { void test_typed_atom_interface() {
CAF_CHECKPOINT();
scoped_actor self; scoped_actor self;
auto tst = spawn_typed(testee_impl); auto tst = spawn_typed(testee_impl);
self->sync_send(tst, abc_atom()).await( self->sync_send(tst, abc_atom()).await(
...@@ -105,7 +106,7 @@ int main() { ...@@ -105,7 +106,7 @@ int main() {
after(std::chrono::seconds(0)) >> CAF_UNEXPECTED_TOUT_CB() after(std::chrono::seconds(0)) >> CAF_UNEXPECTED_TOUT_CB()
); );
atom_value x = atom("abc"); atom_value x = atom("abc");
atom_value y = abc_atom(); atom_value y = abc_atom::value;
CAF_CHECK_EQUAL(x, y); CAF_CHECK_EQUAL(x, y);
auto msg = make_message(abc_atom()); auto msg = make_message(abc_atom());
self->send(self, msg); self->send(self, msg);
......
...@@ -34,18 +34,17 @@ void ping(event_based_actor* self, size_t num_pings) { ...@@ -34,18 +34,17 @@ void ping(event_based_actor* self, size_t num_pings) {
CAF_PRINT("num_pings: " << num_pings); CAF_PRINT("num_pings: " << num_pings);
auto count = std::make_shared<size_t>(0); auto count = std::make_shared<size_t>(0);
self->become( self->become(
on(atom("kickoff"), arg_match) >> [=](const actor& pong) { [=](kickoff_atom, const actor& pong) {
CAF_CHECKPOINT(); CAF_CHECKPOINT();
self->send(pong, atom("ping"), 1); self->send(pong, ping_atom::value, 1);
self->become( self->become(
on(atom("pong"), arg_match) >> [=](pong_atom, int value)->std::tuple<atom_value, int> {
[=](int value)->std::tuple<atom_value, int> {
if (++*count >= num_pings) { if (++*count >= num_pings) {
CAF_PRINT("received " << num_pings CAF_PRINT("received " << num_pings
<< " pings, call self->quit"); << " pings, call self->quit");
self->quit(); self->quit();
} }
return std::make_tuple(atom("ping"), value + 1); return std::make_tuple(ping_atom::value, value + 1);
}, },
others() >> CAF_UNEXPECTED_MSG_CB(self)); others() >> CAF_UNEXPECTED_MSG_CB(self));
}, },
...@@ -56,14 +55,13 @@ void ping(event_based_actor* self, size_t num_pings) { ...@@ -56,14 +55,13 @@ void ping(event_based_actor* self, size_t num_pings) {
void pong(event_based_actor* self) { void pong(event_based_actor* self) {
CAF_CHECKPOINT(); CAF_CHECKPOINT();
self->become( self->become(
on(atom("ping"), arg_match) >> [=](int value) [=](ping_atom, int value) -> std::tuple<atom_value, int> {
->std::tuple<atom_value, int> {
CAF_CHECKPOINT(); CAF_CHECKPOINT();
self->monitor(self->last_sender()); self->monitor(self->last_sender());
// set next behavior // set next behavior
self->become( self->become(
on(atom("ping"), arg_match) >> [](int val) { [](ping_atom, int val) {
return std::make_tuple(atom("pong"), val); return std::make_tuple(pong_atom::value, val);
}, },
[=](const down_msg& dm) { [=](const down_msg& dm) {
CAF_PRINT("received down_msg{" << dm.reason << "}"); CAF_PRINT("received down_msg{" << dm.reason << "}");
...@@ -72,7 +70,7 @@ void pong(event_based_actor* self) { ...@@ -72,7 +70,7 @@ void pong(event_based_actor* self) {
others() >> CAF_UNEXPECTED_MSG_CB(self) others() >> CAF_UNEXPECTED_MSG_CB(self)
); );
// reply to 'ping' // reply to 'ping'
return std::make_tuple(atom("pong"), value); return std::make_tuple(pong_atom::value, value);
}, },
others() >> CAF_UNEXPECTED_MSG_CB(self)); others() >> CAF_UNEXPECTED_MSG_CB(self));
} }
...@@ -113,17 +111,19 @@ void peer_fun(broker* self, connection_handle hdl, const actor& buddy) { ...@@ -113,17 +111,19 @@ void peer_fun(broker* self, connection_handle hdl, const actor& buddy) {
memcpy(&value, msg.buf.data() + sizeof(atom_value), sizeof(int)); memcpy(&value, msg.buf.data() + sizeof(atom_value), sizeof(int));
self->send(buddy, type, value); self->send(buddy, type, value);
}, },
on(atom("ping"), arg_match) >> [=](int value) { [=](ping_atom, int value) {
CAF_PRINT("received ping{" << value << "}"); CAF_PRINT("received ping{" << value << "}");
write(atom("ping"), value); write(ping_atom::value, value);
}, },
on(atom("pong"), arg_match) >> [=](int value) { [=](pong_atom, int value) {
CAF_PRINT("received pong{" << value << "}"); CAF_PRINT("received pong{" << value << "}");
write(atom("pong"), value); write(pong_atom::value, value);
}, },
[=](const down_msg& dm) { [=](const down_msg& dm) {
CAF_PRINT("received down_msg"); CAF_PRINT("received down_msg");
if (dm.source == buddy) self->quit(dm.reason); if (dm.source == buddy) {
self->quit(dm.reason);
}
}, },
others() >> CAF_UNEXPECTED_MSG_CB(self) others() >> CAF_UNEXPECTED_MSG_CB(self)
); );
...@@ -177,7 +177,7 @@ int main(int argc, char** argv) { ...@@ -177,7 +177,7 @@ int main(int argc, char** argv) {
CAF_CHECKPOINT(); CAF_CHECKPOINT();
auto cl = spawn_io_client(peer_fun, "localhost", port, p); auto cl = spawn_io_client(peer_fun, "localhost", port, p);
CAF_CHECKPOINT(); CAF_CHECKPOINT();
anon_send(p, atom("kickoff"), cl); anon_send(p, kickoff_atom::value, cl);
CAF_CHECKPOINT(); CAF_CHECKPOINT();
}, },
on("-s") >> [&] { on("-s") >> [&] {
......
...@@ -7,38 +7,47 @@ ...@@ -7,38 +7,47 @@
using namespace caf; using namespace caf;
using pop_atom = atom_constant<atom("pop")>;
using push_atom = atom_constant<atom("push")>;
class fixed_stack : public sb_actor<fixed_stack> { class fixed_stack : public sb_actor<fixed_stack> {
public: public:
friend class sb_actor<fixed_stack>; friend class sb_actor<fixed_stack>;
fixed_stack(size_t max) : max_size(max) { fixed_stack(size_t max) : max_size(max) {
full = ( full.assign(
on(atom("push"), arg_match) >> [=](int) { /* discard */ }, [=](push_atom, int) {
on(atom("pop")) >> [=]() -> message { /* discard */
},
[=](pop_atom) -> message {
auto result = data.back(); auto result = data.back();
data.pop_back(); data.pop_back();
become(filled); become(filled);
return make_message(atom("ok"), result); return make_message(ok_atom::value, result);
} }
); );
filled = ( filled.assign(
on(atom("push"), arg_match) >> [=](int what) { [=](push_atom, int what) {
data.push_back(what); data.push_back(what);
if (data.size() == max_size) become(full); if (data.size() == max_size) {
become(full);
}
}, },
on(atom("pop")) >> [=]() -> message { [=](pop_atom) -> message {
auto result = data.back(); auto result = data.back();
data.pop_back(); data.pop_back();
if (data.empty()) become(empty); if (data.empty()) {
return make_message(atom("ok"), result); become(empty);
}
return make_message(ok_atom::value, result);
} }
); );
empty = ( empty.assign(
on(atom("push"), arg_match) >> [=](int what) { [=](push_atom, int what) {
data.push_back(what); data.push_back(what);
become(filled); become(filled);
}, },
on(atom("pop")) >> [=] { [=](pop_atom) {
return atom("failure"); return error_atom::value;
} }
); );
} }
...@@ -58,19 +67,20 @@ fixed_stack::~fixed_stack() { ...@@ -58,19 +67,20 @@ fixed_stack::~fixed_stack() {
// avoid weak-vtables warning // avoid weak-vtables warning
} }
void test_fixed_stack_actor() { void test_fixed_stack_actor() {
scoped_actor self; scoped_actor self;
auto st = spawn<fixed_stack>(size_t{10}); auto st = spawn<fixed_stack>(size_t{10});
// push 20 values // push 20 values
for (int i = 0; i < 20; ++i) self->send(st, atom("push"), i); for (int i = 0; i < 20; ++i) self->send(st, push_atom::value, i);
// pop 20 times // pop 20 times
for (int i = 0; i < 20; ++i) self->send(st, atom("pop")); for (int i = 0; i < 20; ++i) self->send(st, pop_atom::value);
// expect 10 failure messages // expect 10 failure messages
{ {
int i = 0; int i = 0;
self->receive_for(i, 10) ( self->receive_for(i, 10) (
on(atom("failure")) >> CAF_CHECKPOINT_CB() [](error_atom) {
CAF_CHECKPOINT();
}
); );
CAF_CHECKPOINT(); CAF_CHECKPOINT();
} }
...@@ -79,7 +89,7 @@ void test_fixed_stack_actor() { ...@@ -79,7 +89,7 @@ void test_fixed_stack_actor() {
std::vector<int> values; std::vector<int> values;
int i = 0; int i = 0;
self->receive_for(i, 10) ( self->receive_for(i, 10) (
on(atom("ok"), arg_match) >> [&](int value) { [&](ok_atom, int value) {
values.push_back(value); values.push_back(value);
} }
); );
......
...@@ -4,26 +4,29 @@ ...@@ -4,26 +4,29 @@
using namespace caf; using namespace caf;
using msg_atom = atom_constant<atom("msg")>;
using timeout_atom = atom_constant<atom("timeout")>;
void testee(event_based_actor* self) { void testee(event_based_actor* self) {
auto counter = std::make_shared<int>(0); auto counter = std::make_shared<int>(0);
auto grp = group::get("local", "test"); auto grp = group::get("local", "test");
self->join(grp); self->join(grp);
CAF_CHECKPOINT(); CAF_CHECKPOINT();
self->become( self->become(
on(atom("msg")) >> [=] { [=](msg_atom) {
CAF_CHECKPOINT(); CAF_CHECKPOINT();
++*counter; ++*counter;
self->leave(grp); self->leave(grp);
self->send(grp, atom("msg")); self->send(grp, msg_atom::value);
}, },
on(atom("over")) >> [=] { [=](timeout_atom) {
// this actor should receive only 1 message // this actor should receive only 1 message
CAF_CHECK(*counter == 1); CAF_CHECK(*counter == 1);
self->quit(); self->quit();
} }
); );
self->send(grp, atom("msg")); self->send(grp, msg_atom::value);
self->delayed_send(self, std::chrono::seconds(1), atom("over")); self->delayed_send(self, std::chrono::seconds(1), timeout_atom::value);
} }
int main() { int main() {
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
using namespace caf; using namespace caf;
using namespace std; using namespace std;
using hi_atom = atom_constant<atom("hi")>;
using ho_atom = atom_constant<atom("ho")>;
optional<int> even_int(int i) { optional<int> even_int(int i) {
if (i % 2 == 0) { if (i % 2 == 0) {
return i; return i;
...@@ -86,13 +89,11 @@ function<void()> f(int idx) { ...@@ -86,13 +89,11 @@ function<void()> f(int idx) {
} }
void test_atoms() { void test_atoms() {
{ auto expr = on(hi_atom::value) >> f(0);
auto expr = on(atom("hi")) >> f(0); CAF_CHECK(invoked(0, expr, hi_atom::value));
CAF_CHECK(invoked(0, expr, atom("hi"))); CAF_CHECK(not_invoked(expr, ho_atom::value));
CAF_CHECK(not_invoked(expr, atom("ho"))); CAF_CHECK(not_invoked(expr, hi_atom::value, hi_atom::value));
CAF_CHECK(not_invoked(expr, atom("hi"), atom("hi"))); CAF_CHECK(not_invoked(expr, hi_atom::value));
CAF_CHECK(not_invoked(expr, "hi"));
}
} }
void test_custom_projections() { void test_custom_projections() {
......
...@@ -28,13 +28,6 @@ int main() { ...@@ -28,13 +28,6 @@ int main() {
CAF_TEST(test_metaprogramming); CAF_TEST(test_metaprogramming);
CAF_CHECK((ctm<type_list<int, float, double>, type_list<double, int, float>>::value));
CAF_CHECK((! ctm<type_list<int, float, double>, type_list<double, int, float, int>>::value));
CAF_CHECK((! ctm<type_list<int, float, double>, type_list<>>::value));
CAF_CHECK((! ctm<type_list<>, type_list<double, int, float, int>>::value));
using if1 = type_list<replies_to<int, double>::with<void>, using if1 = type_list<replies_to<int, double>::with<void>,
replies_to<int>::with<int>>; replies_to<int>::with<int>>;
using if2 = type_list<replies_to<int>::with<int>, using if2 = type_list<replies_to<int>::with<int>,
......
...@@ -8,17 +8,17 @@ void test_simple_reply_response() { ...@@ -8,17 +8,17 @@ void test_simple_reply_response() {
auto s = spawn([](event_based_actor* self) -> behavior { auto s = spawn([](event_based_actor* self) -> behavior {
return ( return (
others() >> [=]() -> message { others() >> [=]() -> message {
CAF_CHECK(self->last_dequeued() == make_message(atom("hello"))); CAF_CHECK(self->last_dequeued() == make_message(ok_atom::value));
self->quit(); self->quit();
return self->last_dequeued(); return self->last_dequeued();
} }
); );
}); });
scoped_actor self; scoped_actor self;
self->send(s, atom("hello")); self->send(s, ok_atom::value);
self->receive( self->receive(
others() >> [&] { others() >> [&] {
CAF_CHECK(self->last_dequeued() == make_message(atom("hello"))); CAF_CHECK(self->last_dequeued() == make_message(ok_atom::value));
} }
); );
self->await_all_other_actors_done(); self->await_all_other_actors_done();
......
...@@ -26,30 +26,30 @@ class event_testee : public sb_actor<event_testee> { ...@@ -26,30 +26,30 @@ class event_testee : public sb_actor<event_testee> {
public: public:
event_testee() { event_testee() {
wait4string = ( wait4string = behavior{
on<string>() >> [=] { [=](const std::string&) {
become(wait4int); become(wait4int);
}, },
on<atom("get_state")>() >> [=] { [=](get_atom) {
return "wait4string"; return "wait4string";
} }
); };
wait4float = ( wait4float = behavior{
on<float>() >> [=] { [=](float) {
become(wait4string); become(wait4string);
}, },
on<atom("get_state")>() >> [=] { [=](get_atom) {
return "wait4float"; return "wait4float";
} }
); };
wait4int = ( wait4int = behavior{
on<int>() >> [=] { [=](int) {
become(wait4float); become(wait4float);
}, },
on<atom("get_state")>() >> [=] { [=](get_atom) {
return "wait4int"; return "wait4int";
} }
); };
} }
}; };
...@@ -121,7 +121,7 @@ class testee_actor { ...@@ -121,7 +121,7 @@ class testee_actor {
on<string>() >> [&] { on<string>() >> [&] {
string_received = true; string_received = true;
}, },
on<atom("get_state")>() >> [&] { [&](get_atom) {
return "wait4string"; return "wait4string";
} }
) )
...@@ -134,7 +134,7 @@ class testee_actor { ...@@ -134,7 +134,7 @@ class testee_actor {
on<float>() >> [&] { on<float>() >> [&] {
float_received = true; float_received = true;
}, },
on<atom("get_state")>() >> [&] { [&](get_atom) {
return "wait4float"; return "wait4float";
} }
) )
...@@ -149,7 +149,7 @@ class testee_actor { ...@@ -149,7 +149,7 @@ class testee_actor {
on<int>() >> [&] { on<int>() >> [&] {
wait4float(self); wait4float(self);
}, },
on<atom("get_state")>() >> [&] { [&](get_atom) {
return "wait4int"; return "wait4int";
} }
); );
...@@ -178,7 +178,7 @@ string behavior_test(scoped_actor& self, actor et) { ...@@ -178,7 +178,7 @@ string behavior_test(scoped_actor& self, actor et) {
self->send(et, .3f); self->send(et, .3f);
self->send(et, "hello again"); self->send(et, "hello again");
self->send(et, "goodbye"); self->send(et, "goodbye");
self->send(et, atom("get_state")); self->send(et, get_atom::value);
self->receive ( self->receive (
[&](const string& str) { [&](const string& str) {
result = str; result = str;
......
...@@ -72,7 +72,7 @@ void client(event_based_actor* self, actor parent, server_type serv) { ...@@ -72,7 +72,7 @@ void client(event_based_actor* self, actor parent, server_type serv) {
self->sync_send(serv, my_request{10, 20}).then( self->sync_send(serv, my_request{10, 20}).then(
[=](bool val2) { [=](bool val2) {
CAF_CHECK_EQUAL(val2, false); CAF_CHECK_EQUAL(val2, false);
self->send(parent, atom("passed")); self->send(parent, passed_atom::value);
} }
); );
} }
...@@ -105,7 +105,9 @@ void test_typed_spawn(server_type ts) { ...@@ -105,7 +105,9 @@ void test_typed_spawn(server_type ts) {
); );
self->spawn<monitored>(client, self, ts); self->spawn<monitored>(client, self, ts);
self->receive( self->receive(
on(atom("passed")) >> CAF_CHECKPOINT_CB() [](passed_atom) {
CAF_CHECKPOINT();
}
); );
self->receive( self->receive(
[](const down_msg& dmsg) { [](const down_msg& dmsg) {
......
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