Commit 2d771bf0 authored by Dominik Charousset's avatar Dominik Charousset

Fix formatting, tools, and unit tests

parent 1fdc78ce
......@@ -115,11 +115,11 @@ struct base_state {
}
actor_ostream print() {
return aout(self) << color << name << " (id = " << self->id() << "): ";
return aout(self) << color << self->name() << " (id = " << self->id()
<< "): ";
}
virtual bool init(std::string m_name, std::string m_color) {
name = std::move(m_name);
virtual bool init(std::string m_color) {
color = std::move(m_color);
print() << "started" << color::reset_endl;
return true;
......@@ -130,13 +130,18 @@ struct base_state {
}
local_actor* self;
std::string name;
std::string color;
};
struct client_job_state : base_state {
static inline const char* name = "curl.client-job";
using base_state::base_state;
};
// encapsulates an HTTP request
behavior client_job(stateful_actor<base_state>* self, const actor& parent) {
if (!self->state.init("client-job", color::blue))
behavior client_job(stateful_actor<client_job_state>* self,
const actor& parent) {
if (!self->state.init(color::blue))
return {}; // returning an empty behavior terminates the actor
self->send(parent, read_atom_v, "http://www.example.com/index.html",
uint64_t{0}, uint64_t{4095});
......@@ -166,16 +171,18 @@ struct client_state : base_state {
std::random_device rd;
std::default_random_engine re;
std::uniform_int_distribution<int> dist;
static inline const char* name = "curl.client";
};
// spawns HTTP requests
behavior client(stateful_actor<client_state>* self, const actor& parent) {
using std::chrono::milliseconds;
self->link_to(parent);
if (!self->state.init("client", color::green))
if (!self->state.init(color::green))
return {}; // returning an empty behavior terminates the actor
self->send(self, next_atom_v);
return {[=](next_atom) {
return {
[=](next_atom) {
auto& st = self->state;
st.print() << "spawn new client_job (nr. " << ++st.count << ")"
<< color::reset_endl;
......@@ -185,7 +192,8 @@ behavior client(stateful_actor<client_state>* self, const actor& parent) {
// compute random delay until next job is launched
auto delay = st.dist(st.re);
self->delayed_send(self, milliseconds(delay), next_atom_v);
}};
},
};
}
struct curl_state : base_state {
......@@ -207,22 +215,23 @@ struct curl_state : base_state {
return size;
}
bool init(std::string m_name, std::string m_color) override {
bool init(std::string m_color) override {
curl = curl_easy_init();
if (curl == nullptr)
return false;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curl_state::callback);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
return base_state::init(std::move(m_name), std::move(m_color));
return base_state::init(std::move(m_color));
}
CURL* curl = nullptr;
buffer_type buf;
static inline const char* name = "curl.worker";
};
// manages a CURL session
behavior curl_worker(stateful_actor<curl_state>* self, const actor& parent) {
if (!self->state.init("curl-worker", color::yellow))
if (!self->state.init(color::yellow))
return {}; // returning an empty behavior terminates the actor
return {[=](read_atom, const std::string& fname, uint64_t offset,
uint64_t range) -> message {
......@@ -278,10 +287,11 @@ struct master_state : base_state {
}
std::vector<actor> idle;
std::vector<actor> busy;
static inline const char* name = "curl.master";
};
behavior curl_master(stateful_actor<master_state>* self) {
if (!self->state.init("curl-master", color::magenta))
if (!self->state.init(color::magenta))
return {}; // returning an empty behavior terminates the actor
// spawn workers
for (size_t i = 0; i < num_curl_workers; ++i)
......
// showcases how to add custom message types to CAF
// if friend access for serialization is available
#include <utility>
#include <iostream>
#include <utility>
#include "caf/all.hpp"
......@@ -57,11 +57,7 @@ private:
};
behavior testee(event_based_actor* self) {
return {
[=](const foo& x) {
aout(self) << deep_to_string(x) << endl;
}
};
return {[=](const foo& x) { aout(self) << deep_to_string(x) << endl; }};
}
void caf_main(actor_system& sys) {
......
......@@ -253,7 +253,6 @@ caf_add_test_suites(caf-core-test
detail.parser.read_string
detail.parser.read_timespan
detail.parser.read_unsigned_integer
detail.print
detail.ringbuffer
detail.ripemd_160
detail.serialized_size
......
......@@ -75,7 +75,6 @@ public:
return identifier_;
}
/// @private
const actor& dispatcher() {
return dispatcher_;
......
......@@ -38,7 +38,8 @@ public:
~type_id_list_builder();
// -- modifiers ---------------------------------------------------------------
// -- modifiers
// ---------------------------------------------------------------
void reserve(size_t new_capacity);
......
......@@ -116,8 +116,7 @@ public:
// -- inspection -------------------------------------------------------------
template <class Inspector>
friend bool
inspect(Inspector& f, ipv6_address& x) {
friend bool inspect(Inspector& f, ipv6_address& x) {
return f.object(x).fields(f.field("bytes", x.bytes_));
}
......
......@@ -31,12 +31,11 @@ public:
/// Deserializes tracing data from `source` and either overrides the content
/// of `dst` or allocates a new object if `dst` is null.
/// @returns the result of `source`.
virtual error deserialize(deserializer& source,
virtual bool deserialize(deserializer& source,
std::unique_ptr<tracing_data>& dst) const = 0;
/// @copydoc deserialize
virtual error_code<sec>
deserialize(binary_deserializer& source,
virtual bool deserialize(binary_deserializer& source,
std::unique_ptr<tracing_data>& dst) const = 0;
};
......
......@@ -108,6 +108,13 @@ string_view type_name_or_anonymous() {
return "anonymous";
}
/// Returns the type name of given `type` or an empty string if `type` is an
/// invalid ID.
CAF_CORE_EXPORT string_view query_type_name(type_id_t type);
/// Returns the type of given `name` or `invalid_type_id` if no type matches.
CAF_CORE_EXPORT type_id_t query_type_id(string_view name);
} // namespace caf
/// Starts a code block for registering custom types to CAF. Stores the first ID
......
......@@ -247,14 +247,14 @@ void parse_element(string_parser_state& ps, std::string& x,
parser::read_string(ps, make_consumer(x));
return;
}
auto is_legal
= [=](char c) { return c != '\0' && strchr(char_blacklist, c) == nullptr; };
auto is_legal = [=](char c) {
return c != '\0' && strchr(char_blacklist, c) == nullptr;
};
for (auto c = ps.current(); is_legal(c); c = ps.next())
x += c;
while (!x.empty() && isspace(x.back()))
x.pop_back();
ps.code = ps.at_end() ? pec::success :
pec::trailing_character;
ps.code = ps.at_end() ? pec::success : pec::trailing_character;
}
// -- convenience functions ----------------------------------------------------
......
......@@ -89,11 +89,9 @@ public:
CAF_LOG_TRACE(""); // serializing who would cause a deadlock
exclusive_guard guard(mtx_);
auto e = subscribers_.end();
auto cmp = [&](const strong_actor_ptr& lhs) {
return lhs.get() == who;
};
auto cmp = [&](const strong_actor_ptr& lhs) { return lhs.get() == who; };
auto i = std::find_if(subscribers_.begin(), e, cmp);
if(i == e)
if (i == e)
return {false, subscribers_.size()};
subscribers_.erase(i);
return {true, subscribers_.size()};
......@@ -134,8 +132,7 @@ using local_group_ptr = intrusive_ptr<local_group>;
class local_dispatcher : public event_based_actor {
public:
explicit local_dispatcher(actor_config& cfg, local_group_ptr g)
: event_based_actor(cfg),
group_(std::move(g)) {
: event_based_actor(cfg), group_(std::move(g)) {
// nop
}
......@@ -161,15 +158,13 @@ public:
CAF_LOG_TRACE(CAF_ARG(dm));
auto first = acquaintances_.begin();
auto last = acquaintances_.end();
auto i = std::find_if(first, last, [&](const actor& a) {
return a == dm.source;
});
auto i = std::find_if(first, last,
[&](const actor& a) { return a == dm.source; });
if (i != last)
acquaintances_.erase(i);
});
// return behavior
return {
[=](join_atom, const actor& other) {
return {[=](join_atom, const actor& other) {
CAF_LOG_TRACE(CAF_ARG(other));
if (acquaintances_.insert(other).second) {
monitor(other);
......@@ -184,11 +179,11 @@ public:
[=](forward_atom, const message& what) {
CAF_LOG_TRACE(CAF_ARG(what));
// local forwarding
group_->send_all_subscribers(current_element_->sender, what, context());
group_->send_all_subscribers(current_element_->sender, what,
context());
// forward to all acquaintances
send_to_acquaintances(what);
}
};
}};
}
private:
......@@ -216,8 +211,7 @@ using local_group_proxy_ptr = intrusive_ptr<local_group_proxy>;
class proxy_dispatcher : public event_based_actor {
public:
proxy_dispatcher(actor_config& cfg, local_group_proxy_ptr grp)
: event_based_actor(cfg),
group_(std::move(grp)) {
: event_based_actor(cfg), group_(std::move(grp)) {
CAF_LOG_TRACE("");
}
......@@ -235,7 +229,8 @@ class local_group_proxy : public local_group {
public:
local_group_proxy(actor_system& sys, actor remote_dispatcher,
local_group_module& mod, std::string id, node_id nid)
: local_group(mod, std::move(id), std::move(nid), std::move(remote_dispatcher)),
: local_group(mod, std::move(id), std::move(nid),
std::move(remote_dispatcher)),
proxy_dispatcher_{sys.spawn<proxy_dispatcher, hidden>(this)},
monitor_{sys.spawn<hidden>(dispatcher_monitor_actor, this)} {
// nop
......@@ -264,8 +259,8 @@ public:
}
}
void enqueue(strong_actor_ptr sender, message_id mid,
message msg, execution_unit* eu) override {
void enqueue(strong_actor_ptr sender, message_id mid, message msg,
execution_unit* eu) override {
CAF_LOG_TRACE(CAF_ARG(sender) << CAF_ARG(mid) << CAF_ARG(msg));
// forward message to the dispatcher
dispatcher_->enqueue(std::move(sender), mid,
......@@ -285,15 +280,12 @@ private:
self->set_down_handler([=](down_msg& down) {
CAF_LOG_TRACE(CAF_ARG(down));
auto msg = make_message(group_down_msg{group(grp)});
grp->send_all_subscribers(self->ctrl(), std::move(msg),
self->context());
grp->send_all_subscribers(self->ctrl(), std::move(msg), self->context());
self->quit(down.reason);
});
return {
[] {
return {[] {
// nop
}
};
}};
}
actor proxy_dispatcher_;
......@@ -311,11 +303,9 @@ behavior proxy_dispatcher::make_behavior() {
};
set_default_handler(fwd);
// return dummy behavior
return {
[] {
return {[] {
// nop
}
};
}};
}
class local_group_module : public group_module {
......@@ -330,8 +320,8 @@ public:
auto i = instances_.find(identifier);
if (i != instances_.end())
return group{i->second};
auto tmp = make_counted<local_group>(*this, identifier,
system().node(), none);
auto tmp = make_counted<local_group>(*this, identifier, system().node(),
none);
upgrade_to_unique_guard uguard(guard);
auto p = instances_.emplace(identifier, tmp);
auto result = p.first->second;
......
......@@ -291,7 +291,6 @@ save_data(Serializer& sink, const message::data_ptr& data) {
storage += meta.padded_size;
}
return sink.end_sequence() && sink.end_field() && sink.end_object();
}
} // namespace
......
......@@ -36,31 +36,64 @@ tracing_data::~tracing_data() {
// nop
}
template <class Inspector>
bool inspect(Inspector& f, tracing_data& x) {
return x.serialize(f);
}
namespace {
using access = optional_inspector_access<tracing_data_ptr>;
template <class Serializer>
bool serialize_impl(Serializer& sink, const tracing_data_ptr& x) {
if (!x) {
return sink.begin_object("tracing_data") //
&& sink.begin_field("value", false) //
&& sink.end_field() //
&& sink.end_object();
}
return sink.begin_object("tracing_data") //
&& sink.begin_field("value", true) //
&& x->serialize(sink) //
&& sink.end_field() //
&& sink.end_object();
}
template <class Deserializer>
bool deserialize_impl(Deserializer& source, tracing_data_ptr& x) {
bool is_present = false;
if (!source.begin_object("tracing_data")
|| !source.begin_field("value", is_present))
return false;
if (!is_present)
return source.end_field() && source.end_object();
auto ctx = source.context();
if (ctx == nullptr) {
source.emplace_error(sec::no_context,
"cannot deserialize tracing data without context");
return false;
}
auto tc = ctx->system().tracing_context();
if (tc == nullptr) {
source.emplace_error(sec::no_tracing_context,
"cannot deserialize tracing data without context");
return false;
}
return tc->deserialize(source, x) //
&& source.end_field() //
&& source.end_object();
}
} // namespace
bool inspect(serializer& sink, const tracing_data_ptr& x) {
return access::apply_object(sink, detail::as_mutable_ref(x));
return serialize_impl(sink, x);
}
bool inspect(binary_serializer& sink, const tracing_data_ptr& x) {
return access::apply_object(sink, detail::as_mutable_ref(x));
return serialize_impl(sink, x);
}
bool inspect(deserializer& source, tracing_data_ptr& x) {
return access::apply_object(source, x);
return deserialize_impl(source, x);
}
bool inspect(binary_deserializer& source, tracing_data_ptr& x) {
return access::apply_object(source, x);
return deserialize_impl(source, x);
}
} // namespace caf
......@@ -169,11 +169,11 @@ CAF_TEST(profilers record asynchronous messaging) {
CAF_CHECK_EQUAL(string_list({
"new: foo",
"new: bar, parent: foo",
"foo sends: (\"hello bar\")",
"bar got: (\"hello bar\")",
"bar sends: (\"hello foo\")",
"foo sends: message(std::string(\"hello bar\"))",
"bar got: message(std::string(\"hello bar\"))",
"bar sends: message(std::string(\"hello foo\"))",
"bar consumed the message",
"foo got: (\"hello foo\")",
"foo got: message(std::string(\"hello foo\"))",
"delete: bar",
"foo consumed the message",
"delete: foo",
......@@ -210,15 +210,15 @@ CAF_TEST(profilers record request / response messaging) {
"new: worker",
"new: server",
"new: client",
"client sends: (19, 23)",
"server got: (19, 23)",
"server sends: (19, 23)",
"client sends: message(int32_t(19), int32_t(23))",
"server got: message(int32_t(19), int32_t(23))",
"server sends: message(int32_t(19), int32_t(23))",
"server consumed the message",
"delete: server",
"worker got: (19, 23)",
"worker sends: (42)",
"worker got: message(int32_t(19), int32_t(23))",
"worker sends: message(int32_t(42))",
"worker consumed the message",
"client got: (42)",
"client got: message(int32_t(42))",
"client consumed the message",
"delete: worker",
"delete: client",
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE detail.print
#include "caf/detail/print.hpp"
#include "caf/test/dsl.hpp"
using namespace caf;
namespace {
struct fixture {
};
} // namespace
CAF_TEST_FIXTURE_SCOPE(print_tests, fixture)
CAF_TEST(todo) {
// implement me
}
CAF_TEST_FIXTURE_SCOPE_END()
......@@ -32,10 +32,10 @@
#include "caf/type_id.hpp"
#include "caf/type_id_list.hpp"
using std::make_tuple;
using std::map;
using std::string;
using std::vector;
using std::make_tuple;
using namespace caf;
......@@ -92,7 +92,8 @@ CAF_TEST(to_string converts messages to strings) {
CAF_CHECK_EQUAL(msg_as_string(), "message()");
CAF_CHECK_EQUAL(msg_as_string("hello", "world"),
R"__(message(std::string(hello), std::string(world)))__");
CAF_CHECK_EQUAL(msg_as_string(svec{"one", "two", "three"}),
CAF_CHECK_EQUAL(
msg_as_string(svec{"one", "two", "three"}),
R"__(message(std::vector<std::string>([one, two, three])))__");
CAF_CHECK_EQUAL(
msg_as_string(svec{"one", "two"}, "three", "four",
......
......@@ -116,7 +116,6 @@ bool inspect(Inspector& f, person& x) {
class foobar {
public:
const std::string& foo() {
return foo_;
}
......
......@@ -209,7 +209,6 @@ CAF_TEST(custom type) {
CAF_TEST(read_config accepts the to_string output of settings) {
fill();
auto str = to_string(x);
printf("%s\n",str.c_str());
settings y;
config_option_set dummy;
detail::config_consumer consumer{dummy, y};
......
......@@ -46,38 +46,36 @@ public:
// nop
}
error serialize(serializer& sink) const override {
return sink(value);
bool serialize(serializer& sink) const override {
return inspect_object(sink, value);
}
error_code<sec> serialize(binary_serializer& sink) const override {
return sink(value);
bool serialize(binary_serializer& sink) const override {
return inspect_object(sink, value);
}
};
class dummy_tracing_data_factory : public tracing_data_factory {
public:
error deserialize(deserializer& source,
bool deserialize(deserializer& source,
std::unique_ptr<tracing_data>& dst) const override {
return deserialize_impl(source, dst);
}
error_code<sec>
deserialize(binary_deserializer& source,
bool deserialize(binary_deserializer& source,
std::unique_ptr<tracing_data>& dst) const override {
return deserialize_impl(source, dst);
}
private:
template <class Deserializer>
typename Deserializer::result_type
deserialize_impl(Deserializer& source,
bool deserialize_impl(Deserializer& source,
std::unique_ptr<tracing_data>& dst) const {
string value;
if (auto err = source(value))
return err;
if (!inspect_object(source, value))
return false;
dst.reset(new dummy_tracing_data(std::move(value)));
return {};
return true;
}
};
......@@ -194,12 +192,11 @@ CAF_TEST(profilers inject tracing data into asynchronous messages) {
CAF_TEST(tracing data is serializable) {
byte_buffer buf;
binary_serializer sink{sys, buf};
tracing_data_ptr data;
tracing_data_ptr copy;
data.reset(new dummy_tracing_data("iTrace"));
CAF_CHECK_EQUAL(sink(data), none);
tracing_data_ptr data{new dummy_tracing_data("iTrace")};
CAF_CHECK(inspect_object(sink, data));
binary_deserializer source{sys, buf};
CAF_CHECK_EQUAL(source(copy), none);
tracing_data_ptr copy;
CAF_CHECK(inspect_object(source, copy));
CAF_REQUIRE_NOT_EQUAL(copy.get(), nullptr);
CAF_CHECK_EQUAL(dynamic_cast<dummy_tracing_data&>(*copy).value, "iTrace");
}
......
......@@ -109,8 +109,8 @@ public:
.set("caf.middleman.enable-automatic-connections", autoconn)
.set("caf.middleman.workers", size_t{0})
.set("caf.scheduler.policy", autoconn ? "testing" : "stealing")
.set("caf.logger.inline-output", true)
.set("caf.logger.console.verbosity", "debug")
.set("caf.logger.inline-output", true)
.set("caf.logger.console.verbosity", "debug")
.set("caf.middleman.attach-utility-actors", autoconn)) {
app_ids.emplace_back(to_string(defaults::middleman::app_identifier));
auto& mm = sys.middleman();
......
......@@ -208,8 +208,9 @@ struct entity {
template <class Inspector>
bool inspect(Inspector& f, entity& x) {
return f(f.field("aid", x.aid), f.field("tid", x.tid), f.field("nid", x.nid),
f.field("vid", x.vid), f.field("hidden", x.hidden),
return f.object(x).fields(f.field("aid", x.aid), f.field("tid", x.tid),
f.field("nid", x.nid), f.field("vid", x.vid),
f.field("hidden", x.hidden),
f.field("pretty_name", x.pretty_name));
}
......@@ -424,11 +425,11 @@ struct se_event {
string to_string(const se_event& x) {
string res;
res += "node{";
res += to_string(*x.source);
res += deep_to_string(*x.source);
res += ", ";
res += deep_to_string(x.vstamp);
res += ", ";
res += to_string(x.type);
res += deep_to_string(x.type);
res += ", ";
res += deep_to_string(x.fields);
res += "}";
......
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