Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:Adrien.Gougeon/simgrid
[simgrid.git] / teshsuite / s4u / activity-lifecycle / testing_exec.cpp
1 /* Copyright (c) 2010-2020. 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: exec activities")
9 {
10   XBT_INFO("#####[ launch next \"exec\" test ]#####");
11
12   BEGIN_SECTION("exec")
13   {
14     XBT_INFO("Launch an execute(5s), and let it proceed");
15     bool global = false;
16
17     simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&global]() {
18       assert_exit(true, 5.);
19       simgrid::s4u::this_actor::execute(500000000);
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("exec killed at start")
31   {
32     XBT_INFO("Launch an execute(5s), and kill it right after start");
33     simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_killed", all_hosts[1], []() {
34       assert_exit(false, 0);
35       simgrid::s4u::this_actor::execute(500000000);
36       FAIL("I should be dead now");
37     });
38
39     simgrid::s4u::this_actor::yield();
40     exec5->kill();
41
42     END_SECTION;
43   }
44
45   BEGIN_SECTION("exec killed in middle")
46   {
47     XBT_INFO("Launch an execute(5s), and kill it after 2 secs");
48     simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_killed", all_hosts[1], []() {
49       assert_exit(false, 2);
50       simgrid::s4u::this_actor::execute(500000000);
51       FAIL("I should be dead now");
52     });
53
54     simgrid::s4u::this_actor::sleep_for(2);
55     exec5->kill();
56
57     END_SECTION;
58   }
59
60   BEGIN_SECTION("exec restarted at start")
61   {
62     XBT_INFO("Launch an execute(5s), and restart its host right after start");
63     simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], []() {
64       assert_exit(false, 0);
65       simgrid::s4u::this_actor::execute(500000000);
66       FAIL("I should be dead now");
67     });
68
69     simgrid::s4u::this_actor::yield();
70     exec5->get_host()->turn_off();
71     exec5->get_host()->turn_on();
72
73     END_SECTION;
74   }
75
76   BEGIN_SECTION("exec restarted in middle")
77   {
78     XBT_INFO("Launch an execute(5s), and restart its host after 2 secs");
79     simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], []() {
80       assert_exit(false, 2);
81       simgrid::s4u::this_actor::execute(500000000);
82       FAIL("I should be dead now");
83     });
84
85     simgrid::s4u::this_actor::sleep_for(2);
86     exec5->get_host()->turn_off();
87     exec5->get_host()->turn_on();
88
89     END_SECTION;
90   }
91
92   BEGIN_SECTION("exec restarted at end")
93   {
94     XBT_INFO("Launch an execute(5s), and restart its host right when it stops");
95     bool execution_done = false;
96
97     simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], [&execution_done]() {
98       assert_exit(true, 5);
99       simgrid::s4u::this_actor::execute(500000000);
100       execution_done = true;
101     });
102
103     simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
104       simgrid::s4u::this_actor::sleep_for(5);
105       XBT_VERB("Killer!");
106       all_hosts[1]->turn_off();
107       all_hosts[1]->turn_on();
108     });
109
110     simgrid::s4u::this_actor::sleep_for(9);
111     INFO("Was restarted actor already dead in the scheduling round during which the host_off simcall was issued?");
112     REQUIRE(execution_done);
113
114     END_SECTION;
115   }
116
117   simgrid::s4u::this_actor::sleep_for(10);
118   assert_cleanup();
119 }