Commit 0d30b74b authored by Dominik Charousset's avatar Dominik Charousset

Implement ping-pong example unit test

parent d1a25387
...@@ -55,6 +55,9 @@ add(remoting distributed_calculator) ...@@ -55,6 +55,9 @@ add(remoting distributed_calculator)
add(broker simple_broker) add(broker simple_broker)
add(broker simple_http_broker) add(broker simple_http_broker)
# testing DSL
add(testing ping_pong)
if(CAF_BUILD_PROTOBUF_EXAMPLES) if(CAF_BUILD_PROTOBUF_EXAMPLES)
find_package(Protobuf) find_package(Protobuf)
if(PROTOBUF_FOUND AND PROTOBUF_PROTOC_EXECUTABLE) if(PROTOBUF_FOUND AND PROTOBUF_PROTOC_EXECUTABLE)
......
// Manual refs: lines 12-65 (Testing)
#define CAF_SUITE ping_pong
#include "caf/test/dsl.hpp"
#include "caf/test/unit_test_impl.hpp"
#include "caf/all.hpp"
using namespace caf;
namespace {
using ping_atom = atom_constant<atom("ping")>;
using pong_atom = atom_constant<atom("pong")>;
behavior ping(event_based_actor* self, actor pong_actor, int n) {
self->send(pong_actor, ping_atom::value, n);
return {
[=](pong_atom, int x) {
if (x > 1)
self->send(pong_actor, ping_atom::value, x - 1);
}
};
}
behavior pong() {
return {
[=](ping_atom, int x) {
return std::make_tuple(pong_atom::value, x);
}
};
}
struct ping_pong_fixture : test_coordinator_fixture<> {
actor pong_actor;
ping_pong_fixture() {
// Spawn the Pong actor.
pong_actor = sys.spawn(pong);
// Run initialization code for Pong.
run();
}
};
} // namespace
CAF_TEST_FIXTURE_SCOPE(ping_pong_tests, ping_pong_fixture)
CAF_TEST(three pings) {
// Spawn the Ping actor and run its initialization code.
auto ping_actor = sys.spawn(ping, pong_actor, 3);
sched.run_once();
// Test communication between Ping and Pong.
expect((ping_atom, int), from(ping_actor).to(pong_actor).with(_, 3));
expect((pong_atom, int), from(pong_actor).to(ping_actor).with(_, 3));
expect((ping_atom, int), from(ping_actor).to(pong_actor).with(_, 2));
expect((pong_atom, int), from(pong_actor).to(ping_actor).with(_, 2));
expect((ping_atom, int), from(ping_actor).to(pong_actor).with(_, 1));
expect((pong_atom, int), from(pong_actor).to(ping_actor).with(_, 1));
// No further messages allowed.
disallow((ping_atom, int), from(ping_actor).to(pong_actor).with(_, 1));
}
CAF_TEST_FIXTURE_SCOPE_END()
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