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 #define CATCH_CONFIG_RUNNER // we supply our own main()
8 #include "../../src/include/catch.hpp"
10 #include <simgrid/s4u.hpp>
11 #include <xbt/config.hpp>
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
15 std::vector<simgrid::s4u::Host*> all_hosts;
17 /* Helper function easing the testing of actor's ending condition */
18 static void assert_exit(bool exp_success, double duration)
20 double expected_time = simgrid::s4u::Engine::get_clock() + duration;
21 simgrid::s4u::this_actor::on_exit([exp_success, expected_time](bool got_failed) {
22 INFO("Check exit status. Expected: " << exp_success);
23 REQUIRE(exp_success == not got_failed);
24 INFO("Check date at exit. Expected: " << expected_time);
25 REQUIRE(simgrid::s4u::Engine::get_clock() == Approx(expected_time));
26 XBT_VERB("Checks on exit successful");
30 /* Helper function in charge of doing some sanity checks after each test */
31 static void assert_cleanup()
33 /* Check that no actor remain (but on host[0], where main_dispatcher lives */
34 for (unsigned int i = 0; i < all_hosts.size(); i++) {
35 std::vector<simgrid::s4u::ActorPtr> all_actors = all_hosts[i]->get_all_actors();
36 unsigned int expected_count = (i == 0) ? 1 : 0; // host[0] contains main_dispatcher, all other are empty
37 if (all_actors.size() != expected_count) {
38 INFO("Host " << all_hosts[i]->get_cname() << " contains " << all_actors.size() << " actors but " << expected_count
39 << " are expected (i=" << i << "). Existing actors: ");
40 for (auto act : all_actors)
41 UNSCOPED_INFO(" - " << act->get_cname());
42 FAIL("This is wrong");
45 // TODO: Check that all LMM are empty
52 /* We need an extra actor here, so that it can sleep until the end of each test */
53 #define BEGIN_SECTION(descr) SECTION(descr) { simgrid::s4u::Actor::create(descr, all_hosts[0], []()
54 #define END_SECTION })
56 TEST_CASE("Activity lifecycle: sleep activities")
58 XBT_INFO("#####[ launch next \"sleep\" test ]#####");
60 BEGIN_SECTION("sleep")
62 XBT_INFO("Launch a sleep(5), and let it proceed");
65 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5", all_hosts[1], [&global]() {
67 simgrid::s4u::this_actor::sleep_for(5);
71 simgrid::s4u::this_actor::sleep_for(9);
72 INFO("Did the forked actor modify the global after sleeping, or was it killed before?");
78 BEGIN_SECTION("sleep killed at start")
80 XBT_INFO("Launch a sleep(5), and kill it right after start");
81 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
82 assert_exit(false, 0);
83 simgrid::s4u::this_actor::sleep_for(5);
84 FAIL("I should be dead now");
87 simgrid::s4u::this_actor::yield();
93 BEGIN_SECTION("sleep killed in middle")
95 XBT_INFO("Launch a sleep(5), and kill it after 2 secs");
96 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
97 assert_exit(false, 2);
98 simgrid::s4u::this_actor::sleep_for(5);
99 FAIL("I should be dead now");
102 simgrid::s4u::this_actor::sleep_for(2);
108 /* We cannot kill right at the end of the action because killer actors are always rescheduled to the end of the round
109 * to avoid that they exit before their victim dereferences their name */
111 BEGIN_SECTION("sleep restarted at start")
113 XBT_INFO("Launch a sleep(5), and restart its host right after start");
114 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
115 assert_exit(false, 0);
116 simgrid::s4u::this_actor::sleep_for(5);
117 FAIL("I should be dead now");
120 simgrid::s4u::this_actor::yield();
121 sleeper5->get_host()->turn_off();
122 sleeper5->get_host()->turn_on();
127 BEGIN_SECTION("sleep restarted in middle")
129 XBT_INFO("Launch a sleep(5), and restart its host after 2 secs");
130 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
131 assert_exit(false, 2);
132 simgrid::s4u::this_actor::sleep_for(5);
133 FAIL("I should be dead now");
136 simgrid::s4u::this_actor::sleep_for(2);
137 sleeper5->get_host()->turn_off();
138 sleeper5->get_host()->turn_on();
143 BEGIN_SECTION("sleep restarted at end")
145 XBT_INFO("Launch a sleep(5), and restart its host right when it stops");
146 bool sleeper_done = false;
148 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], [&sleeper_done]() {
149 assert_exit(true, 5);
150 simgrid::s4u::this_actor::sleep_for(5);
154 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
155 simgrid::s4u::this_actor::sleep_for(5);
157 all_hosts[1]->turn_off();
158 all_hosts[1]->turn_on();
161 simgrid::s4u::this_actor::sleep_for(9);
162 INFO("Was restarted actor already dead in the scheduling round during which the host_off simcall was issued?");
163 REQUIRE(sleeper_done);
168 BEGIN_SECTION("turn off its own host")
170 XBT_INFO("Launch a sleep(5), then saw off the branch it's sitting on");
171 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
172 assert_exit(false, 5);
173 simgrid::s4u::this_actor::sleep_for(5);
174 simgrid::s4u::this_actor::get_host()->turn_off();
175 FAIL("I should be dead now");
178 simgrid::s4u::this_actor::sleep_for(9);
179 all_hosts[1]->turn_on();
184 simgrid::s4u::this_actor::sleep_for(10);
188 TEST_CASE("Activity lifecycle: exec activities")
190 XBT_INFO("#####[ launch next \"exec\" test ]#####");
192 BEGIN_SECTION("exec")
194 XBT_INFO("Launch a execute(5s), and let it proceed");
197 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&global]() {
198 assert_exit(true, 5.);
199 simgrid::s4u::this_actor::execute(500000000);
203 simgrid::s4u::this_actor::sleep_for(9);
204 INFO("Did the forked actor modify the global after sleeping, or was it killed before?");
210 BEGIN_SECTION("exec killed at start")
212 XBT_INFO("Launch a execute(5s), and kill it right after start");
213 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_killed", all_hosts[1], []() {
214 assert_exit(false, 0);
215 simgrid::s4u::this_actor::execute(500000000);
216 FAIL("I should be dead now");
219 simgrid::s4u::this_actor::yield();
225 BEGIN_SECTION("exec killed in middle")
227 XBT_INFO("Launch a execute(5s), and kill it after 2 secs");
228 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_killed", all_hosts[1], []() {
229 assert_exit(false, 2);
230 simgrid::s4u::this_actor::execute(500000000);
231 FAIL("I should be dead now");
234 simgrid::s4u::this_actor::sleep_for(2);
240 BEGIN_SECTION("exec restarted at start")
242 XBT_INFO("Launch a execute(5s), and restart its host right after start");
243 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], []() {
244 assert_exit(false, 0);
245 simgrid::s4u::this_actor::execute(500000000);
246 FAIL("I should be dead now");
249 simgrid::s4u::this_actor::yield();
250 exec5->get_host()->turn_off();
251 exec5->get_host()->turn_on();
256 BEGIN_SECTION("exec restarted in middle")
258 XBT_INFO("Launch a execute(5s), and restart its host after 2 secs");
259 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], []() {
260 assert_exit(false, 2);
261 simgrid::s4u::this_actor::execute(500000000);
262 FAIL("I should be dead now");
265 simgrid::s4u::this_actor::sleep_for(2);
266 exec5->get_host()->turn_off();
267 exec5->get_host()->turn_on();
272 BEGIN_SECTION("exec restarted at end")
274 XBT_INFO("Launch a execute(5s), and restart its host right when it stops");
275 bool execution_done = false;
277 simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], [&execution_done]() {
278 assert_exit(true, 5);
279 simgrid::s4u::this_actor::execute(500000000);
280 execution_done = true;
283 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
284 simgrid::s4u::this_actor::sleep_for(5);
286 all_hosts[1]->turn_off();
287 all_hosts[1]->turn_on();
290 simgrid::s4u::this_actor::sleep_for(9);
291 INFO("Was restarted actor already dead in the scheduling round during which the host_off simcall was issued?");
292 REQUIRE(execution_done);
297 simgrid::s4u::this_actor::sleep_for(10);
301 TEST_CASE("Activity lifecycle: comm activities")
303 XBT_INFO("#####[ launch next \"comm\" test ]#####");
305 BEGIN_SECTION("comm")
307 XBT_INFO("Launch a communication");
308 bool send_done = false;
309 bool recv_done = false;
311 simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
312 assert_exit(true, 5);
313 char* payload = xbt_strdup("toto");
314 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
318 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
319 assert_exit(true, 5);
320 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
325 simgrid::s4u::this_actor::sleep_for(9);
326 INFO("Sender or receiver killed somehow. It shouldn't");
333 BEGIN_SECTION("comm dsend and quit (put before get)")
335 XBT_INFO("Launch a detached communication and end right after");
336 bool dsend_done = false;
337 bool recv_done = false;
339 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
340 assert_exit(true, 0);
341 char* payload = xbt_strdup("toto");
342 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
346 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
347 assert_exit(true, 3);
348 simgrid::s4u::this_actor::sleep_for(2);
349 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
354 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
355 simgrid::s4u::this_actor::sleep_for(4);
356 INFO("Sender or receiver killed somehow. It shouldn't");
363 BEGIN_SECTION("comm dsend and quit (get before put)")
365 XBT_INFO("Launch a detached communication and end right after");
366 bool dsend_done = false;
367 bool recv_done = false;
369 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
370 assert_exit(true, 2);
371 char* payload = xbt_strdup("toto");
372 simgrid::s4u::this_actor::sleep_for(2);
373 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
377 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
378 assert_exit(true, 3);
379 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
384 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
385 simgrid::s4u::this_actor::sleep_for(4);
386 INFO("Sender or receiver killed somehow. It shouldn't");
393 BEGIN_SECTION("comm kill sender")
395 XBT_INFO("Launch a communication and kill the sender");
396 bool send_done = false;
397 bool recv_done = false;
399 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
400 assert_exit(false, 2);
401 // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd when the sender is killed during
402 // its communication (thanks to RAII). The pointer is then released when the communication is over.
403 std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
404 simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
409 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
410 assert_exit(true, 2);
411 void* payload = nullptr;
412 REQUIRE_THROWS_AS(payload = simgrid::s4u::Mailbox::by_name("mb")->get(), simgrid::NetworkFailureException);
417 simgrid::s4u::this_actor::sleep_for(2);
419 // let the test ends by itself. waiting for surf_precision should be enough.
420 simgrid::s4u::this_actor::sleep_for(0.00001);
422 INFO("Sender was not killed properly or receiver killed somehow. It shouldn't");
423 REQUIRE(not send_done);
429 BEGIN_SECTION("comm recv and kill")
431 XBT_INFO("Launch an actor that waits on a recv, kill its host");
432 bool in_on_exit = false;
433 bool returned_from_main = false;
434 bool in_catch_before_on_exit = false;
435 bool in_catch_after_on_exit = false;
436 bool send_done = false;
438 simgrid::s4u::ActorPtr receiver =
439 simgrid::s4u::Actor::create("receiver", all_hosts[1], [&in_on_exit, &returned_from_main,
440 &in_catch_before_on_exit, &in_catch_after_on_exit]() {
441 assert_exit(false, 1);
443 simgrid::s4u::Mailbox::by_name("mb")->get();
444 } catch (simgrid::NetworkFailureException const&) {
445 // Shouldn't get in here
446 in_catch_before_on_exit = not in_on_exit;
447 in_catch_after_on_exit = in_on_exit;
449 returned_from_main = true;
452 receiver->on_exit([&in_on_exit](bool) { in_on_exit = true; });
454 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&send_done]() {
455 assert_exit(true, 1);
457 REQUIRE_THROWS_AS(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000), simgrid::NetworkFailureException);
461 simgrid::s4u::this_actor::sleep_for(1);
462 receiver->get_host()->turn_off();
464 // Note: If we don't sleep here, we don't "see" the bug
465 simgrid::s4u::this_actor::sleep_for(1);
467 INFO("Receiver's on_exit function was never called");
469 INFO("or receiver mistakenly went to catch clause (before the on_exit function was called)");
470 REQUIRE(not in_catch_before_on_exit);
471 INFO("or receiver mistakenly went to catch clause (after the on_exit function was called)");
472 REQUIRE(not in_catch_after_on_exit);
473 INFO("or receiver returned from main normally even though its host was killed");
474 REQUIRE(not returned_from_main);
475 INFO("or sender killed somehow, and it shouldn't");
477 receiver->get_host()->turn_on();
482 static auto test_link_off_helper = [](double delay) {
483 const double start = simgrid::s4u::Engine::get_clock();
485 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
486 assert_exit(true, 9);
487 double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
488 for (int i = 0; i < 5; i++)
489 milestone[i] += start;
490 for (int i = 0; i < 4; i++) {
491 simgrid::s4u::this_actor::sleep_until(milestone[i]);
493 XBT_VERB("get(%c)", 'A' + i);
494 simgrid::s4u::Mailbox::by_name("mb")->get();
496 } catch (simgrid::NetworkFailureException const&) {
497 XBT_VERB("got expected NetworkFailureException");
500 simgrid::s4u::this_actor::sleep_until(milestone[4]);
503 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&start]() {
504 assert_exit(true, 9);
506 double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
507 for (int i = 0; i < 5; i++)
508 milestone[i] += start;
509 for (int i = 0; i < 2; i++) {
510 simgrid::s4u::this_actor::sleep_until(milestone[i]);
511 XBT_VERB("dsend(%c)", 'A' + i);
512 simgrid::s4u::Mailbox::by_name("mb")->put_init(&data, 100000)->detach();
514 for (int i = 2; i < 4; i++) {
515 simgrid::s4u::this_actor::sleep_until(milestone[i]);
517 XBT_VERB("put(%c)", 'A' + i);
518 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
520 } catch (simgrid::NetworkFailureException const&) {
521 XBT_VERB("got expected NetworkFailureException");
524 simgrid::s4u::this_actor::sleep_until(milestone[4]);
527 for (int i = 0; i < 4; i++) {
528 XBT_VERB("##### %d / 4 #####", i + 1);
529 simgrid::s4u::this_actor::sleep_for(delay);
530 XBT_VERB("link off");
531 simgrid::s4u::Link::by_name("link1")->turn_off();
532 simgrid::s4u::this_actor::sleep_for(2.0 - delay);
534 simgrid::s4u::Link::by_name("link1")->turn_on();
536 simgrid::s4u::this_actor::sleep_for(1.5);
539 BEGIN_SECTION("comm turn link off before send/recv")
541 XBT_INFO("try to communicate with communicating link turned off before start");
542 test_link_off_helper(0.0);
547 BEGIN_SECTION("comm turn link off between send/recv")
549 XBT_INFO("try to communicate with communicating link turned off between send and receive");
550 test_link_off_helper(1.0);
555 BEGIN_SECTION("comm turn link off during transfer")
557 XBT_INFO("try to communicate with communicating link turned off during transfer");
558 test_link_off_helper(2.0);
563 BEGIN_SECTION("comm turn link off during wait_any")
565 XBT_INFO("try to communicate with communicating link turned off during wait_any");
566 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], []() {
567 assert_exit(true, 2);
569 std::vector<simgrid::s4u::CommPtr> pending_comms;
570 simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("mb")->get_async((void**)&data);
571 pending_comms.push_back(comm);
572 REQUIRE_THROWS_AS(simgrid::s4u::Comm::wait_any(&pending_comms), simgrid::NetworkFailureException);
575 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], []() {
576 assert_exit(true, 2);
578 REQUIRE_THROWS_AS(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000), simgrid::NetworkFailureException);
581 simgrid::s4u::this_actor::sleep_for(2.0);
582 XBT_VERB("link off");
583 simgrid::s4u::Link::by_name("link1")->turn_off();
584 simgrid::s4u::this_actor::sleep_for(2.0);
586 simgrid::s4u::Link::by_name("link1")->turn_on();
591 simgrid::s4u::this_actor::sleep_for(10);
595 int main(int argc, char* argv[])
597 simgrid::config::set_value("help-nostop", true);
598 simgrid::s4u::Engine e(&argc, argv);
607 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
608 platf = "./testing_platform.xml";
610 e.load_platform(platf);
613 all_hosts = e.get_all_hosts();
614 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0],
615 [&argc, &argv, &status]() { status = Catch::Session().run(argc, argv); });
618 XBT_INFO("Simulation done");