1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#define CAF_SUITE actor_pool
#include "caf/actor_pool.hpp"
#include "core-test.hpp"
#include "caf/all.hpp"
using namespace caf;
namespace {
std::atomic<size_t> s_ctors;
std::atomic<size_t> s_dtors;
class worker : public event_based_actor {
public:
worker(actor_config& cfg) : event_based_actor(cfg) {
++s_ctors;
}
~worker() override {
++s_dtors;
}
behavior make_behavior() override {
auto nested = exit_handler_;
set_exit_handler(
[=](scheduled_actor* self, exit_msg& em) { nested(self, em); });
return {
[](int32_t x, int32_t y) { return x + y; },
};
}
};
struct fixture {
// allows us to check s_dtors after dtor of actor_system
actor_system_config cfg;
union {
actor_system system;
};
union {
scoped_execution_unit context;
};
std::function<actor()> spawn_worker;
fixture() {
new (&system) actor_system(cfg);
new (&context) scoped_execution_unit(&system);
spawn_worker = [&] { return system.spawn<worker>(); };
}
~fixture() {
system.await_all_actors_done();
context.~scoped_execution_unit();
system.~actor_system();
CHECK_EQ(s_dtors.load(), s_ctors.load());
}
};
#define HANDLE_ERROR \
[](const error& err) { \
CAF_FAIL("AUT responded with an error: " + to_string(err)); \
}
} // namespace
BEGIN_FIXTURE_SCOPE(fixture)
CAF_TEST(round_robin_actor_pool) {
scoped_actor self{system};
auto pool = actor_pool::make(&context, 5, spawn_worker,
actor_pool::round_robin());
self->send(pool, sys_atom_v, put_atom_v, spawn_worker());
std::vector<actor> workers;
for (int32_t i = 0; i < 6; ++i) {
self->request(pool, infinite, i, i)
.receive(
[&](int32_t res) {
CHECK_EQ(res, i + i);
auto sender = actor_cast<strong_actor_ptr>(self->current_sender());
CAF_REQUIRE(sender);
workers.push_back(actor_cast<actor>(std::move(sender)));
},
HANDLE_ERROR);
}
CHECK_EQ(workers.size(), 6u);
CHECK(std::unique(workers.begin(), workers.end()) == workers.end());
self->request(pool, infinite, sys_atom_v, get_atom_v)
.receive(
[&](std::vector<actor>& ws) {
std::sort(workers.begin(), workers.end());
std::sort(ws.begin(), ws.end());
CAF_REQUIRE_EQUAL(workers.size(), ws.size());
CHECK(std::equal(workers.begin(), workers.end(), ws.begin()));
},
HANDLE_ERROR);
MESSAGE("await last worker");
anon_send_exit(workers.back(), exit_reason::user_shutdown);
self->wait_for(workers.back());
MESSAGE("last worker shut down");
workers.pop_back();
// poll actor pool up to 10 times or until it removes the failed worker
bool success = false;
size_t i = 0;
while (!success && ++i <= 10) {
self->request(pool, infinite, sys_atom_v, get_atom_v)
.receive(
[&](std::vector<actor>& ws) {
success = workers.size() == ws.size();
if (success) {
std::sort(ws.begin(), ws.end());
CHECK_EQ(workers, ws);
} else {
// wait a bit until polling again
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
},
HANDLE_ERROR);
}
CAF_REQUIRE_EQUAL(success, true);
MESSAGE("about to send exit to workers");
self->send_exit(pool, exit_reason::user_shutdown);
self->wait_for(workers);
}
CAF_TEST(broadcast_actor_pool) {
scoped_actor self{system};
auto spawn5 = [&] {
return actor_pool::make(&context, 5, fixture::spawn_worker,
actor_pool::broadcast());
};
CHECK_EQ(system.registry().running(), 1u);
auto pool = actor_pool::make(&context, 5, spawn5, actor_pool::broadcast());
CHECK_EQ(system.registry().running(), 32u);
self->send(pool, 1, 2);
std::vector<int> results;
int i = 0;
self->receive_for(i, 25)([&](int res) { results.push_back(res); },
after(std::chrono::milliseconds(250)) >>
[] { CAF_ERROR("didn't receive a result"); });
CHECK_EQ(results.size(), 25u);
CHECK(std::all_of(results.begin(), results.end(),
[](int res) { return res == 3; }));
self->send_exit(pool, exit_reason::user_shutdown);
}
CAF_TEST(random_actor_pool) {
scoped_actor self{system};
auto pool = actor_pool::make(&context, 5, spawn_worker, actor_pool::random());
for (int i = 0; i < 5; ++i) {
self->request(pool, std::chrono::milliseconds(250), 1, 2)
.receive([&](int res) { CHECK_EQ(res, 3); }, HANDLE_ERROR);
}
self->send_exit(pool, exit_reason::user_shutdown);
}
END_FIXTURE_SCOPE()
|