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_sync_send.cpp
More file actions
348 lines (324 loc) · 13.3 KB
/
Copy pathtest_sync_send.cpp
File metadata and controls
348 lines (324 loc) · 13.3 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
#include "test.hpp"
#include "cppa/cppa.hpp"
using namespace std;
using namespace cppa;
using namespace cppa::placeholders;
namespace {
struct sync_mirror : sb_actor<sync_mirror> {
behavior init_state;
sync_mirror() {
init_state = (
others() >> [=] { return last_dequeued(); }
);
}
};
// replies to 'f' with 0.0f and to 'i' with 0
struct float_or_int : sb_actor<float_or_int> {
behavior init_state = (
on(atom("f")) >> [] { return 0.0f; },
on(atom("i")) >> [] { return 0; }
);
};
struct popular_actor : event_based_actor { // popular actors have a buddy
actor m_buddy;
popular_actor(const actor& buddy) : m_buddy(buddy) { }
inline const actor& buddy() const { return m_buddy; }
void report_failure() {
send(buddy(), atom("failure"));
quit();
}
};
/******************************************************************************\
* test case 1: *
* *
* A B C *
* | | | *
* | --(sync_send)--> | | *
* | | --(forward)----> | *
* | X |---\ *
* | | | *
* | |<--/ *
* | <-------------(reply)-------------- | *
* X X *
\******************************************************************************/
struct A : popular_actor {
A(const actor& buddy) : popular_actor(buddy) { }
behavior make_behavior() override {
return (
on(atom("go"), arg_match) >> [=](const actor& next) {
CPPA_CHECKPOINT();
sync_send(next, atom("gogo")).then([=] {
CPPA_CHECKPOINT();
send(buddy(), atom("success"));
quit();
});
},
others() >> [=] { report_failure(); }
);
}
};
struct B : popular_actor {
B(const actor& buddy) : popular_actor(buddy) { }
behavior make_behavior() override {
return (
others() >> [=] {
CPPA_CHECKPOINT();
forward_to(buddy());
quit();
}
);
}
};
struct C : sb_actor<C> {
behavior init_state;
C() {
init_state = (
on(atom("gogo")) >> [=]() -> atom_value {
CPPA_CHECKPOINT();
quit();
return atom("gogogo");
}
);
}
};
/******************************************************************************\
* test case 2: *
* *
* A D C *
* | | | *
* | --(sync_send)--> | | *
* | | --(sync_send)--> | *
* | | |---\ *
* | | | | *
* | | |<--/ *
* | | <---(reply)----- | *
* | <---(reply)----- | *
* X X *
\******************************************************************************/
struct D : popular_actor {
D(const actor& buddy) : popular_actor(buddy) { }
behavior make_behavior() override {
return (
others() >> [=] {
/*
response_promise handle = make_response_promise();
sync_send_tuple(buddy(), last_dequeued()).then([=] {
reply_to(handle, last_dequeued());
quit();
});
//*/
return sync_send_tuple(buddy(), last_dequeued()).then([=]() -> any_tuple {
quit();
return last_dequeued();
});//*/
}
);
}
};
/******************************************************************************\
* test case 3: *
* *
* Client Server Worker *
* | | | *
* | | <---(idle)------ | *
* | ---(request)---> | | *
* | | ---(request)---> | *
* | | |---\ *
* | X | | *
* | |<--/ *
* | <------------(response)------------ | *
* X *
\******************************************************************************/
struct server : event_based_actor {
behavior make_behavior() override {
auto die = [=] { quit(exit_reason::user_shutdown); };
return (
on(atom("idle"), arg_match) >> [=](actor worker) {
become (
keep_behavior,
on(atom("request")) >> [=] {
forward_to(worker);
unbecome(); // await next idle message
},
on(atom("idle")) >> skip_message,
others() >> die
);
},
on(atom("request")) >> skip_message,
others() >> die
);
}
};
void compile_time_optional_variant_check(event_based_actor* self) {
typedef optional_variant<std::tuple<int, float>,
std::tuple<float, int, int>>
msg_type;
self->sync_send(self, atom("msg")).then([](msg_type) {});
}
void test_sync_send() {
scoped_actor self;
self->on_sync_failure([&] {
CPPA_FAILURE("received: " << to_string(self->last_dequeued()));
});
self->spawn<monitored + blocking_api>([](blocking_actor* s) {
CPPA_LOGC_TRACE("NONE", "main$sync_failure_test", "id = " << s->id());
int invocations = 0;
auto foi = s->spawn<float_or_int, linked>();
s->send(foi, atom("i"));
s->receive(on_arg_match >> [](int i) { CPPA_CHECK_EQUAL(i, 0); });
s->on_sync_failure([=] {
CPPA_FAILURE("received: " << to_string(s->last_dequeued()));
});
s->sync_send(foi, atom("i")).await(
[&](int i) { CPPA_CHECK_EQUAL(i, 0); ++invocations; },
[&](float) { CPPA_UNEXPECTED_MSG(s); }
);
s->sync_send(foi, atom("f")).await(
[&](int) { CPPA_UNEXPECTED_MSG(s); },
[&](float f) { CPPA_CHECK_EQUAL(f, 0.f); ++invocations; }
);
CPPA_CHECK_EQUAL(invocations, 2);
CPPA_PRINT("trigger sync failure");
// provoke invocation of s->handle_sync_failure()
bool sync_failure_called = false;
bool int_handler_called = false;
s->on_sync_failure([&] {
sync_failure_called = true;
});
s->sync_send(foi, atom("f")).await(
on<int>() >> [&] {
int_handler_called = true;
}
);
CPPA_CHECK_EQUAL(sync_failure_called, true);
CPPA_CHECK_EQUAL(int_handler_called, false);
s->quit(exit_reason::user_shutdown);
});
self->receive (
on_arg_match >> [&](const down_msg& dm) {
CPPA_CHECK_EQUAL(dm.reason, exit_reason::user_shutdown);
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
auto mirror = spawn<sync_mirror>();
bool continuation_called = false;
self->sync_send(mirror, 42)
.await([&](int value) {
continuation_called = true;
CPPA_CHECK_EQUAL(value, 42);
});
CPPA_CHECK_EQUAL(continuation_called, true);
self->send_exit(mirror, exit_reason::user_shutdown);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
auto non_normal_down_msg = [](down_msg dm) -> optional<down_msg> {
if (dm.reason != exit_reason::normal) return dm;
return none;
};
auto await_success_message = [&] {
self->receive (
on(atom("success")) >> CPPA_CHECKPOINT_CB(),
on(atom("failure")) >> CPPA_FAILURE_CB("A didn't receive sync response"),
on(non_normal_down_msg) >> [&](const down_msg& dm) {
CPPA_FAILURE("A exited for reason " << dm.reason);
}
);
};
self->send(self->spawn<A, monitored>(self), atom("go"), spawn<B>(spawn<C>()));
await_success_message();
CPPA_CHECKPOINT();
self->await_all_other_actors_done();
self->send(self->spawn<A, monitored>(self), atom("go"), spawn<D>(spawn<C>()));
await_success_message();
CPPA_CHECKPOINT();
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
self->timed_sync_send(self, std::chrono::milliseconds(50), atom("NoWay")).await(
on<sync_timeout_msg>() >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
// we should have received two DOWN messages with normal exit reason
// plus 'NoWay'
int i = 0;
self->receive_for(i, 3) (
on_arg_match >> [&](const down_msg& dm) {
CPPA_CHECK_EQUAL(dm.reason, exit_reason::normal);
},
on(atom("NoWay")) >> [] {
CPPA_CHECKPOINT();
CPPA_PRINT("trigger \"actor did not reply to a "
"synchronous request message\"");
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self),
after(std::chrono::seconds(0)) >> CPPA_UNEXPECTED_TOUT_CB()
);
CPPA_CHECKPOINT();
// mailbox should be empty now
self->receive (
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self),
after(std::chrono::seconds(0)) >> CPPA_CHECKPOINT_CB()
);
// check wheter continuations are invoked correctly
auto c = spawn<C>(); // replies only to 'gogo' messages
// first test: sync error must occur, continuation must not be called
bool timeout_occured = false;
self->on_sync_timeout([&] { timeout_occured = true; });
self->on_sync_failure(CPPA_UNEXPECTED_MSG_CB_REF(self));
self->timed_sync_send(c, std::chrono::milliseconds(500), atom("HiThere"))
.await(CPPA_FAILURE_CB("C replied to 'HiThere'!"));
CPPA_CHECK_EQUAL(timeout_occured, true);
self->on_sync_failure(CPPA_UNEXPECTED_MSG_CB_REF(self));
self->sync_send(c, atom("gogo")).await(CPPA_CHECKPOINT_CB());
self->send_exit(c, exit_reason::user_shutdown);
self->await_all_other_actors_done();
CPPA_CHECKPOINT();
// test use case 3
self->spawn<monitored + blocking_api>([](blocking_actor* s) { // client
auto serv = s->spawn<server, linked>(); // server
auto work = s->spawn<linked>([](event_based_actor* w) { // worker
w->become(on(atom("request")) >> []{ return atom("response"); });
});
// first 'idle', then 'request'
anon_send(serv, atom("idle"), work);
s->sync_send(serv, atom("request")).await(
on(atom("response")) >> [=] {
CPPA_CHECKPOINT();
CPPA_CHECK_EQUAL(s->last_sender(), work);
},
others() >> [&] {
CPPA_PRINTERR("unexpected message: "
<< to_string(s->last_dequeued()));
}
);
// first 'request', then 'idle'
auto handle = s->sync_send(serv, atom("request"));
send_as(work, serv, atom("idle"));
handle.await(
on(atom("response")) >> [=] {
CPPA_CHECKPOINT();
CPPA_CHECK_EQUAL(s->last_sender(), work);
},
others() >> CPPA_UNEXPECTED_MSG_CB(s)
);
s->send(s, "Ever danced with the devil in the pale moonlight?");
// response: {'EXIT', exit_reason::user_shutdown}
s->receive_loop(others() >> CPPA_UNEXPECTED_MSG_CB(s));
});
self->receive (
on_arg_match >> [&](const down_msg& dm) {
CPPA_CHECK_EQUAL(dm.reason, exit_reason::user_shutdown);
},
others() >> CPPA_UNEXPECTED_MSG_CB_REF(self)
);
}
} // namespace <anonymous>
int main() {
CPPA_TEST(test_sync_send);
test_sync_send();
await_all_actors_done();
CPPA_CHECKPOINT();
shutdown();
// shutdown warning about unused function (only performs compile-time check)
static_cast<void>(compile_time_optional_variant_check);
return CPPA_TEST_RESULT();
}
You can’t perform that action at this time.
