1 /* Copyright (c) 2010-2022. 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 TEST_CASE("Activity lifecycle: sleep activities")
10 XBT_INFO("#####[ launch next \"sleep\" test ]#####");
12 BEGIN_SECTION("sleep")
14 XBT_INFO("Launch a sleep(5), and let it proceed");
17 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5", all_hosts[1], [&global]() {
19 simgrid::s4u::this_actor::sleep_for(5);
23 simgrid::s4u::this_actor::sleep_for(9);
24 INFO("Did the forked actor modify the global after sleeping, or was it killed before?");
30 BEGIN_SECTION("sleep killed at start")
32 XBT_INFO("Launch a sleep(5), and kill it right after start");
33 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
34 assert_exit(false, 0);
35 simgrid::s4u::this_actor::sleep_for(5);
36 FAIL("I should be dead now");
39 simgrid::s4u::this_actor::yield();
45 BEGIN_SECTION("sleep killed in middle")
47 XBT_INFO("Launch a sleep(5), and kill it after 2 secs");
48 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
49 assert_exit(false, 2);
50 simgrid::s4u::this_actor::sleep_for(5);
51 FAIL("I should be dead now");
54 simgrid::s4u::this_actor::sleep_for(2);
60 /* We cannot kill right at the end of the action because killer actors are always rescheduled to the end of the round
61 * to avoid that they exit before their victim dereferences their name */
63 BEGIN_SECTION("sleep restarted at start")
65 XBT_INFO("Launch a sleep(5), and restart its host right after start");
66 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
67 assert_exit(false, 0);
68 simgrid::s4u::this_actor::sleep_for(5);
69 FAIL("I should be dead now");
72 simgrid::s4u::this_actor::yield();
73 sleeper5->get_host()->turn_off();
74 sleeper5->get_host()->turn_on();
79 BEGIN_SECTION("sleep restarted in middle")
81 XBT_INFO("Launch a sleep(5), and restart its host after 2 secs");
82 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
83 assert_exit(false, 2);
84 simgrid::s4u::this_actor::sleep_for(5);
85 FAIL("I should be dead now");
88 simgrid::s4u::this_actor::sleep_for(2);
89 sleeper5->get_host()->turn_off();
90 sleeper5->get_host()->turn_on();
95 BEGIN_SECTION("sleep restarted at end")
97 XBT_INFO("Launch a sleep(5), and restart its host right when it stops");
98 bool sleeper_done = false;
100 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], [&sleeper_done]() {
101 assert_exit(true, 5);
102 simgrid::s4u::this_actor::sleep_for(5);
106 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
107 simgrid::s4u::this_actor::sleep_for(5);
109 all_hosts[1]->turn_off();
110 all_hosts[1]->turn_on();
113 simgrid::s4u::this_actor::sleep_for(9);
114 INFO("Was restarted actor already dead in the scheduling round during which the host_off simcall was issued?");
115 REQUIRE(sleeper_done);
120 BEGIN_SECTION("turn off its own host")
122 XBT_INFO("Launch a sleep(5), then saw off the branch it's sitting on");
123 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
124 assert_exit(false, 5);
125 simgrid::s4u::this_actor::sleep_for(5);
126 simgrid::s4u::this_actor::get_host()->turn_off();
127 FAIL("I should be dead now");
130 simgrid::s4u::this_actor::sleep_for(9);
131 all_hosts[1]->turn_on();
136 simgrid::s4u::this_actor::sleep_for(10);