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