File: blocking_actor.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 (189 lines) | stat: -rw-r--r-- 5,647 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
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    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.                                      *
 ******************************************************************************/

#include "caf/config.hpp"

#define CAF_SUITE blocking_actor
#include "caf/test/unit_test.hpp"

#include "caf/all.hpp"

using namespace caf;

namespace {

struct fixture {
  actor_system_config cfg;
  actor_system system;
  scoped_actor self;

  fixture() : system(cfg), self(system) {
    // nop
  }
};

} // namespace

CAF_TEST_FIXTURE_SCOPE(blocking_actor_tests, fixture)

CAF_TEST(catch_all) {
  self->send(self, 42);
  self->receive(
    [](float) {
      CAF_FAIL("received unexpected float");
    },
    others >> [](message_view& x) -> result<message> {
      CAF_CHECK_EQUAL(to_string(x.content()), "(42)");
      return sec::unexpected_message;
    }
  );
  self->receive(
    [](const error& err) {
      CAF_CHECK_EQUAL(err, sec::unexpected_message);
    }
  );
}

CAF_TEST(behavior_ref) {
  behavior bhvr{
    [](int i) {
      CAF_CHECK_EQUAL(i, 42);
    }
  };
  self->send(self, 42);
  self->receive(bhvr);
}

CAF_TEST(timeout_in_scoped_actor) {
  bool timeout_called = false;
  scoped_actor self{system};
  self->receive(
    after(std::chrono::milliseconds(20)) >> [&] {
      timeout_called = true;
    }
  );
  CAF_CHECK(timeout_called);
}

// -- scoped_actors using skip -------------------------------------------------

using msg_t = int;
// send_order_t contains messages which are send to an actor in the same order
// as in vector
using send_order_t = std::vector<msg_t>;
// sequence_t contains a number of messages for processing by an actor with
// the information to skip the current message for later processing
using sequence_t = std::vector<std::pair<msg_t, bool>>;
using check_order_t = std::pair<send_order_t, sequence_t>;

behavior check_order_behavior_factory(local_actor*,
                                      sequence_t::const_iterator* seq_it_ptr) {
  return {
    [=](int i) -> result<void> {
      auto& seq_it = *seq_it_ptr;
      CAF_CHECK_EQUAL(i, seq_it->first);
      if (seq_it->second) {
        CAF_MESSAGE("current: " << i << "; awaiting: " << seq_it->first
                                << " SKIPPED");
        ++seq_it;
        return skip();
      } else {
        CAF_MESSAGE("current: " << i << "; awaiting: " << seq_it->first
                                << " OK");
        ++seq_it;
        return unit;
      }
    }
  };
}

void check_order_event_based_actor(const check_order_t& corder) {
  actor_system_config cfg;
  actor_system system{cfg};
  auto& send_order = corder.first;
  auto& sequence = corder.second;
  auto seq_it = sequence.cbegin();
  {
    auto tmp = system.spawn(
      [&](event_based_actor* self) {
        self->set_default_handler(skip);
        for(auto i : send_order) {
          self->send(self, i);
        }
        self->become(
          check_order_behavior_factory(self, &seq_it));
      }
    );

  }
  system.await_all_actors_done();
}

void check_order_scoped_actor(const check_order_t& corder) {
  actor_system_config cfg;
  actor_system system{cfg};
  auto& send_order = corder.first;
  auto& sequence = corder.second;
  auto seq_it = begin(sequence);
  scoped_actor self{system};
  auto check_order_behavior =
    check_order_behavior_factory(actor_cast<local_actor*>(self), &seq_it);
  for(auto i : send_order) {
    self->send(self, i);
  }
  while (seq_it != end(sequence)) {
    self->receive(check_order_behavior);
  }
}

CAF_TEST(skip_message) {
  check_order_t a = {
    {0, 1, 2, 3}, //recv_order = 0,1,2,3
    {{0, false}, {1, false}, {2, false}, {3, false}}
  };
  check_order_t b = {
    {3, 2, 1, 0}, //recv_order = 0,1,2,3
    {{3, true}, {2, true}, {1, true}, {0, false},
    {3, true}, {2, true}, {1, false},
    {3, true}, {2, false},
    {3, false}}
  };
  check_order_t c = {
    {1, 0, 2}, //recv_order = 0,1,2
    {{1, true}, {0, false},
    {1, false},
    {2, false}}
  };
  check_order_t d = {
    {3, 1, 2, 0}, //recv_order = 0,1,2,3
    {{3, true}, {1, true}, {2, true}, {0, false},
    {3, true}, {1, false},
    {3, true}, {2, false},
    {3, false}}
  };
  check_order_event_based_actor(a);
  check_order_event_based_actor(b);
  check_order_event_based_actor(c);
  check_order_event_based_actor(d);
  check_order_scoped_actor(a);
  check_order_scoped_actor(b);
  check_order_scoped_actor(c);
  check_order_scoped_actor(d);
}

CAF_TEST_FIXTURE_SCOPE_END()