File: stateful_actor.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (233 lines) | stat: -rw-r--r-- 6,364 bytes parent folder | download
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
// 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 stateful_actor

#include "caf/stateful_actor.hpp"

#include "core-test.hpp"

#include "caf/event_based_actor.hpp"

using namespace caf;

using namespace std::string_literals;

namespace {

using typed_adder_actor
  = typed_actor<result<void>(add_atom, int), result<int>(get_atom)>;

struct counter {
  int value = 0;
};

behavior adder(stateful_actor<counter>* self) {
  return {
    [=](add_atom, int x) { self->state.value += x; },
    [=](get_atom) { return self->state.value; },
  };
}

class adder_class : public stateful_actor<counter> {
public:
  adder_class(actor_config& cfg) : stateful_actor<counter>(cfg) {
    // nop
  }

  behavior make_behavior() override {
    return adder(this);
  }
};

typed_adder_actor::behavior_type
typed_adder(typed_adder_actor::stateful_pointer<counter> self) {
  return {
    [=](add_atom, int x) { self->state.value += x; },
    [=](get_atom) { return self->state.value; },
  };
}

class typed_adder_class : public typed_adder_actor::stateful_impl<counter> {
public:
  using super = typed_adder_actor::stateful_impl<counter>;

  typed_adder_class(actor_config& cfg) : super(cfg) {
    // nop
  }

  behavior_type make_behavior() override {
    return typed_adder(this);
  }
};

struct fixture : test_coordinator_fixture<> {
  fixture() {
    // nop
  }

  template <class ActorUnderTest>
  void test_adder(ActorUnderTest aut) {
    inject((add_atom, int), from(self).to(aut).with(add_atom_v, 7));
    inject((add_atom, int), from(self).to(aut).with(add_atom_v, 4));
    inject((add_atom, int), from(self).to(aut).with(add_atom_v, 9));
    inject((get_atom), from(self).to(aut).with(get_atom_v));
    expect((int), from(aut).to(self).with(20));
  }

  template <class State>
  void test_name(const char* expected) {
    auto aut = sys.spawn([](stateful_actor<State>* self) -> behavior {
      return {
        [=](get_atom) {
          self->quit();
          return self->name();
        },
      };
    });
    inject((get_atom), from(self).to(aut).with(get_atom_v));
    expect((std::string), from(aut).to(self).with(expected));
  }
};

} // namespace

BEGIN_FIXTURE_SCOPE(fixture)

CAF_TEST(stateful actors can be dynamically typed) {
  test_adder(sys.spawn(adder));
  test_adder(sys.spawn<typed_adder_class>());
}

CAF_TEST(stateful actors can be statically typed) {
  test_adder(sys.spawn(typed_adder));
  test_adder(sys.spawn<adder_class>());
}

CAF_TEST(stateful actors without explicit name use the name of the parent) {
  struct state {
    // empty
  };
  test_name<state>("user.scheduled-actor");
}

namespace {

struct named_state {
  static inline const char* name = "testee";
};

} // namespace

CAF_TEST(states with static C string names override the default name) {
  test_name<named_state>("testee");
}

namespace {

int32_t add_operation(int32_t x, int32_t y) {
  return x + y;
}

} // namespace

CAF_TEST(states can accept constructor arguments and provide a behavior) {
  struct state_type {
    using operation_type = int32_t (*)(int32_t, int32_t);
    int32_t x;
    int32_t y;
    operation_type f;
    state_type(int32_t x, int32_t y, operation_type f) : x(x), y(y), f(f) {
      // nop
    }
    behavior make_behavior() {
      return {
        [=](int32_t x, int32_t y) {
          this->x = x;
          this->y = y;
        },
        [=](get_atom) { return f(x, y); },
      };
    }
  };
  using actor_type = stateful_actor<state_type>;
  auto testee = sys.spawn<actor_type>(10, 20, add_operation);
  auto& state = deref<actor_type>(testee).state;
  CHECK_EQ(state.x, 10);
  CHECK_EQ(state.y, 20);
  inject((get_atom), from(self).to(testee).with(get_atom_v));
  expect((int32_t), from(testee).to(self).with(30));
  inject((int32_t, int32_t), to(testee).with(1, 2));
  CHECK_EQ(state.x, 1);
  CHECK_EQ(state.y, 2);
  inject((get_atom), from(self).to(testee).with(get_atom_v));
  expect((int32_t), from(testee).to(self).with(3));
}

CAF_TEST(states optionally take the self pointer as first argument) {
  struct state_type : named_state {
    event_based_actor* self;
    int x;
    state_type(event_based_actor* self, int x) : self(self), x(x) {
      // nop
    }
    behavior make_behavior() {
      return {
        [=](get_atom) { return self->name(); },
      };
    }
  };
  using actor_type = stateful_actor<state_type>;
  auto testee = sys.spawn<actor_type>(10);
  auto& state = deref<actor_type>(testee).state;
  CHECK(state.self == &deref<actor_type>(testee));
  CHECK_EQ(state.x, 10);
  inject((get_atom), from(self).to(testee).with(get_atom_v));
  expect((std::string), from(testee).to(self).with("testee"s));
}

CAF_TEST(typed actors can use typed_actor_pointer as self pointer) {
  struct state_type : named_state {
    using self_pointer = typed_adder_actor::pointer_view;
    self_pointer self;
    int value;
    state_type(self_pointer self, int x) : self(self), value(x) {
      // nop
    }
    auto make_behavior() {
      return make_typed_behavior([=](add_atom, int x) { value += x; },
                                 [=](get_atom) { return value; });
    }
  };
  using actor_type = typed_adder_actor::stateful_impl<state_type>;
  auto testee = sys.spawn<actor_type>(10);
  auto& state = deref<actor_type>(testee).state;
  CHECK(state.self == &deref<actor_type>(testee));
  CHECK_EQ(state.value, 10);
  inject((add_atom, int), from(self).to(testee).with(add_atom_v, 1));
  inject((get_atom), from(self).to(testee).with(get_atom_v));
  expect((int), from(testee).to(self).with(11));
}

CAF_TEST(returned behaviors take precedence over make_behavior in the state) {
  struct state_type : named_state {
    behavior make_behavior() {
      CAF_LOG_TRACE("");
      return {
        [](int32_t x, int32_t y) { return x - y; },
      };
    }
  };
  auto fun = [](stateful_actor<state_type>*, int32_t num) -> behavior {
    CAF_LOG_TRACE(CAF_ARG(num));
    return {
      [num](int32_t x, int32_t y) { return x + y + num; },
    };
  };
  auto testee = sys.spawn<lazy_init>(fun, 10);
  inject((int32_t, int32_t), from(self).to(testee).with(1, 2));
  expect((int32_t), from(testee).to(self).with(13));
}

END_FIXTURE_SCOPE()