File: continuous_streaming.cpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (247 lines) | stat: -rw-r--r-- 8,747 bytes parent folder | download | duplicates (4)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 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 continuous_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/event_based_actor.hpp"
#include "caf/stateful_actor.hpp"

using std::string;

using namespace caf;

namespace {

/// Returns the sum of natural numbers up until `n`, i.e., 1 + 2 + ... + n.
int32_t sum(int32_t n) {
  return (n * (n + 1)) / 2;
}

TESTEE_SETUP();

TESTEE_STATE(file_reader) {
  std::vector<int32_t> buf;
};

VARARGS_TESTEE(file_reader, size_t buf_size) {
  return {
    [=](string& fname) -> result<stream<int32_t>, string> {
      CAF_CHECK_EQUAL(fname, "numbers.txt");
      CAF_CHECK_EQUAL(self->mailbox().empty(), true);
      return attach_stream_source(
        self,
        // forward file name in handshake to next stage
        std::forward_as_tuple(std::move(fname)),
        // initialize state
        [=](unit_t&) {
          auto& xs = self->state.buf;
          xs.resize(buf_size);
          std::iota(xs.begin(), xs.end(), 1);
        },
        // get next element
        [=](unit_t&, downstream<int32_t>& out, size_t num) {
          auto& xs = self->state.buf;
          CAF_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 unit_t&) {
          if (self->state.buf.empty()) {
            CAF_MESSAGE(self->name() << " is done");
            return true;
          }
          return false;
        });
    },
  };
}

TESTEE_STATE(sum_up) {
  int32_t x = 0;
};

TESTEE(sum_up) {
  return {
    [=](stream<int32_t>& in, const string& fname) {
      CAF_CHECK_EQUAL(fname, "numbers.txt");
      using int_ptr = int32_t*;
      return attach_stream_sink(
        self,
        // input stream
        in,
        // initialize state
        [=](int_ptr& x) { x = &self->state.x; },
        // processing step
        [](int_ptr& x, int32_t y) { *x += y; },
        // cleanup
        [=](int_ptr&, const error&) {
          CAF_MESSAGE(self->name() << " is done");
        });
    },
    [=](join_atom atm, actor src) {
      CAF_MESSAGE(self->name() << " joins a stream");
      self->send(self * src, atm);
    },
  };
}

TESTEE_STATE(stream_multiplexer) {
  stream_stage_ptr<int32_t, broadcast_downstream_manager<int32_t>> stage;
};

TESTEE(stream_multiplexer) {
  self->state.stage = attach_continuous_stream_stage(
    self,
    // initialize state
    [](unit_t&) {
      // nop
    },
    // processing step
    [](unit_t&, downstream<int32_t>& out, int32_t x) { out.push(x); },
    // cleanup
    [=](unit_t&, const error&) { CAF_MESSAGE(self->name() << " is done"); });
  return {
    [=](join_atom) {
      CAF_MESSAGE("received 'join' request");
      return self->state.stage->add_outbound_path(
        std::make_tuple("numbers.txt"));
    },
    [=](const stream<int32_t>& in, std::string& fname) {
      CAF_CHECK_EQUAL(fname, "numbers.txt");
      return self->state.stage->add_inbound_path(in);
    },
    [=](close_atom, int32_t sink_index) {
      auto& out = self->state.stage->out();
      out.close(out.path_slots().at(static_cast<size_t>(sink_index)));
    },
  };
}

struct config : actor_system_config {
  config() {
    add_message_types<id_block::core_test>();
  }
};

using fixture = test_coordinator_fixture<config>;

} // namespace

// -- unit tests ---------------------------------------------------------------

CAF_TEST_FIXTURE_SCOPE(local_streaming_tests, fixture)

CAF_TEST(depth_3_pipeline_with_fork) {
  auto src = sys.spawn(file_reader, 50u);
  auto stg = sys.spawn(stream_multiplexer);
  auto snk1 = sys.spawn(sum_up);
  auto snk2 = sys.spawn(sum_up);
  auto& st = deref<stream_multiplexer_actor>(stg).state;
  CAF_MESSAGE("connect sinks to the stage (fork)");
  self->send(snk1, join_atom_v, stg);
  self->send(snk2, join_atom_v, stg);
  consume_messages();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 2u);
  CAF_MESSAGE("connect source to the stage (fork)");
  self->send(stg * src, "numbers.txt");
  consume_messages();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 2u);
  CAF_CHECK_EQUAL(st.stage->inbound_paths().size(), 1u);
  run();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 2u);
  CAF_CHECK_EQUAL(st.stage->inbound_paths().size(), 0u);
  CAF_CHECK_EQUAL(deref<sum_up_actor>(snk1).state.x, 1275);
  CAF_CHECK_EQUAL(deref<sum_up_actor>(snk2).state.x, 1275);
  self->send_exit(stg, exit_reason::kill);
}

CAF_TEST(depth_3_pipeline_with_join) {
  auto src1 = sys.spawn(file_reader, 50u);
  auto src2 = sys.spawn(file_reader, 50u);
  auto stg = sys.spawn(stream_multiplexer);
  auto snk = sys.spawn(sum_up);
  auto& st = deref<stream_multiplexer_actor>(stg).state;
  CAF_MESSAGE("connect sink to the stage");
  self->send(snk, join_atom_v, stg);
  consume_messages();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 1u);
  CAF_MESSAGE("connect sources to the stage (join)");
  self->send(stg * src1, "numbers.txt");
  self->send(stg * src2, "numbers.txt");
  consume_messages();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 1u);
  CAF_CHECK_EQUAL(st.stage->inbound_paths().size(), 2u);
  run();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 1u);
  CAF_CHECK_EQUAL(st.stage->inbound_paths().size(), 0u);
  CAF_CHECK_EQUAL(deref<sum_up_actor>(snk).state.x, 2550);
  self->send_exit(stg, exit_reason::kill);
}

CAF_TEST(closing_downstreams_before_end_of_stream) {
  auto src = sys.spawn(file_reader, 10000u);
  auto stg = sys.spawn(stream_multiplexer);
  auto snk1 = sys.spawn(sum_up);
  auto snk2 = sys.spawn(sum_up);
  auto& st = deref<stream_multiplexer_actor>(stg).state;
  CAF_MESSAGE("connect sinks to the stage (fork)");
  self->send(snk1, join_atom_v, stg);
  self->send(snk2, join_atom_v, stg);
  consume_messages();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 2u);
  CAF_MESSAGE("connect source to the stage (fork)");
  self->send(stg * src, "numbers.txt");
  consume_messages();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 2u);
  CAF_CHECK_EQUAL(st.stage->inbound_paths().size(), 1u);
  CAF_MESSAGE("do a single round of credit");
  trigger_timeouts();
  consume_messages();
  CAF_MESSAGE("make sure the stream isn't done yet");
  CAF_REQUIRE(!deref<file_reader_actor>(src).state.buf.empty());
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 2u);
  CAF_CHECK_EQUAL(st.stage->inbound_paths().size(), 1u);
  CAF_MESSAGE("get the next not-yet-buffered integer");
  auto next_pending = deref<file_reader_actor>(src).state.buf.front();
  CAF_REQUIRE_GREATER(next_pending, 0);
  auto sink1_result = sum(next_pending - 1);
  CAF_MESSAGE("gracefully close sink 1, next pending: " << next_pending);
  self->send(stg, close_atom_v, 0);
  expect((atom_value, int32_t), from(self).to(stg));
  CAF_MESSAGE("ship remaining elements");
  run();
  CAF_CHECK_EQUAL(st.stage->out().num_paths(), 1u);
  CAF_CHECK_EQUAL(st.stage->inbound_paths().size(), 0u);
  CAF_CHECK_LESS(deref<sum_up_actor>(snk1).state.x, sink1_result);
  CAF_CHECK_EQUAL(deref<sum_up_actor>(snk2).state.x, sum(10000));
  self->send_exit(stg, exit_reason::kill);
}

CAF_TEST_FIXTURE_SCOPE_END()