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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// 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 selective_streaming
#include "core-test.hpp"
#include <memory>
#include <numeric>
#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/attach_continuous_stream_stage.hpp"
#include "caf/attach_stream_sink.hpp"
#include "caf/attach_stream_source.hpp"
#include "caf/broadcast_downstream_manager.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp"
using namespace caf;
namespace {
using value_type = std::pair<level, std::string>;
struct select {
static bool apply(level x, const value_type& y) noexcept {
return x == level::all || x == y.first;
}
bool operator()(level x, const value_type& y) const noexcept {
return apply(x, y);
}
};
using manager_type = broadcast_downstream_manager<value_type, level, select>;
using buf = std::vector<value_type>;
buf make_log(level lvl) {
buf result{{level::trace, "trace1"},
{level::trace, "trace2"},
{level::debug, "debug1"},
{level::error, "error1"},
{level::trace, "trace3"}};
auto predicate = [=](const value_type& x) { return !select::apply(lvl, x); };
auto e = result.end();
auto i = std::remove_if(result.begin(), e, predicate);
if (i != e)
result.erase(i, e);
return result;
}
TESTEE_SETUP();
TESTEE(log_producer) {
return {
[=](level lvl) -> result<stream<value_type>> {
auto res = attach_stream_source(
self,
// initialize state
[=](buf& xs) { xs = make_log(lvl); },
// get next element
[](buf& xs, downstream<value_type>& out, size_t num) {
MESSAGE("push " << num << " messages downstream");
auto n = std::min(num, xs.size());
for (size_t i = 0; i < n; ++i)
out.push(xs[i]);
xs.erase(xs.begin(), xs.begin() + static_cast<ptrdiff_t>(n));
},
// check whether we reached the end
[=](const buf& xs) {
if (xs.empty()) {
MESSAGE(self->name() << " is done");
return true;
}
return false;
},
unit, policy::arg<manager_type>::value);
auto& out = res.ptr()->out();
static_assert(std::is_same<decltype(out), manager_type&>::value,
"source has wrong manager_type type");
out.set_filter(res.outbound_slot(), lvl);
return res;
},
};
}
TESTEE_STATE(log_dispatcher) {
stream_stage_ptr<value_type, manager_type> stage;
};
TESTEE(log_dispatcher) {
self->state.stage = attach_continuous_stream_stage(
self,
// initialize state
[](unit_t&) {
// nop
},
// processing step
[](unit_t&, downstream<value_type>& out, value_type x) {
out.push(std::move(x));
},
// cleanup
[=](unit_t&, const error&) { MESSAGE(self->name() << " is done"); },
policy::arg<manager_type>::value);
return {
[=](join_atom, level lvl) {
auto& stg = self->state.stage;
MESSAGE("received 'join' request");
auto result = stg->add_outbound_path();
stg->out().set_filter(result, lvl);
return result;
},
[=](const stream<value_type>& in) {
self->state.stage->add_inbound_path(in);
},
};
}
TESTEE_STATE(log_consumer) {
std::vector<value_type> log;
};
TESTEE(log_consumer) {
return {
[=](stream<value_type>& in) {
return attach_stream_sink(
self,
// input stream
in,
// initialize state
[=](unit_t&) {
// nop
},
// processing step
[=](unit_t&, value_type x) {
self->state.log.emplace_back(std::move(x));
},
// cleanup and produce result message
[=](unit_t&, const error&) { MESSAGE(self->name() << " is done"); });
},
};
}
} // namespace
// -- unit tests ---------------------------------------------------------------
BEGIN_FIXTURE_SCOPE(test_coordinator_fixture<>)
CAF_TEST(select_all) {
auto src = sys.spawn(log_producer);
auto snk = sys.spawn(log_consumer);
MESSAGE(CAF_ARG(self) << CAF_ARG(src) << CAF_ARG(snk));
MESSAGE("initiate stream handshake");
self->send(snk * src, level::all);
run();
CHECK_EQ(deref<log_consumer_actor>(snk).state.log, make_log(level::all));
}
CAF_TEST(select_trace) {
auto src = sys.spawn(log_producer);
auto snk = sys.spawn(log_consumer);
MESSAGE(CAF_ARG(self) << CAF_ARG(src) << CAF_ARG(snk));
MESSAGE("initiate stream handshake");
self->send(snk * src, level::trace);
run();
CHECK_EQ(deref<log_consumer_actor>(snk).state.log, make_log(level::trace));
}
CAF_TEST(forking) {
auto src = sys.spawn(log_producer);
auto stg = sys.spawn(log_dispatcher);
auto snk1 = sys.spawn(log_consumer);
auto snk2 = sys.spawn(log_consumer);
sched.run();
self->send(stg * src, level::all);
self->send(snk1 * stg, join_atom_v, level::trace);
self->send(snk2 * stg, join_atom_v, level::error);
sched.run();
auto& st = deref<log_dispatcher_actor>(stg).state;
run_until([&] {
return st.stage->inbound_paths().empty() && st.stage->out().clean();
});
CHECK_EQ(deref<log_consumer_actor>(snk1).state.log, make_log(level::trace));
CHECK_EQ(deref<log_consumer_actor>(snk2).state.log, make_log(level::error));
self->send(stg, exit_reason::kill);
}
END_FIXTURE_SCOPE()
|