Skip to content
Navigation Menu
{{ message }}
forked from actor-framework/actor-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_typed_spawn.cpp
More file actions
354 lines (309 loc) · 10.6 KB
/
Copy pathtest_typed_spawn.cpp
File metadata and controls
354 lines (309 loc) · 10.6 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2015 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* 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/string_algorithms.hpp"
#include "caf/all.hpp"
#include "test.hpp"
using namespace std;
using namespace caf;
namespace {
/******************************************************************************
* simple request/response test *
******************************************************************************/
struct my_request {
int a;
int b;
};
using server_type = typed_actor<replies_to<my_request>::with<bool>>;
bool operator==(const my_request& lhs, const my_request& rhs) {
return lhs.a == rhs.a && lhs.b == rhs.b;
}
server_type::behavior_type typed_server1() {
return {
[](const my_request& req) {
return req.a == req.b;
}
};
}
server_type::behavior_type typed_server2(server_type::pointer) {
return typed_server1();
}
class typed_server3 : public server_type::base {
public:
typed_server3(const string& line, actor buddy) { send(buddy, line); }
behavior_type make_behavior() override { return typed_server2(this); }
};
void client(event_based_actor* self, actor parent, server_type serv) {
self->sync_send(serv, my_request{0, 0}).then(
[=](bool val1) {
CAF_CHECK_EQUAL(val1, true);
self->sync_send(serv, my_request{10, 20}).then(
[=](bool val2) {
CAF_CHECK_EQUAL(val2, false);
self->send(parent, passed_atom::value);
}
);
}
);
}
void test_typed_spawn(server_type ts) {
scoped_actor self;
self->send(ts, my_request{1, 2});
self->receive(
[](bool value) {
CAF_CHECK_EQUAL(value, false);
}
);
self->send(ts, my_request{42, 42});
self->receive(
[](bool value) {
CAF_CHECK_EQUAL(value, true);
}
);
self->sync_send(ts, my_request{10, 20}).await(
[](bool value) {
CAF_CHECK_EQUAL(value, false);
}
);
self->sync_send(ts, my_request{0, 0}).await(
[](bool value) {
CAF_CHECK_EQUAL(value, true);
}
);
self->spawn<monitored>(client, self, ts);
self->receive(
[](passed_atom) {
CAF_CHECKPOINT();
}
);
self->receive(
[](const down_msg& dmsg) {
CAF_CHECK_EQUAL(dmsg.reason, exit_reason::normal);
}
);
self->send_exit(ts, exit_reason::user_shutdown);
}
/******************************************************************************
* test skipping of messages intentionally + using become() *
******************************************************************************/
struct get_state_msg {};
using event_testee_type = typed_actor<replies_to<get_state_msg>::with<string>,
replies_to<string>::with<void>,
replies_to<float>::with<void>,
replies_to<int>::with<int>>;
class event_testee : public event_testee_type::base {
public:
behavior_type wait4string() {
return {on<get_state_msg>() >> [] { return "wait4string"; },
on<string>() >> [=] { become(wait4int()); },
(on<float>() || on<int>()) >> skip_message};
}
behavior_type wait4int() {
return {
on<get_state_msg>() >> [] { return "wait4int"; },
on<int>() >> [=]()->int {become(wait4float());
return 42;
},
(on<float>() || on<string>()) >> skip_message
};
}
behavior_type wait4float() {
return {
on<get_state_msg>() >> [] {
return "wait4float";
},
on<float>() >> [=] { become(wait4string()); },
(on<string>() || on<int>()) >> skip_message};
}
behavior_type make_behavior() override {
return wait4int();
}
};
void test_event_testee() {
scoped_actor self;
auto et = self->spawn_typed<event_testee>();
string result;
self->send(et, 1);
self->send(et, 2);
self->send(et, 3);
self->send(et, .1f);
self->send(et, "hello event testee!");
self->send(et, .2f);
self->send(et, .3f);
self->send(et, "hello again event testee!");
self->send(et, "goodbye event testee!");
typed_actor<replies_to<get_state_msg>::with<string>> sub_et = et;
// $:: is the anonymous namespace
set<string> iface{"caf::replies_to<get_state_msg>::with<@str>",
"caf::replies_to<@str>::with<void>",
"caf::replies_to<float>::with<void>",
"caf::replies_to<@i32>::with<@i32>"};
CAF_CHECK_EQUAL(join(sub_et->message_types(), ","), join(iface, ","));
self->send(sub_et, get_state_msg{});
// we expect three 42s
int i = 0;
self->receive_for(i, 3)([](int value) { CAF_CHECK_EQUAL(value, 42); });
self->receive(
[&](const string& str) {
result = str;
},
after(chrono::minutes(1)) >> [&] {
CAF_PRINTERR("event_testee does not reply");
throw runtime_error("event_testee does not reply");
}
);
self->send_exit(et, exit_reason::user_shutdown);
self->await_all_other_actors_done();
CAF_CHECK_EQUAL(result, "wait4int");
}
/******************************************************************************
* simple 'forwarding' chain *
******************************************************************************/
using string_actor = typed_actor<replies_to<string>::with<string>>;
void simple_relay(string_actor::pointer self, string_actor master, bool leaf) {
string_actor next =
leaf ? spawn_typed(simple_relay, master, false) : master;
self->link_to(next);
self->become(
[=](const string& str) {
return self->sync_send(next, str).then(
[](const string & answer)->string {
return answer;
}
);
});
}
string_actor::behavior_type simple_string_reverter() {
return {
[](const string& str) {
return string{str.rbegin(), str.rend()};
}
};
}
void test_simple_string_reverter() {
scoped_actor self;
// actor-under-test
auto aut = self->spawn_typed<monitored>(simple_relay,
spawn_typed(simple_string_reverter),
true);
set<string> iface{"caf::replies_to<@str>::with<@str>"};
CAF_CHECK(aut->message_types() == iface);
self->sync_send(aut, "Hello World!").await([](const string& answer) {
CAF_CHECK_EQUAL(answer, "!dlroW olleH");
});
anon_send_exit(aut, exit_reason::user_shutdown);
}
/******************************************************************************
* sending typed actor handles *
******************************************************************************/
using int_actor = typed_actor<replies_to<int>::with<int>>;
int_actor::behavior_type int_fun() {
return {on_arg_match >> [](int i) { return i * i; }};
}
behavior foo(event_based_actor* self) {
return {
on_arg_match >> [=](int i, int_actor server) {
return self->sync_send(server, i).then([=](int result) -> int {
self->quit(exit_reason::normal);
return result;
});
}
};
}
void test_sending_typed_actors() {
scoped_actor self;
auto aut = spawn_typed(int_fun);
self->send(spawn(foo), 10, aut);
self->receive(on_arg_match >> [](int i) { CAF_CHECK_EQUAL(i, 100); });
self->send_exit(aut, exit_reason::user_shutdown);
}
int_actor::behavior_type int_fun2(int_actor::pointer self) {
self->trap_exit(true);
return {
[=](int i) {
self->monitor(self->current_sender());
return i * i;
},
[=](const down_msg& dm) {
CAF_CHECK_EQUAL(dm.reason, exit_reason::normal);
self->quit();
},
[=](const exit_msg&) {
CAF_UNEXPECTED_MSG(self);
}
};
}
behavior foo2(event_based_actor* self) {
return {
[=](int i, int_actor server) {
return self->sync_send(server, i).then([=](int result) -> int {
self->quit(exit_reason::normal);
return result;
});
}
};
}
void test_sending_typed_actors_and_down_msg() {
scoped_actor self;
auto aut = spawn_typed(int_fun2);
self->send(spawn(foo2), 10, aut);
self->receive([](int i) { CAF_CHECK_EQUAL(i, 100); });
}
} // namespace <anonymous>
/******************************************************************************
* put it all together *
******************************************************************************/
int main() {
CAF_TEST(test_typed_spawn);
// announce stuff
announce<get_state_msg>("get_state_msg");
announce<int_actor>("int_actor");
announce<my_request>("my_request", &my_request::a, &my_request::b);
// run test series with typed_server(1|2)
test_typed_spawn(spawn_typed(typed_server1));
await_all_actors_done();
CAF_CHECKPOINT();
test_typed_spawn(spawn_typed(typed_server2));
await_all_actors_done();
CAF_CHECKPOINT();
{
scoped_actor self;
test_typed_spawn(spawn_typed<typed_server3>("hi there", self));
self->receive(on("hi there") >> CAF_CHECKPOINT_CB());
}
await_all_actors_done();
CAF_CHECKPOINT();
// run test series with event_testee
test_event_testee();
await_all_actors_done();
CAF_CHECKPOINT();
// run test series with string reverter
test_simple_string_reverter();
await_all_actors_done();
CAF_CHECKPOINT();
// run test series with sending of typed actors
test_sending_typed_actors();
await_all_actors_done();
CAF_CHECKPOINT();
// and again plus check whether typed actors can handle system messages
test_sending_typed_actors_and_down_msg();
await_all_actors_done();
CAF_CHECKPOINT();
shutdown();
return CAF_TEST_RESULT();
}
You can’t perform that action at this time.
