1 /* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved. */
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. */
6 #include "activity-lifecycle.hpp"
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(...) \
14 FAIL("Expected exception NetworkFailureException not caught"); \
15 } catch (simgrid::NetworkFailureException const&) { \
16 XBT_VERB("got expected NetworkFailureException"); \
20 static void test_link_off_helper(double delay)
22 const double start = simgrid::s4u::Engine::get_clock();
24 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
26 double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
27 for (double& m : milestone)
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();
36 simgrid::s4u::this_actor::sleep_until(milestone[4]);
39 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&start]() {
42 double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
43 for (double& m : milestone)
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();
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);
57 simgrid::s4u::this_actor::sleep_until(milestone[4]);
60 for (int i = 0; i < 4; i++) {
61 XBT_VERB("##### %d / 4 #####", i + 1);
62 simgrid::s4u::this_actor::sleep_for(delay);
64 simgrid::s4u::Link::by_name("link1")->turn_off();
65 simgrid::s4u::this_actor::sleep_for(2.0 - delay);
67 simgrid::s4u::Link::by_name("link1")->turn_on();
69 simgrid::s4u::this_actor::sleep_for(1.5);
72 TEST_CASE("Activity lifecycle: comm activities")
74 XBT_INFO("#####[ launch next \"comm\" test ]#####");
78 XBT_INFO("Launch a communication");
79 bool send_done = false;
80 bool recv_done = false;
82 simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
84 char* payload = xbt_strdup("toto");
85 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
89 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
91 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
96 simgrid::s4u::this_actor::sleep_for(9);
97 INFO("Sender or receiver killed somehow. It shouldn't");
104 BEGIN_SECTION("comm dsend and quit (put before get)")
106 XBT_INFO("Launch a detached communication and end right after");
107 bool dsend_done = false;
108 bool recv_done = false;
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();
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();
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");
134 BEGIN_SECTION("comm dsend and quit (get before put)")
136 XBT_INFO("Launch a detached communication and end right after");
137 bool dsend_done = false;
138 bool recv_done = false;
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();
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();
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");
164 BEGIN_SECTION("comm kill sender")
166 XBT_INFO("Launch a communication and kill the sender");
167 bool send_done = false;
168 bool recv_done = false;
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);
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();
189 simgrid::s4u::this_actor::sleep_for(2);
191 // let the test ends by itself. waiting for surf_precision should be enough.
192 simgrid::s4u::this_actor::sleep_for(0.00001);
194 INFO("Sender was not killed properly or receiver killed somehow. It shouldn't");
195 REQUIRE(not send_done);
201 BEGIN_SECTION("comm recv and kill")
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;
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);
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;
221 returned_from_main = true;
224 receiver->on_exit([&in_on_exit](bool) { in_on_exit = true; });
226 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&send_done]() {
227 assert_exit(true, 1);
229 REQUIRE_NETWORK_FAILURE(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000));
233 simgrid::s4u::this_actor::sleep_for(1);
234 receiver->get_host()->turn_off();
236 // Note: If we don't sleep here, we don't "see" the bug
237 simgrid::s4u::this_actor::sleep_for(1);
239 INFO("Receiver's on_exit function was never called");
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");
249 receiver->get_host()->turn_on();
254 BEGIN_SECTION("comm turn link off before send/recv")
256 XBT_INFO("try to communicate with communicating link turned off before start");
257 test_link_off_helper(0.0);
262 BEGIN_SECTION("comm turn link off between send/recv")
264 XBT_INFO("try to communicate with communicating link turned off between send and receive");
265 test_link_off_helper(1.0);
270 BEGIN_SECTION("comm turn link off during transfer")
272 XBT_INFO("try to communicate with communicating link turned off during transfer");
273 test_link_off_helper(2.0);
278 BEGIN_SECTION("comm turn link off during wait_any")
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);
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));
289 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], []() {
290 assert_exit(true, 2);
292 REQUIRE_NETWORK_FAILURE(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000));
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);
300 simgrid::s4u::Link::by_name("link1")->turn_on();
305 simgrid::s4u::this_actor::sleep_for(10);