Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Change raw for-loops to range for-loops.
[simgrid.git] / teshsuite / s4u / activity-lifecycle / testing_comm.cpp
1 /* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "activity-lifecycle.hpp"
7
8 // Normally, we should be able use Catch2's REQUIRE_THROWS_AS(...), but it generates errors with Address Sanitizer.
9 // They're certainly false positive. Nevermind and use this simpler replacement.
10 #define REQUIRE_NETWORK_FAILURE(...)                                                                                   \
11   do {                                                                                                                 \
12     try {                                                                                                              \
13       __VA_ARGS__;                                                                                                     \
14       FAIL("Expected exception NetworkFailureException not caught");                                                   \
15     } catch (simgrid::NetworkFailureException const&) {                                                                \
16       XBT_VERB("got expected NetworkFailureException");                                                                \
17     }                                                                                                                  \
18   } while (0)
19
20 static void test_link_off_helper(double delay)
21 {
22   const double start = simgrid::s4u::Engine::get_clock();
23
24   simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
25     assert_exit(true, 9);
26     double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
27     for (double& m : milestone)
28       m += start;
29     for (int i = 0; i < 4; i++) {
30       simgrid::s4u::this_actor::sleep_until(milestone[i]);
31       REQUIRE_NETWORK_FAILURE({
32         INFO("get(" << ('A' + i) << ")");
33         simgrid::s4u::Mailbox::by_name("mb")->get();
34       });
35     }
36     simgrid::s4u::this_actor::sleep_until(milestone[4]);
37   });
38
39   simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&start]() {
40     assert_exit(true, 9);
41     int data            = 42;
42     double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
43     for (double& m : milestone)
44       m += start;
45     for (int i = 0; i < 2; i++) {
46       simgrid::s4u::this_actor::sleep_until(milestone[i]);
47       XBT_VERB("dsend(%c)", 'A' + i);
48       simgrid::s4u::Mailbox::by_name("mb")->put_init(&data, 100000)->detach();
49     }
50     for (int i = 2; i < 4; i++) {
51       simgrid::s4u::this_actor::sleep_until(milestone[i]);
52       REQUIRE_NETWORK_FAILURE({
53         INFO("put(" << ('A' + i) << ")");
54         simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
55       });
56     }
57     simgrid::s4u::this_actor::sleep_until(milestone[4]);
58   });
59
60   for (int i = 0; i < 4; i++) {
61     XBT_VERB("##### %d / 4 #####", i + 1);
62     simgrid::s4u::this_actor::sleep_for(delay);
63     XBT_VERB("link off");
64     simgrid::s4u::Link::by_name("link1")->turn_off();
65     simgrid::s4u::this_actor::sleep_for(2.0 - delay);
66     XBT_VERB("link on");
67     simgrid::s4u::Link::by_name("link1")->turn_on();
68   }
69   simgrid::s4u::this_actor::sleep_for(1.5);
70 };
71
72 TEST_CASE("Activity lifecycle: comm activities")
73 {
74   XBT_INFO("#####[ launch next \"comm\" test ]#####");
75
76   BEGIN_SECTION("comm")
77   {
78     XBT_INFO("Launch a communication");
79     bool send_done = false;
80     bool recv_done = false;
81
82     simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
83       assert_exit(true, 5);
84       char* payload = xbt_strdup("toto");
85       simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
86       send_done = true;
87     });
88
89     simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
90       assert_exit(true, 5);
91       void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
92       xbt_free(payload);
93       recv_done = true;
94     });
95
96     simgrid::s4u::this_actor::sleep_for(9);
97     INFO("Sender or receiver killed somehow. It shouldn't");
98     REQUIRE(send_done);
99     REQUIRE(recv_done);
100
101     END_SECTION;
102   }
103
104   BEGIN_SECTION("comm dsend and quit (put before get)")
105   {
106     XBT_INFO("Launch a detached communication and end right after");
107     bool dsend_done = false;
108     bool recv_done  = false;
109
110     simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
111       assert_exit(true, 0);
112       char* payload = xbt_strdup("toto");
113       simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
114       dsend_done = true;
115     });
116
117     simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
118       assert_exit(true, 3);
119       simgrid::s4u::this_actor::sleep_for(2);
120       void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
121       xbt_free(payload);
122       recv_done = true;
123     });
124
125     // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
126     simgrid::s4u::this_actor::sleep_for(4);
127     INFO("Sender or receiver killed somehow. It shouldn't");
128     REQUIRE(dsend_done);
129     REQUIRE(recv_done);
130
131     END_SECTION;
132   }
133
134   BEGIN_SECTION("comm dsend and quit (get before put)")
135   {
136     XBT_INFO("Launch a detached communication and end right after");
137     bool dsend_done = false;
138     bool recv_done  = false;
139
140     simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
141       assert_exit(true, 2);
142       char* payload = xbt_strdup("toto");
143       simgrid::s4u::this_actor::sleep_for(2);
144       simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
145       dsend_done = true;
146     });
147
148     simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
149       assert_exit(true, 3);
150       void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
151       xbt_free(payload);
152       recv_done = true;
153     });
154
155     // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
156     simgrid::s4u::this_actor::sleep_for(4);
157     INFO("Sender or receiver killed somehow. It shouldn't");
158     REQUIRE(dsend_done);
159     REQUIRE(recv_done);
160
161     END_SECTION;
162   }
163
164   BEGIN_SECTION("comm kill sender")
165   {
166     XBT_INFO("Launch a communication and kill the sender");
167     bool send_done = false;
168     bool recv_done = false;
169
170     simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
171       assert_exit(false, 2);
172       // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd when the sender is killed during
173       // its communication (thanks to RAII).  The pointer is then released when the communication is over.
174       std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
175       simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
176       payload.release();
177       send_done = true;
178     });
179
180     simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
181       assert_exit(true, 2);
182       REQUIRE_NETWORK_FAILURE({
183         void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
184         xbt_free(payload);
185       });
186       recv_done = true;
187     });
188
189     simgrid::s4u::this_actor::sleep_for(2);
190     sender->kill();
191     // let the test ends by itself. waiting for surf_precision should be enough.
192     simgrid::s4u::this_actor::sleep_for(0.00001);
193
194     INFO("Sender was not killed properly or receiver killed somehow. It shouldn't");
195     REQUIRE(not send_done);
196     REQUIRE(recv_done);
197
198     END_SECTION;
199   }
200
201   BEGIN_SECTION("comm recv and kill")
202   {
203     XBT_INFO("Launch an actor that waits on a recv, kill its host");
204     bool in_on_exit              = false;
205     bool returned_from_main      = false;
206     bool in_catch_before_on_exit = false;
207     bool in_catch_after_on_exit  = false;
208     bool send_done               = false;
209
210     simgrid::s4u::ActorPtr receiver =
211         simgrid::s4u::Actor::create("receiver", all_hosts[1], [&in_on_exit, &returned_from_main,
212                                                                &in_catch_before_on_exit, &in_catch_after_on_exit]() {
213           assert_exit(false, 1);
214           try {
215             simgrid::s4u::Mailbox::by_name("mb")->get();
216           } catch (simgrid::NetworkFailureException const&) {
217             // Shouldn't get in here after the on_exit function
218             in_catch_before_on_exit = not in_on_exit;
219             in_catch_after_on_exit  = in_on_exit;
220           }
221           returned_from_main = true;
222         });
223
224     receiver->on_exit([&in_on_exit](bool) { in_on_exit = true; });
225
226     simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&send_done]() {
227       assert_exit(true, 1);
228       int data = 42;
229       REQUIRE_NETWORK_FAILURE(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000));
230       send_done = true;
231     });
232
233     simgrid::s4u::this_actor::sleep_for(1);
234     receiver->get_host()->turn_off();
235
236     // Note: If we don't sleep here, we don't "see" the bug
237     simgrid::s4u::this_actor::sleep_for(1);
238
239     INFO("Receiver's on_exit function was never called");
240     REQUIRE(in_on_exit);
241     INFO("or receiver mistakenly went to catch clause (before the on_exit function was called)");
242     REQUIRE(not in_catch_before_on_exit);
243     INFO("or receiver mistakenly went to catch clause (after the on_exit function was called)");
244     REQUIRE(not in_catch_after_on_exit);
245     INFO("or receiver returned from main normally even though its host was killed");
246     REQUIRE(not returned_from_main);
247     INFO("or sender killed somehow, and it shouldn't");
248     REQUIRE(send_done);
249     receiver->get_host()->turn_on();
250
251     END_SECTION;
252   }
253
254   BEGIN_SECTION("comm turn link off before send/recv")
255   {
256     XBT_INFO("try to communicate with communicating link turned off before start");
257     test_link_off_helper(0.0);
258
259     END_SECTION;
260   }
261
262   BEGIN_SECTION("comm turn link off between send/recv")
263   {
264     XBT_INFO("try to communicate with communicating link turned off between send and receive");
265     test_link_off_helper(1.0);
266
267     END_SECTION;
268   }
269
270   BEGIN_SECTION("comm turn link off during transfer")
271   {
272     XBT_INFO("try to communicate with communicating link turned off during transfer");
273     test_link_off_helper(2.0);
274
275     END_SECTION;
276   }
277
278   BEGIN_SECTION("comm turn link off during wait_any")
279   {
280     XBT_INFO("try to communicate with communicating link turned off during wait_any");
281     simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], []() {
282       assert_exit(true, 2);
283       int* data;
284       simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("mb")->get_async((void**)&data);
285       std::vector<simgrid::s4u::CommPtr> pending_comms = {comm};
286       REQUIRE_NETWORK_FAILURE(simgrid::s4u::Comm::wait_any(&pending_comms));
287     });
288
289     simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], []() {
290       assert_exit(true, 2);
291       int data = 42;
292       REQUIRE_NETWORK_FAILURE(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000));
293     });
294
295     simgrid::s4u::this_actor::sleep_for(2.0);
296     XBT_VERB("link off");
297     simgrid::s4u::Link::by_name("link1")->turn_off();
298     simgrid::s4u::this_actor::sleep_for(2.0);
299     XBT_VERB("link on");
300     simgrid::s4u::Link::by_name("link1")->turn_on();
301
302     END_SECTION;
303   }
304
305   simgrid::s4u::this_actor::sleep_for(10);
306   assert_cleanup();
307 }