Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / teshsuite / s4u / activity-lifecycle / testing_sleep.cpp
1 /* Copyright (c) 2010-2022. 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 TEST_CASE("Activity lifecycle: sleep activities")
9 {
10   XBT_INFO("#####[ launch next \"sleep\" test ]#####");
11
12   BEGIN_SECTION("sleep")
13   {
14     XBT_INFO("Launch a sleep(5), and let it proceed");
15     bool global = false;
16
17     simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5", all_hosts[1], [&global]() {
18       assert_exit(true, 5);
19       simgrid::s4u::this_actor::sleep_for(5);
20       global = true;
21     });
22
23     simgrid::s4u::this_actor::sleep_for(9);
24     INFO("Did the forked actor modify the global after sleeping, or was it killed before?");
25     REQUIRE(global);
26
27     END_SECTION;
28   }
29
30   BEGIN_SECTION("sleep killed at start")
31   {
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");
37     });
38
39     simgrid::s4u::this_actor::yield();
40     sleeper5->kill();
41
42     END_SECTION;
43   }
44
45   BEGIN_SECTION("sleep killed in middle")
46   {
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");
52     });
53
54     simgrid::s4u::this_actor::sleep_for(2);
55     sleeper5->kill();
56
57     END_SECTION;
58   }
59
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 */
62
63   BEGIN_SECTION("sleep restarted at start")
64   {
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");
70     });
71
72     simgrid::s4u::this_actor::yield();
73     sleeper5->get_host()->turn_off();
74     sleeper5->get_host()->turn_on();
75
76     END_SECTION;
77   }
78
79   BEGIN_SECTION("sleep restarted in middle")
80   {
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");
86     });
87
88     simgrid::s4u::this_actor::sleep_for(2);
89     sleeper5->get_host()->turn_off();
90     sleeper5->get_host()->turn_on();
91
92     END_SECTION;
93   }
94
95   BEGIN_SECTION("sleep restarted at end")
96   {
97     XBT_INFO("Launch a sleep(5), and restart its host right when it stops");
98     bool sleeper_done = false;
99
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);
103       sleeper_done = true;
104     });
105
106     simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
107       simgrid::s4u::this_actor::sleep_for(5);
108       XBT_VERB("Killer!");
109       all_hosts[1]->turn_off();
110       all_hosts[1]->turn_on();
111     });
112
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);
116
117     END_SECTION;
118   }
119
120   BEGIN_SECTION("turn off its own host")
121   {
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");
128     });
129
130     simgrid::s4u::this_actor::sleep_for(9);
131     all_hosts[1]->turn_on();
132
133     END_SECTION;
134   }
135
136   simgrid::s4u::this_actor::sleep_for(10);
137   assert_cleanup();
138 }