Commit 4fd393da authored by Jakob Otto's avatar Jakob Otto

Add testcase in transport_worker_dispatcher

parent ea102198
......@@ -24,6 +24,8 @@
#include "host_fixture.hpp"
#include "caf/make_actor.hpp"
#include "caf/monitorable_actor.hpp"
#include "caf/node_id.hpp"
#include "caf/uri.hpp"
......@@ -32,6 +34,16 @@ using namespace caf::net;
namespace {
struct dummy_actor : public monitorable_actor {
dummy_actor(actor_config& cfg) : monitorable_actor(cfg) {
// nop
}
void enqueue(mailbox_element_ptr, execution_unit*) override {
// nop
}
};
class dummy_application {
public:
dummy_application(std::shared_ptr<std::vector<byte>> rec_buf, uint8_t id)
......@@ -102,12 +114,12 @@ private:
struct testdata {
testdata(uint8_t worker_id, node_id id, ip_endpoint ep)
: worker_id(worker_id), id(id), ep(ep) {
: worker_id(worker_id), nid(id), ep(ep) {
// nop
}
uint8_t worker_id;
node_id id;
node_id nid;
ip_endpoint ep;
};
......@@ -136,18 +148,53 @@ uri operator"" _u(const char* cstr, size_t cstr_len) {
}
struct fixture : host_fixture {
fixture() {
using dispatcher_type = transport_worker_dispatcher<dummy_application_factory,
ip_endpoint>;
fixture()
: buf{std::make_shared<std::vector<byte>>()},
dispatcher{dummy_application_factory{buf}} {
// nop
}
std::unique_ptr<net::endpoint_manager::message>
make_dummy_message(node_id nid) {
actor_id aid = 42;
actor_config cfg;
auto p = make_actor<dummy_actor, strong_actor_ptr>(aid, nid, &sys, cfg);
std::vector<byte> payload;
auto strong_actor = actor_cast<strong_actor_ptr>(p);
mailbox_element::forwarding_stack stack;
auto elem = make_mailbox_element(std::move(strong_actor),
make_message_id(12345), std::move(stack),
make_message());
return detail::make_unique<endpoint_manager::message>(std::move(elem),
payload);
}
template <class Application, class IdType>
void add_new_workers(
transport_worker_dispatcher<Application, IdType>& dispatcher) {
for (auto& data : test_data) {
dispatcher.add_new_worker(data.id, data.ep);
dispatcher.add_new_worker(data.nid, data.ep);
}
}
void test_write_message(testdata& testcase) {
auto msg = make_dummy_message(testcase.nid);
if (!msg->msg->sender)
CAF_FAIL("sender is null");
dispatcher.add_new_worker(testcase.nid, "[::1]:1"_ep);
dispatcher.write_message(dummy, std::move(msg));
}
actor_system_config cfg{};
actor_system sys{cfg};
std::shared_ptr<std::vector<byte>> buf;
dispatcher_type dispatcher;
dummy dummy;
std::vector<testdata> test_data{
{0, make_node_id("http:file"_u), "[::1]:1"_ep},
{1, make_node_id("http:file?a=1&b=2"_u), "[fe80::2:34]:12345"_ep},
......@@ -156,66 +203,34 @@ struct fixture : host_fixture {
};
};
std::unique_ptr<net::endpoint_manager::message> make_dummy_message() {
actor act;
std::vector<byte> payload;
auto strong_actor = actor_cast<strong_actor_ptr>(act);
mailbox_element::forwarding_stack stack;
auto elem = make_mailbox_element(std::move(strong_actor),
make_message_id(12345), std::move(stack),
make_message());
return detail::make_unique<endpoint_manager::message>(std::move(elem),
payload);
}
#define CHECK_HANDLE_DATA(dispatcher, dummy, testcase, buf) \
dispatcher.handle_data(dummy, span<byte>{}, testcase.ep); \
CAF_CHECK_EQUAL(buf->size(), 1u); \
CAF_CHECK_EQUAL(static_cast<byte>(testcase.worker_id), buf->at(0)); \
buf->clear();
#define CHECK_WRITE_MESSAGE(dispatcher, dummy, worker_id, buf) \
{ \
auto msg = make_dummy_message(); \
if (!msg->msg->sender) \
CAF_FAIL("sender is null"); \
auto nid = msg->msg->sender->node(); \
dispatcher.add_new_worker(nid, "[::1]:1"_ep); \
dispatcher.write_message(dummy, std::move(msg)); \
CAF_CHECK_EQUAL(buf->size(), 1u); \
CAF_CHECK_EQUAL(static_cast<byte>(worker_id), buf->at(0)); \
buf->clear(); \
}
#define CHECK_WRITE_MESSAGE(dispatcher, dummy, testcase, buf) \
test_write_message(testcase); \
CAF_CHECK_EQUAL(buf->size(), 1u); \
CAF_CHECK_EQUAL(static_cast<byte>(testcase.worker_id), buf->at(0)); \
buf->clear();
} // namespace
CAF_TEST_FIXTURE_SCOPE(transport_worker_dispatcher_test, fixture)
CAF_TEST(handle_data) {
auto buf = std::make_shared<std::vector<byte>>();
using dispatcher_type = transport_worker_dispatcher<dummy_application_factory,
ip_endpoint>;
dispatcher_type dispatcher{dummy_application_factory{buf}};
add_new_workers(dispatcher);
dummy dummy{};
CHECK_HANDLE_DATA(dispatcher, dummy, test_data.at(0), buf);
CHECK_HANDLE_DATA(dispatcher, dummy, test_data.at(1), buf);
CHECK_HANDLE_DATA(dispatcher, dummy, test_data.at(2), buf);
CHECK_HANDLE_DATA(dispatcher, dummy, test_data.at(3), buf);
}
// TODO: figure out how to set node_id in messages/ create messages with passed
// node_ids
/*CAF_TEST(write_message) {
auto buf = std::make_shared<std::vector<byte>>();
using dispatcher_type = transport_worker_dispatcher<dummy_application_factory,
ip_endpoint>;
dispatcher_type dispatcher{dummy_application_factory{buf}};
dummy dummy{};
CHECK_WRITE_MESSAGE(dispatcher, dummy, 0, buf);
CHECK_WRITE_MESSAGE(dispatcher, dummy, 1, buf);
CHECK_WRITE_MESSAGE(dispatcher, dummy, 2, buf);
CHECK_WRITE_MESSAGE(dispatcher, dummy, 3, buf);
}*/
CAF_TEST(write_message) {
CHECK_WRITE_MESSAGE(dispatcher, dummy, test_data.at(0), buf);
CHECK_WRITE_MESSAGE(dispatcher, dummy, test_data.at(1), buf);
CHECK_WRITE_MESSAGE(dispatcher, dummy, test_data.at(2), buf);
CHECK_WRITE_MESSAGE(dispatcher, dummy, test_data.at(3), buf);
}
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