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 static void test_link_off_helper(double delay)
10 const double start = simgrid::s4u::Engine::get_clock();
12 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
14 double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
15 for (int i = 0; i < 5; i++)
16 milestone[i] += start;
17 for (int i = 0; i < 4; i++) {
18 simgrid::s4u::this_actor::sleep_until(milestone[i]);
20 XBT_VERB("get(%c)", 'A' + i);
21 simgrid::s4u::Mailbox::by_name("mb")->get();
23 } catch (simgrid::NetworkFailureException const&) {
24 XBT_VERB("got expected NetworkFailureException");
27 simgrid::s4u::this_actor::sleep_until(milestone[4]);
30 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&start]() {
33 double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
34 for (int i = 0; i < 5; i++)
35 milestone[i] += start;
36 for (int i = 0; i < 2; i++) {
37 simgrid::s4u::this_actor::sleep_until(milestone[i]);
38 XBT_VERB("dsend(%c)", 'A' + i);
39 simgrid::s4u::Mailbox::by_name("mb")->put_init(&data, 100000)->detach();
41 for (int i = 2; i < 4; i++) {
42 simgrid::s4u::this_actor::sleep_until(milestone[i]);
44 XBT_VERB("put(%c)", 'A' + i);
45 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
47 } catch (simgrid::NetworkFailureException const&) {
48 XBT_VERB("got expected NetworkFailureException");
51 simgrid::s4u::this_actor::sleep_until(milestone[4]);
54 for (int i = 0; i < 4; i++) {
55 XBT_VERB("##### %d / 4 #####", i + 1);
56 simgrid::s4u::this_actor::sleep_for(delay);
58 simgrid::s4u::Link::by_name("link1")->turn_off();
59 simgrid::s4u::this_actor::sleep_for(2.0 - delay);
61 simgrid::s4u::Link::by_name("link1")->turn_on();
63 simgrid::s4u::this_actor::sleep_for(1.5);
66 TEST_CASE("Activity lifecycle: comm activities")
68 XBT_INFO("#####[ launch next \"comm\" test ]#####");
72 XBT_INFO("Launch a communication");
73 bool send_done = false;
74 bool recv_done = false;
76 simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
78 char* payload = xbt_strdup("toto");
79 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
83 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
85 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
90 simgrid::s4u::this_actor::sleep_for(9);
91 INFO("Sender or receiver killed somehow. It shouldn't");
98 BEGIN_SECTION("comm dsend and quit (put before get)")
100 XBT_INFO("Launch a detached communication and end right after");
101 bool dsend_done = false;
102 bool recv_done = false;
104 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
105 assert_exit(true, 0);
106 char* payload = xbt_strdup("toto");
107 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
111 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
112 assert_exit(true, 3);
113 simgrid::s4u::this_actor::sleep_for(2);
114 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
119 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
120 simgrid::s4u::this_actor::sleep_for(4);
121 INFO("Sender or receiver killed somehow. It shouldn't");
128 BEGIN_SECTION("comm dsend and quit (get before put)")
130 XBT_INFO("Launch a detached communication and end right after");
131 bool dsend_done = false;
132 bool recv_done = false;
134 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
135 assert_exit(true, 2);
136 char* payload = xbt_strdup("toto");
137 simgrid::s4u::this_actor::sleep_for(2);
138 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
142 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
143 assert_exit(true, 3);
144 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
149 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
150 simgrid::s4u::this_actor::sleep_for(4);
151 INFO("Sender or receiver killed somehow. It shouldn't");
158 BEGIN_SECTION("comm kill sender")
160 XBT_INFO("Launch a communication and kill the sender");
161 bool send_done = false;
162 bool recv_done = false;
164 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
165 assert_exit(false, 2);
166 // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd when the sender is killed during
167 // its communication (thanks to RAII). The pointer is then released when the communication is over.
168 std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
169 simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
174 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
175 assert_exit(true, 2);
176 void* payload = nullptr;
177 REQUIRE_THROWS_AS(payload = simgrid::s4u::Mailbox::by_name("mb")->get(), simgrid::NetworkFailureException);
182 simgrid::s4u::this_actor::sleep_for(2);
184 // let the test ends by itself. waiting for surf_precision should be enough.
185 simgrid::s4u::this_actor::sleep_for(0.00001);
187 INFO("Sender was not killed properly or receiver killed somehow. It shouldn't");
188 REQUIRE(not send_done);
194 BEGIN_SECTION("comm recv and kill")
196 XBT_INFO("Launch an actor that waits on a recv, kill its host");
197 bool in_on_exit = false;
198 bool returned_from_main = false;
199 bool in_catch_before_on_exit = false;
200 bool in_catch_after_on_exit = false;
201 bool send_done = false;
203 simgrid::s4u::ActorPtr receiver =
204 simgrid::s4u::Actor::create("receiver", all_hosts[1], [&in_on_exit, &returned_from_main,
205 &in_catch_before_on_exit, &in_catch_after_on_exit]() {
206 assert_exit(false, 1);
208 simgrid::s4u::Mailbox::by_name("mb")->get();
209 } catch (simgrid::NetworkFailureException const&) {
210 // Shouldn't get in here
211 in_catch_before_on_exit = not in_on_exit;
212 in_catch_after_on_exit = in_on_exit;
214 returned_from_main = true;
217 receiver->on_exit([&in_on_exit](bool) { in_on_exit = true; });
219 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&send_done]() {
220 assert_exit(true, 1);
222 REQUIRE_THROWS_AS(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000), simgrid::NetworkFailureException);
226 simgrid::s4u::this_actor::sleep_for(1);
227 receiver->get_host()->turn_off();
229 // Note: If we don't sleep here, we don't "see" the bug
230 simgrid::s4u::this_actor::sleep_for(1);
232 INFO("Receiver's on_exit function was never called");
234 INFO("or receiver mistakenly went to catch clause (before the on_exit function was called)");
235 REQUIRE(not in_catch_before_on_exit);
236 INFO("or receiver mistakenly went to catch clause (after the on_exit function was called)");
237 REQUIRE(not in_catch_after_on_exit);
238 INFO("or receiver returned from main normally even though its host was killed");
239 REQUIRE(not returned_from_main);
240 INFO("or sender killed somehow, and it shouldn't");
242 receiver->get_host()->turn_on();
247 BEGIN_SECTION("comm turn link off before send/recv")
249 XBT_INFO("try to communicate with communicating link turned off before start");
250 test_link_off_helper(0.0);
255 BEGIN_SECTION("comm turn link off between send/recv")
257 XBT_INFO("try to communicate with communicating link turned off between send and receive");
258 test_link_off_helper(1.0);
263 BEGIN_SECTION("comm turn link off during transfer")
265 XBT_INFO("try to communicate with communicating link turned off during transfer");
266 test_link_off_helper(2.0);
271 BEGIN_SECTION("comm turn link off during wait_any")
273 XBT_INFO("try to communicate with communicating link turned off during wait_any");
274 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], []() {
275 assert_exit(true, 2);
277 std::vector<simgrid::s4u::CommPtr> pending_comms;
278 simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("mb")->get_async((void**)&data);
279 pending_comms.push_back(comm);
280 REQUIRE_THROWS_AS(simgrid::s4u::Comm::wait_any(&pending_comms), simgrid::NetworkFailureException);
283 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], []() {
284 assert_exit(true, 2);
286 REQUIRE_THROWS_AS(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000), simgrid::NetworkFailureException);
289 simgrid::s4u::this_actor::sleep_for(2.0);
290 XBT_VERB("link off");
291 simgrid::s4u::Link::by_name("link1")->turn_off();
292 simgrid::s4u::this_actor::sleep_for(2.0);
294 simgrid::s4u::Link::by_name("link1")->turn_on();
299 simgrid::s4u::this_actor::sleep_for(10);