/
test_sync_send.cpp
151 lines (140 loc) · 5.5 KB
/
test_sync_send.cpp
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
#include "test.hpp"
#include "cppa/cppa.hpp"
using namespace cppa;
using namespace cppa::placeholders;
struct popular_actor : event_based_actor { // popular actors have a buddy
actor_ptr m_buddy;
popular_actor(const actor_ptr& buddy) : m_buddy(buddy) { }
inline const actor_ptr& buddy() const { return m_buddy; }
void report_failure() {
send(buddy(), atom("failure"));
self->quit();
}
};
/******************************************************************************\
* test case 1: *
* *
* A B C *
* | | | *
* | --(sync_send)--> | | *
* | | --(forward)----> | *
* | X |---\ *
* | | | *
* | |<--/ *
* | <-------------(reply)-------------- | *
* X X *
\******************************************************************************/
struct A : popular_actor {
A(const actor_ptr& buddy) : popular_actor(buddy) { }
void init() {
become (
on(atom("go"), arg_match) >> [=](const actor_ptr& next) {
sync_send(next, atom("gogo")).then([=] {
send(buddy(), atom("success"));
quit();
});
},
others() >> [=] { report_failure(); }
);
}
};
#ifdef __clang__
struct B : sb_actor<B,popular_actor> {
B(const actor_ptr& buddy) : sb_actor<B,popular_actor>(buddy) { }
behavior init_state = (
others() >> [=] {
forward_to(buddy());
quit();
}
);
};
#else
struct B : popular_actor {
B(const actor_ptr& buddy) : popular_actor(buddy) { }
void init() {
become (
others() >> [=] {
forward_to(buddy());
quit();
}
);
}
};
#endif
struct C : sb_actor<C> {
behavior init_state = (
on(atom("gogo")) >> [=] {
reply(atom("gogogo"));
self->quit();
}
);
};
/******************************************************************************\
* test case 2: *
* *
* A D C *
* | | | *
* | --(sync_send)--> | | *
* | | --(sync_send)--> | *
* | | |---\ *
* | | | | *
* | | |<--/ *
* | | <---(reply)----- | *
* | <---(reply)----- | *
* X X *
\******************************************************************************/
struct D : popular_actor {
response_handle m_handle;
D(const actor_ptr& buddy) : popular_actor(buddy) { }
void init() {
become (
others() >> [=] {
m_handle = make_response_handle();
sync_send_tuple(buddy(), last_dequeued()).then([=] {
m_handle.apply(last_dequeued());
quit();
});
}
);
}
};
int main() {
CPPA_TEST(test__sync_send);
auto await_success_message = [&] {
receive (
on(atom("success")) >> CPPA_CHECKPOINT_CB(),
on(atom("failure")) >> CPPA_ERROR_CB("A didn't receive sync response"),
on(atom("DOWN"), arg_match).when(_x2 != exit_reason::normal)
>> [&](uint32_t err) {
CPPA_ERROR("A exited for reason " << err);
}
);
};
send(spawn_monitor<A>(self), atom("go"), spawn<B>(spawn<C>()));
await_success_message();
await_all_others_done();
send(spawn_monitor<A>(self), atom("go"), spawn<D>(spawn<C>()));
await_success_message();
await_all_others_done();
timed_sync_send(self, std::chrono::milliseconds(50), atom("NoWay")).await(
on(atom("TIMEOUT")) >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB()
);
// we should have received two DOWN messages with normal exit reason
// plus 'NoWay'
int i = 0;
receive_for(i, 3) (
on(atom("DOWN"), exit_reason::normal) >> CPPA_CHECKPOINT_CB(),
on(atom("NoWay")) >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB(),
after(std::chrono::seconds(0)) >> CPPA_UNEXPECTED_TOUT_CB()
);
CPPA_CHECKPOINT();
// mailbox should be empty now
receive (
others() >> CPPA_UNEXPECTED_MSG_CB(),
after(std::chrono::seconds(0)) >> CPPA_CHECKPOINT_CB()
);
shutdown();
return CPPA_TEST_RESULT();
}