Commit 366c771a authored by Dominik Charousset's avatar Dominik Charousset

Improve calculator example

parent 6430464e
...@@ -10,20 +10,26 @@ ...@@ -10,20 +10,26 @@
#include "caf/all.hpp" #include "caf/all.hpp"
using std::cout; using std::cout;
using std::cerr;
using std::endl; using std::endl;
using namespace caf; using namespace caf;
using plus_atom = atom_constant<atom("plus")>; using plus_atom = atom_constant<atom("plus")>;
using minus_atom = atom_constant<atom("minus")>; using minus_atom = atom_constant<atom("minus")>;
using result_atom = atom_constant<atom("result")>;
using calculator_actor =
typed_actor<replies_to<plus_atom, int, int>::with<result_atom, int>,
replies_to<minus_atom, int, int>::with<result_atom, int>>;
// implementation using the blocking API // implementation using the blocking API
void blocking_calculator(blocking_actor* self) { void blocking_calculator(blocking_actor* self) {
self->receive_loop ( self->receive_loop (
[](plus_atom, int a, int b) { [](plus_atom, int a, int b) {
return a + b; return std::make_tuple(result_atom::value, a + b);
}, },
[](minus_atom, int a, int b) { [](minus_atom, int a, int b) {
return a - b; return std::make_tuple(result_atom::value, a - b);
}, },
others >> [=] { others >> [=] {
cout << "unexpected: " << to_string(self->current_message()) << endl; cout << "unexpected: " << to_string(self->current_message()) << endl;
...@@ -31,22 +37,36 @@ void blocking_calculator(blocking_actor* self) { ...@@ -31,22 +37,36 @@ void blocking_calculator(blocking_actor* self) {
); );
} }
// execute this behavior until actor terminates // implementation using the event-based API
behavior calculator(event_based_actor* self) { behavior calculator(event_based_actor* self) {
return behavior{ return behavior{
[](plus_atom, int a, int b) { [](plus_atom, int a, int b) {
return a + b; return std::make_tuple(result_atom::value, a + b);
}, },
[](minus_atom, int a, int b) { [](minus_atom, int a, int b) {
return a - b; return std::make_tuple(result_atom::value, a - b);
}, },
others >> [=] { others >> [=] {
cout << "unexpected: " << to_string(self->current_message()) << endl; cerr << "unexpected: " << to_string(self->current_message()) << endl;
} }
}; };
} }
void tester(event_based_actor* self, const actor& testee) { // implementation using the statically typed API
calculator_actor::behavior_type typed_calculator() {
return {
[](plus_atom, int a, int b) {
return std::make_tuple(result_atom::value, a + b);
},
[](minus_atom, int a, int b) {
return std::make_tuple(result_atom::value, a - b);
}
};
}
// tests a calculator instance
template <class Handle>
void tester(event_based_actor* self, const Handle& testee, int x, int y) {
self->link_to(testee); self->link_to(testee);
// will be invoked if we receive an unexpected response message // will be invoked if we receive an unexpected response message
self->on_sync_failure([=] { self->on_sync_failure([=] {
...@@ -54,13 +74,13 @@ void tester(event_based_actor* self, const actor& testee) { ...@@ -54,13 +74,13 @@ void tester(event_based_actor* self, const actor& testee) {
self->quit(exit_reason::user_shutdown); self->quit(exit_reason::user_shutdown);
}); });
// first test: 2 + 1 = 3 // first test: 2 + 1 = 3
self->sync_send(testee, plus_atom::value, 2, 1).then( self->sync_send(testee, plus_atom::value, x, y).then(
on(3) >> [=] { [=](result_atom, int res1) {
// second test: 2 - 1 = 1 aout(self) << x << " + " << y << " = " << res1 << endl;
self->sync_send(testee, minus_atom::value, 2, 1).then( self->sync_send(testee, minus_atom::value, x, y).then(
on(1) >> [=] { [=](result_atom, int res2) {
// both tests succeeded // both tests succeeded
aout(self) << "AUT (actor under test) seems to be ok" << endl; aout(self) << x << " - " << y << " = " << res2 << endl;
self->quit(exit_reason::user_shutdown); self->quit(exit_reason::user_shutdown);
} }
); );
...@@ -68,12 +88,20 @@ void tester(event_based_actor* self, const actor& testee) { ...@@ -68,12 +88,20 @@ void tester(event_based_actor* self, const actor& testee) {
); );
} }
void test_calculators() {
scoped_actor self;
aout(self) << "blocking actor:" << endl;
self->spawn(tester<actor>, spawn<blocking_api>(blocking_calculator), 1, 2);
self->await_all_other_actors_done();
aout(self) << "event-based actor:" << endl;
self->spawn(tester<actor>, spawn(calculator), 3, 4);
self->await_all_other_actors_done();
aout(self) << "typed actor:" << endl;
self->spawn(tester<calculator_actor>, spawn_typed(typed_calculator), 5, 6);
self->await_all_other_actors_done();
}
int main() { int main() {
cout << "test blocking actor" << endl; test_calculators();
spawn(tester, spawn<blocking_api>(blocking_calculator));
await_all_actors_done();
cout << "test event-based actor" << endl;
spawn(tester, spawn(calculator));
await_all_actors_done();
shutdown(); shutdown();
} }
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